registry.go 5.3 KB

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