base.go 12 KB

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