base.go 11 KB

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