registry.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/registry"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. func NewRegistryScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetRegistryScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetRegistryScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getRegistryRoutes(r, config, basePath, factory)
  23. if len(children) > 0 {
  24. r.Route(projPath.RelativePath, func(r chi.Router) {
  25. for _, child := range children {
  26. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  27. routes = append(routes, childRoutes...)
  28. }
  29. })
  30. }
  31. return routes
  32. }
  33. func getRegistryRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/registries/{registry_id}"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryGetHandler
  46. getEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: relPath,
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. types.RegistryScope,
  58. },
  59. },
  60. )
  61. getHandler := registry.NewRegistryGetHandler(
  62. config,
  63. factory.GetResultWriter(),
  64. )
  65. routes = append(routes, &Route{
  66. Endpoint: getEndpoint,
  67. Handler: getHandler,
  68. Router: r,
  69. })
  70. // POST /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryUpdateHandler
  71. updateEndpoint := factory.NewAPIEndpoint(
  72. &types.APIRequestMetadata{
  73. Verb: types.APIVerbUpdate,
  74. Method: types.HTTPVerbPost,
  75. Path: &types.Path{
  76. Parent: basePath,
  77. RelativePath: relPath,
  78. },
  79. Scopes: []types.PermissionScope{
  80. types.UserScope,
  81. types.ProjectScope,
  82. types.RegistryScope,
  83. },
  84. },
  85. )
  86. updateHandler := registry.NewRegistryUpdateHandler(
  87. config,
  88. factory.GetDecoderValidator(),
  89. factory.GetResultWriter(),
  90. )
  91. routes = append(routes, &Route{
  92. Endpoint: updateEndpoint,
  93. Handler: updateHandler,
  94. Router: r,
  95. })
  96. // GET /api/projects/{project_id}/registries/{registry_id}/repositories -> registry.NewRegistryListRepositoriesHandler
  97. listRepositoriesEndpoint := factory.NewAPIEndpoint(
  98. &types.APIRequestMetadata{
  99. Verb: types.APIVerbList,
  100. Method: types.HTTPVerbGet,
  101. Path: &types.Path{
  102. Parent: basePath,
  103. RelativePath: relPath + "/repositories",
  104. },
  105. Scopes: []types.PermissionScope{
  106. types.UserScope,
  107. types.ProjectScope,
  108. types.RegistryScope,
  109. },
  110. },
  111. )
  112. listRepositoriesHandler := registry.NewRegistryListRepositoriesHandler(
  113. config,
  114. factory.GetResultWriter(),
  115. )
  116. routes = append(routes, &Route{
  117. Endpoint: listRepositoriesEndpoint,
  118. Handler: listRepositoriesHandler,
  119. Router: r,
  120. })
  121. // POST /api/projects/{project_id}/registries/{registry_id}/repository -> registry.NewRegistryCreateRepositoryHandler
  122. createRepositoryEndpoint := factory.NewAPIEndpoint(
  123. &types.APIRequestMetadata{
  124. Verb: types.APIVerbCreate,
  125. Method: types.HTTPVerbPost,
  126. Path: &types.Path{
  127. Parent: basePath,
  128. RelativePath: relPath + "/repository",
  129. },
  130. Scopes: []types.PermissionScope{
  131. types.UserScope,
  132. types.ProjectScope,
  133. types.RegistryScope,
  134. },
  135. },
  136. )
  137. createRepositoryHandler := registry.NewRegistryCreateRepositoryHandler(
  138. config,
  139. factory.GetDecoderValidator(),
  140. factory.GetResultWriter(),
  141. )
  142. routes = append(routes, &Route{
  143. Endpoint: createRepositoryEndpoint,
  144. Handler: createRepositoryHandler,
  145. Router: r,
  146. })
  147. return routes, newPath
  148. }