| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package router
- import (
- "fmt"
- "github.com/go-chi/chi"
- "github.com/porter-dev/porter/api/server/handlers/registry"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/server/shared/router"
- "github.com/porter-dev/porter/api/types"
- )
- func NewRegistryScopedRegisterer(children ...*router.Registerer) *router.Registerer {
- return &router.Registerer{
- GetRoutes: GetRegistryScopedRoutes,
- Children: children,
- }
- }
- func GetRegistryScopedRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- children ...*router.Registerer,
- ) []*router.Route {
- routes, projPath := getRegistryRoutes(r, config, basePath, factory)
- if len(children) > 0 {
- r.Route(projPath.RelativePath, func(r chi.Router) {
- for _, child := range children {
- childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
- routes = append(routes, childRoutes...)
- }
- })
- }
- return routes
- }
- func getRegistryRoutes(
- r chi.Router,
- config *config.Config,
- basePath *types.Path,
- factory shared.APIEndpointFactory,
- ) ([]*router.Route, *types.Path) {
- relPath := "/registries/{registry_id}"
- newPath := &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- }
- routes := make([]*router.Route, 0)
- // GET /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryGetHandler
- getEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbGet,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- getHandler := registry.NewRegistryGetHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: getEndpoint,
- Handler: getHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryUpdateHandler
- updateEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbUpdate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- updateHandler := registry.NewRegistryUpdateHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: updateEndpoint,
- Handler: updateHandler,
- Router: r,
- })
- // DELETE /api/projects/{project_id}/registries/{registry_id} -> registry.NewRegistryDeleteHandler
- deleteEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbDelete,
- Method: types.HTTPVerbDelete,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath,
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- deleteHandler := registry.NewRegistryDeleteHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: deleteEndpoint,
- Handler: deleteHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/registries/{registry_id}/repositories -> registry.NewRegistryListRepositoriesHandler
- listRepositoriesEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbList,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/repositories",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- listRepositoriesHandler := registry.NewRegistryListRepositoriesHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listRepositoriesEndpoint,
- Handler: listRepositoriesHandler,
- Router: r,
- })
- // GET /api/projects/{project_id}/registries/{registry_id}/repositories/* -> registry.NewRegistryListImagesHandler
- listImagesEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbList,
- Method: types.HTTPVerbGet,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: fmt.Sprintf(
- "%s/repositories/%s",
- relPath,
- types.URLParamWildcard,
- ),
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- listImagesHandler := registry.NewRegistryListImagesHandler(
- config,
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: listImagesEndpoint,
- Handler: listImagesHandler,
- Router: r,
- })
- // POST /api/projects/{project_id}/registries/{registry_id}/repository -> registry.NewRegistryCreateRepositoryHandler
- createRepositoryEndpoint := factory.NewAPIEndpoint(
- &types.APIRequestMetadata{
- Verb: types.APIVerbCreate,
- Method: types.HTTPVerbPost,
- Path: &types.Path{
- Parent: basePath,
- RelativePath: relPath + "/repository",
- },
- Scopes: []types.PermissionScope{
- types.UserScope,
- types.ProjectScope,
- types.RegistryScope,
- },
- },
- )
- createRepositoryHandler := registry.NewRegistryCreateRepositoryHandler(
- config,
- factory.GetDecoderValidator(),
- factory.GetResultWriter(),
- )
- routes = append(routes, &router.Route{
- Endpoint: createRepositoryEndpoint,
- Handler: createRepositoryHandler,
- Router: r,
- })
- return routes, newPath
- }
|