base.go 12 KB

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