base.go 14 KB

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