base.go 13 KB

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