2
0

registry.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/go-chi/chi"
  5. "github.com/porter-dev/porter/api/server/handlers/registry"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/server/shared/router"
  9. "github.com/porter-dev/porter/api/types"
  10. )
  11. func NewRegistryScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  12. return &router.Registerer{
  13. GetRoutes: GetRegistryScopedRoutes,
  14. Children: children,
  15. }
  16. }
  17. func GetRegistryScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*router.Registerer,
  23. ) []*router.Route {
  24. routes, projPath := getRegistryRoutes(r, config, basePath, factory)
  25. if len(children) > 0 {
  26. r.Route(projPath.RelativePath, func(r chi.Router) {
  27. for _, child := range children {
  28. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  29. routes = append(routes, childRoutes...)
  30. }
  31. })
  32. }
  33. return routes
  34. }
  35. func getRegistryRoutes(
  36. r chi.Router,
  37. config *config.Config,
  38. basePath *types.Path,
  39. factory shared.APIEndpointFactory,
  40. ) ([]*router.Route, *types.Path) {
  41. relPath := "/registries/{registry_id}"
  42. newPath := &types.Path{
  43. Parent: basePath,
  44. RelativePath: relPath,
  45. }
  46. routes := make([]*router.Route, 0)
  47. // GET /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryGetHandler
  48. getEndpoint := factory.NewAPIEndpoint(
  49. &types.APIRequestMetadata{
  50. Verb: types.APIVerbGet,
  51. Method: types.HTTPVerbGet,
  52. Path: &types.Path{
  53. Parent: basePath,
  54. RelativePath: relPath,
  55. },
  56. Scopes: []types.PermissionScope{
  57. types.UserScope,
  58. types.ProjectScope,
  59. types.RegistryScope,
  60. },
  61. },
  62. )
  63. getHandler := registry.NewRegistryGetHandler(
  64. config,
  65. factory.GetResultWriter(),
  66. )
  67. routes = append(routes, &router.Route{
  68. Endpoint: getEndpoint,
  69. Handler: getHandler,
  70. Router: r,
  71. })
  72. // POST /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryUpdateHandler
  73. updateEndpoint := factory.NewAPIEndpoint(
  74. &types.APIRequestMetadata{
  75. Verb: types.APIVerbUpdate,
  76. Method: types.HTTPVerbPost,
  77. Path: &types.Path{
  78. Parent: basePath,
  79. RelativePath: relPath,
  80. },
  81. Scopes: []types.PermissionScope{
  82. types.UserScope,
  83. types.ProjectScope,
  84. types.RegistryScope,
  85. },
  86. },
  87. )
  88. updateHandler := registry.NewRegistryUpdateHandler(
  89. config,
  90. factory.GetDecoderValidator(),
  91. factory.GetResultWriter(),
  92. )
  93. routes = append(routes, &router.Route{
  94. Endpoint: updateEndpoint,
  95. Handler: updateHandler,
  96. Router: r,
  97. })
  98. // DELETE /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryDeleteHandler
  99. deleteEndpoint := factory.NewAPIEndpoint(
  100. &types.APIRequestMetadata{
  101. Verb: types.APIVerbDelete,
  102. Method: types.HTTPVerbDelete,
  103. Path: &types.Path{
  104. Parent: basePath,
  105. RelativePath: relPath,
  106. },
  107. Scopes: []types.PermissionScope{
  108. types.UserScope,
  109. types.ProjectScope,
  110. types.RegistryScope,
  111. },
  112. },
  113. )
  114. deleteHandler := registry.NewRegistryDeleteHandler(
  115. config,
  116. factory.GetResultWriter(),
  117. )
  118. routes = append(routes, &router.Route{
  119. Endpoint: deleteEndpoint,
  120. Handler: deleteHandler,
  121. Router: r,
  122. })
  123. // GET /api/projects/{project_id}/registries/{registry_id}/repositories -> registry.NewRegistryListRepositoriesHandler
  124. listRepositoriesEndpoint := factory.NewAPIEndpoint(
  125. &types.APIRequestMetadata{
  126. Verb: types.APIVerbList,
  127. Method: types.HTTPVerbGet,
  128. Path: &types.Path{
  129. Parent: basePath,
  130. RelativePath: relPath + "/repositories",
  131. },
  132. Scopes: []types.PermissionScope{
  133. types.UserScope,
  134. types.ProjectScope,
  135. types.RegistryScope,
  136. },
  137. },
  138. )
  139. listRepositoriesHandler := registry.NewRegistryListRepositoriesHandler(
  140. config,
  141. factory.GetResultWriter(),
  142. )
  143. routes = append(routes, &router.Route{
  144. Endpoint: listRepositoriesEndpoint,
  145. Handler: listRepositoriesHandler,
  146. Router: r,
  147. })
  148. // GET /api/projects/{project_id}/registries/{registry_id}/repositories/* -> registry.NewRegistryListImagesHandler
  149. listImagesEndpoint := factory.NewAPIEndpoint(
  150. &types.APIRequestMetadata{
  151. Verb: types.APIVerbList,
  152. Method: types.HTTPVerbGet,
  153. Path: &types.Path{
  154. Parent: basePath,
  155. RelativePath: fmt.Sprintf(
  156. "%s/repositories/%s",
  157. relPath,
  158. types.URLParamWildcard,
  159. ),
  160. },
  161. Scopes: []types.PermissionScope{
  162. types.UserScope,
  163. types.ProjectScope,
  164. types.RegistryScope,
  165. },
  166. },
  167. )
  168. listImagesHandler := registry.NewRegistryListImagesHandler(
  169. config,
  170. factory.GetResultWriter(),
  171. )
  172. routes = append(routes, &router.Route{
  173. Endpoint: listImagesEndpoint,
  174. Handler: listImagesHandler,
  175. Router: r,
  176. })
  177. // POST /api/projects/{project_id}/registries/{registry_id}/repository -> registry.NewRegistryCreateRepositoryHandler
  178. createRepositoryEndpoint := factory.NewAPIEndpoint(
  179. &types.APIRequestMetadata{
  180. Verb: types.APIVerbCreate,
  181. Method: types.HTTPVerbPost,
  182. Path: &types.Path{
  183. Parent: basePath,
  184. RelativePath: relPath + "/repository",
  185. },
  186. Scopes: []types.PermissionScope{
  187. types.UserScope,
  188. types.ProjectScope,
  189. types.RegistryScope,
  190. },
  191. },
  192. )
  193. createRepositoryHandler := registry.NewRegistryCreateRepositoryHandler(
  194. config,
  195. factory.GetDecoderValidator(),
  196. factory.GetResultWriter(),
  197. )
  198. routes = append(routes, &router.Route{
  199. Endpoint: createRepositoryEndpoint,
  200. Handler: createRepositoryHandler,
  201. Router: r,
  202. })
  203. return routes, newPath
  204. }