base.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/billing"
  5. "github.com/porter-dev/porter/api/server/handlers/credentials"
  6. "github.com/porter-dev/porter/api/server/handlers/gitinstallation"
  7. "github.com/porter-dev/porter/api/server/handlers/healthcheck"
  8. "github.com/porter-dev/porter/api/server/handlers/metadata"
  9. "github.com/porter-dev/porter/api/server/handlers/release"
  10. "github.com/porter-dev/porter/api/server/handlers/user"
  11. "github.com/porter-dev/porter/api/server/handlers/webhook"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. "github.com/porter-dev/porter/api/types"
  15. )
  16. func NewBaseRegisterer(children ...*Registerer) *Registerer {
  17. return &Registerer{
  18. GetRoutes: GetBaseRoutes,
  19. Children: children,
  20. }
  21. }
  22. func GetBaseRoutes(
  23. r chi.Router,
  24. config *config.Config,
  25. basePath *types.Path,
  26. factory shared.APIEndpointFactory,
  27. children ...*Registerer,
  28. ) []*Route {
  29. routes := make([]*Route, 0)
  30. // GET /api/readyz -> healthcheck.NewReadyzHandler
  31. getReadyzEndpoint := factory.NewAPIEndpoint(
  32. &types.APIRequestMetadata{
  33. Verb: types.APIVerbGet,
  34. Method: types.HTTPVerbGet,
  35. Path: &types.Path{
  36. Parent: basePath,
  37. RelativePath: "/readyz",
  38. },
  39. Quiet: true,
  40. },
  41. )
  42. getReadyzHandler := healthcheck.NewReadyzHandler(
  43. config,
  44. factory.GetResultWriter(),
  45. )
  46. routes = append(routes, &Route{
  47. Endpoint: getReadyzEndpoint,
  48. Handler: getReadyzHandler,
  49. Router: r,
  50. })
  51. // GET /api/livez -> healthcheck.NewLivezHandler
  52. getLivezEndpoint := factory.NewAPIEndpoint(
  53. &types.APIRequestMetadata{
  54. Verb: types.APIVerbGet,
  55. Method: types.HTTPVerbGet,
  56. Path: &types.Path{
  57. Parent: basePath,
  58. RelativePath: "/livez",
  59. },
  60. Quiet: true,
  61. },
  62. )
  63. getLivezHandler := healthcheck.NewLivezHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &Route{
  68. Endpoint: getLivezEndpoint,
  69. Handler: getLivezHandler,
  70. Router: r,
  71. })
  72. // GET /api/capabilities -> user.NewUserCreateHandler
  73. getMetadataEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbGet,
  76. Method: types.HTTPVerbGet,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: "/metadata",
  80. },
  81. },
  82. )
  83. getMetadataHandler := metadata.NewMetadataGetHandler(
  84. config,
  85. factory.GetResultWriter(),
  86. )
  87. routes = append(routes, &Route{
  88. Endpoint: getMetadataEndpoint,
  89. Handler: getMetadataHandler,
  90. Router: r,
  91. })
  92. // GET /api/integrations/cluster -> metadata.NewListClusterIntegrationsHandler
  93. listClusterIntsEndpoint := factory.NewAPIEndpoint(
  94. &types.APIRequestMetadata{
  95. Verb: types.APIVerbGet,
  96. Method: types.HTTPVerbGet,
  97. Path: &types.Path{
  98. Parent: basePath,
  99. RelativePath: "/integrations/cluster",
  100. },
  101. },
  102. )
  103. listClusterIntsHandler := metadata.NewListClusterIntegrationsHandler(
  104. config,
  105. factory.GetResultWriter(),
  106. )
  107. routes = append(routes, &Route{
  108. Endpoint: listClusterIntsEndpoint,
  109. Handler: listClusterIntsHandler,
  110. Router: r,
  111. })
  112. // GET /api/integrations/registry -> metadata.NewListRegistryIntegrationsHandler
  113. listRegistryIntsEndpoint := factory.NewAPIEndpoint(
  114. &types.APIRequestMetadata{
  115. Verb: types.APIVerbGet,
  116. Method: types.HTTPVerbGet,
  117. Path: &types.Path{
  118. Parent: basePath,
  119. RelativePath: "/integrations/registry",
  120. },
  121. },
  122. )
  123. listRegistryIntsHandler := metadata.NewListRegistryIntegrationsHandler(
  124. config,
  125. factory.GetResultWriter(),
  126. )
  127. routes = append(routes, &Route{
  128. Endpoint: listRegistryIntsEndpoint,
  129. Handler: listRegistryIntsHandler,
  130. Router: r,
  131. })
  132. // GET /api/integrations/helm -> metadata.NewListHelmRepoIntegrationsHandler
  133. listHelmRepoIntsEndpoint := factory.NewAPIEndpoint(
  134. &types.APIRequestMetadata{
  135. Verb: types.APIVerbGet,
  136. Method: types.HTTPVerbGet,
  137. Path: &types.Path{
  138. Parent: basePath,
  139. RelativePath: "/integrations/helm",
  140. },
  141. },
  142. )
  143. listHelmRepoIntsHandler := metadata.NewListHelmRepoIntegrationsHandler(
  144. config,
  145. factory.GetResultWriter(),
  146. )
  147. routes = append(routes, &Route{
  148. Endpoint: listHelmRepoIntsEndpoint,
  149. Handler: listHelmRepoIntsHandler,
  150. Router: r,
  151. })
  152. // POST /api/users -> user.NewUserCreateHandler
  153. createUserEndpoint := factory.NewAPIEndpoint(
  154. &types.APIRequestMetadata{
  155. Verb: types.APIVerbCreate,
  156. Method: types.HTTPVerbPost,
  157. Path: &types.Path{
  158. Parent: basePath,
  159. RelativePath: "/users",
  160. },
  161. },
  162. )
  163. createUserHandler := user.NewUserCreateHandler(
  164. config,
  165. factory.GetDecoderValidator(),
  166. factory.GetResultWriter(),
  167. )
  168. routes = append(routes, &Route{
  169. Endpoint: createUserEndpoint,
  170. Handler: createUserHandler,
  171. Router: r,
  172. })
  173. // POST /api/login -> user.NewUserLoginHandler
  174. loginUserEndpoint := factory.NewAPIEndpoint(
  175. &types.APIRequestMetadata{
  176. Verb: types.APIVerbUpdate,
  177. Method: types.HTTPVerbPost,
  178. Path: &types.Path{
  179. Parent: basePath,
  180. RelativePath: "/login",
  181. },
  182. },
  183. )
  184. loginUserHandler := user.NewUserLoginHandler(
  185. config,
  186. factory.GetDecoderValidator(),
  187. factory.GetResultWriter(),
  188. )
  189. routes = append(routes, &Route{
  190. Endpoint: loginUserEndpoint,
  191. Handler: loginUserHandler,
  192. Router: r,
  193. })
  194. // POST /api/cli/login/exchange -> user.NewCLILoginExchangeHandler
  195. cliLoginExchangeEndpoint := factory.NewAPIEndpoint(
  196. &types.APIRequestMetadata{
  197. Verb: types.APIVerbCreate,
  198. Method: types.HTTPVerbPost,
  199. Path: &types.Path{
  200. Parent: basePath,
  201. RelativePath: "/cli/login/exchange",
  202. },
  203. },
  204. )
  205. cliLoginExchangeHandler := user.NewCLILoginExchangeHandler(
  206. config,
  207. factory.GetDecoderValidator(),
  208. factory.GetResultWriter(),
  209. )
  210. routes = append(routes, &Route{
  211. Endpoint: cliLoginExchangeEndpoint,
  212. Handler: cliLoginExchangeHandler,
  213. Router: r,
  214. })
  215. // POST /api/password/reset/initiate -> user.NewUserPasswordInitiateResetHandler
  216. passwordInitiateResetEndpoint := factory.NewAPIEndpoint(
  217. &types.APIRequestMetadata{
  218. Verb: types.APIVerbCreate,
  219. Method: types.HTTPVerbPost,
  220. Path: &types.Path{
  221. Parent: basePath,
  222. RelativePath: "/password/reset/initiate",
  223. },
  224. },
  225. )
  226. passwordInitiateResetHandler := user.NewUserPasswordInitiateResetHandler(
  227. config,
  228. factory.GetDecoderValidator(),
  229. factory.GetResultWriter(),
  230. )
  231. routes = append(routes, &Route{
  232. Endpoint: passwordInitiateResetEndpoint,
  233. Handler: passwordInitiateResetHandler,
  234. Router: r,
  235. })
  236. // POST /api/password/reset/verify -> user.NewUserPasswordVerifyResetHandler
  237. passwordVerifyResetEndpoint := factory.NewAPIEndpoint(
  238. &types.APIRequestMetadata{
  239. Verb: types.APIVerbCreate,
  240. Method: types.HTTPVerbPost,
  241. Path: &types.Path{
  242. Parent: basePath,
  243. RelativePath: "/password/reset/verify",
  244. },
  245. },
  246. )
  247. passwordVerifyResetHandler := user.NewUserPasswordVerifyResetHandler(
  248. config,
  249. factory.GetDecoderValidator(),
  250. factory.GetResultWriter(),
  251. )
  252. routes = append(routes, &Route{
  253. Endpoint: passwordVerifyResetEndpoint,
  254. Handler: passwordVerifyResetHandler,
  255. Router: r,
  256. })
  257. // POST /api/password/reset/finalize -> user.NewUserPasswordFinalizeResetHandler
  258. passwordFinalizeResetEndpoint := factory.NewAPIEndpoint(
  259. &types.APIRequestMetadata{
  260. Verb: types.APIVerbCreate,
  261. Method: types.HTTPVerbPost,
  262. Path: &types.Path{
  263. Parent: basePath,
  264. RelativePath: "/password/reset/finalize",
  265. },
  266. },
  267. )
  268. passwordFinalizeResetHandler := user.NewUserPasswordFinalizeResetHandler(
  269. config,
  270. factory.GetDecoderValidator(),
  271. factory.GetResultWriter(),
  272. )
  273. routes = append(routes, &Route{
  274. Endpoint: passwordFinalizeResetEndpoint,
  275. Handler: passwordFinalizeResetHandler,
  276. Router: r,
  277. })
  278. // POST /api/webhooks/deploy/{token} -> release.NewWebhookHandler
  279. webhookEndpoint := factory.NewAPIEndpoint(
  280. &types.APIRequestMetadata{
  281. Verb: types.APIVerbUpdate,
  282. Method: types.HTTPVerbPost,
  283. Path: &types.Path{
  284. Parent: basePath,
  285. RelativePath: "/webhooks/deploy/{token}",
  286. },
  287. Scopes: []types.PermissionScope{},
  288. },
  289. )
  290. webhookHandler := release.NewWebhookHandler(
  291. config,
  292. factory.GetDecoderValidator(),
  293. factory.GetResultWriter(),
  294. )
  295. routes = append(routes, &Route{
  296. Endpoint: webhookEndpoint,
  297. Handler: webhookHandler,
  298. Router: r,
  299. })
  300. // GET /api/integrations/github-app/install
  301. githubAppInstallEndpoint := factory.NewAPIEndpoint(
  302. &types.APIRequestMetadata{
  303. Verb: types.APIVerbGet,
  304. Method: types.HTTPVerbGet,
  305. Path: &types.Path{
  306. Parent: basePath,
  307. RelativePath: "/integrations/github-app/install",
  308. },
  309. Scopes: []types.PermissionScope{},
  310. },
  311. )
  312. githubAppInstallHandler := gitinstallation.NewGithubAppInstallHandler(
  313. config,
  314. )
  315. routes = append(routes, &Route{
  316. Endpoint: githubAppInstallEndpoint,
  317. Handler: githubAppInstallHandler,
  318. Router: r,
  319. })
  320. // POST /api/integrations/github-app/webhook
  321. githubAppWebhookEndpoint := factory.NewAPIEndpoint(
  322. &types.APIRequestMetadata{
  323. Verb: types.APIVerbCreate,
  324. Method: types.HTTPVerbPost,
  325. Path: &types.Path{
  326. Parent: basePath,
  327. RelativePath: "/integrations/github-app/webhook",
  328. },
  329. Scopes: []types.PermissionScope{},
  330. },
  331. )
  332. githubAppWebhookHandler := gitinstallation.NewGithubAppWebhookHandler(
  333. config,
  334. factory.GetDecoderValidator(),
  335. factory.GetResultWriter(),
  336. )
  337. routes = append(routes, &Route{
  338. Endpoint: githubAppWebhookEndpoint,
  339. Handler: githubAppWebhookHandler,
  340. Router: r,
  341. })
  342. // GET /api/oauth/login/github
  343. githubLoginStartEndpoint := factory.NewAPIEndpoint(
  344. &types.APIRequestMetadata{
  345. Verb: types.APIVerbGet,
  346. Method: types.HTTPVerbGet,
  347. Path: &types.Path{
  348. Parent: basePath,
  349. RelativePath: "/oauth/login/github",
  350. },
  351. Scopes: []types.PermissionScope{},
  352. },
  353. )
  354. githubLoginStartHandler := user.NewUserOAuthGithubHandler(
  355. config,
  356. factory.GetDecoderValidator(),
  357. factory.GetResultWriter(),
  358. )
  359. routes = append(routes, &Route{
  360. Endpoint: githubLoginStartEndpoint,
  361. Handler: githubLoginStartHandler,
  362. Router: r,
  363. })
  364. // GET /api/oauth/github/callback
  365. githubLoginCallbackEndpoint := factory.NewAPIEndpoint(
  366. &types.APIRequestMetadata{
  367. Verb: types.APIVerbGet,
  368. Method: types.HTTPVerbGet,
  369. Path: &types.Path{
  370. Parent: basePath,
  371. RelativePath: "/oauth/github/callback",
  372. },
  373. Scopes: []types.PermissionScope{},
  374. },
  375. )
  376. githubLoginCallbackHandler := user.NewUserOAuthGithubCallbackHandler(
  377. config,
  378. factory.GetDecoderValidator(),
  379. factory.GetResultWriter(),
  380. )
  381. routes = append(routes, &Route{
  382. Endpoint: githubLoginCallbackEndpoint,
  383. Handler: githubLoginCallbackHandler,
  384. Router: r,
  385. })
  386. // GET /api/oauth/login/google
  387. googleLoginStartEndpoint := factory.NewAPIEndpoint(
  388. &types.APIRequestMetadata{
  389. Verb: types.APIVerbGet,
  390. Method: types.HTTPVerbGet,
  391. Path: &types.Path{
  392. Parent: basePath,
  393. RelativePath: "/oauth/login/google",
  394. },
  395. Scopes: []types.PermissionScope{},
  396. },
  397. )
  398. googleLoginStartHandler := user.NewUserOAuthGoogleHandler(
  399. config,
  400. factory.GetDecoderValidator(),
  401. factory.GetResultWriter(),
  402. )
  403. routes = append(routes, &Route{
  404. Endpoint: googleLoginStartEndpoint,
  405. Handler: googleLoginStartHandler,
  406. Router: r,
  407. })
  408. // GET /api/oauth/google/callback
  409. googleLoginCallbackEndpoint := factory.NewAPIEndpoint(
  410. &types.APIRequestMetadata{
  411. Verb: types.APIVerbGet,
  412. Method: types.HTTPVerbGet,
  413. Path: &types.Path{
  414. Parent: basePath,
  415. RelativePath: "/oauth/google/callback",
  416. },
  417. Scopes: []types.PermissionScope{},
  418. },
  419. )
  420. googleLoginCallbackHandler := user.NewUserOAuthGoogleCallbackHandler(
  421. config,
  422. factory.GetDecoderValidator(),
  423. factory.GetResultWriter(),
  424. )
  425. routes = append(routes, &Route{
  426. Endpoint: googleLoginCallbackEndpoint,
  427. Handler: googleLoginCallbackHandler,
  428. Router: r,
  429. })
  430. // GET /api/internal/credentials
  431. getCredentialsEndpoint := factory.NewAPIEndpoint(
  432. &types.APIRequestMetadata{
  433. Verb: types.APIVerbGet,
  434. Method: types.HTTPVerbGet,
  435. Path: &types.Path{
  436. Parent: basePath,
  437. RelativePath: "/internal/credentials",
  438. },
  439. Scopes: []types.PermissionScope{},
  440. },
  441. )
  442. getCredentialsHandler := credentials.NewGetCredentialsHandler(
  443. config,
  444. factory.GetDecoderValidator(),
  445. factory.GetResultWriter(),
  446. )
  447. routes = append(routes, &Route{
  448. Endpoint: getCredentialsEndpoint,
  449. Handler: getCredentialsHandler,
  450. Router: r,
  451. })
  452. // POST /api/internal/billing -> billing.NewBillingAddProjectHandler
  453. addProjectBillingEndpoint := factory.NewAPIEndpoint(
  454. &types.APIRequestMetadata{
  455. Verb: types.APIVerbCreate,
  456. Method: types.HTTPVerbPost,
  457. Path: &types.Path{
  458. Parent: basePath,
  459. RelativePath: "/internal/billing",
  460. },
  461. Scopes: []types.PermissionScope{},
  462. },
  463. )
  464. addProjectBillingHandler := billing.NewBillingAddProjectHandler(
  465. config,
  466. factory.GetDecoderValidator(),
  467. )
  468. routes = append(routes, &Route{
  469. Endpoint: addProjectBillingEndpoint,
  470. Handler: addProjectBillingHandler,
  471. Router: r,
  472. })
  473. if config.ServerConf.GithubIncomingWebhookSecret != "" {
  474. // POST /api/github/incoming_webhook -> webhook.NewGithubIncomingWebhook
  475. githubIncomingWebhookEndpoint := factory.NewAPIEndpoint(
  476. &types.APIRequestMetadata{
  477. Verb: types.APIVerbCreate,
  478. Method: types.HTTPVerbPost,
  479. Path: &types.Path{
  480. Parent: basePath,
  481. RelativePath: "/github/incoming_webhook",
  482. },
  483. Scopes: []types.PermissionScope{},
  484. },
  485. )
  486. githubIncomingWebhookHandler := webhook.NewGithubIncomingWebhookHandler(
  487. config,
  488. factory.GetDecoderValidator(),
  489. factory.GetResultWriter(),
  490. )
  491. routes = append(routes, &Route{
  492. Endpoint: githubIncomingWebhookEndpoint,
  493. Handler: githubIncomingWebhookHandler,
  494. Router: r,
  495. })
  496. }
  497. return routes
  498. }