Explorar o código

move release scoped router within namespace scoped router

Anukul Sangwan %!s(int64=4) %!d(string=hai) anos
pai
achega
2f2b93e825
Modificáronse 3 ficheiros con 59 adicións e 3 borrados
  1. 54 0
      api/server/router/namespace.go
  2. 3 2
      api/server/router/release.go
  3. 2 1
      api/server/router/router.go

+ 54 - 0
api/server/router/namespace.go

@@ -0,0 +1,54 @@
+package router
+
+import (
+	"github.com/go-chi/chi"
+	"github.com/porter-dev/porter/api/server/shared"
+	"github.com/porter-dev/porter/api/types"
+)
+
+func NewNamespaceScopedRegisterer(children ...*Registerer) *Registerer {
+	return &Registerer{
+		GetRoutes: GetNamespaceScopedRoutes,
+		Children:  children,
+	}
+}
+
+func GetNamespaceScopedRoutes(
+	r chi.Router,
+	config *shared.Config,
+	basePath *types.Path,
+	factory shared.APIEndpointFactory,
+	children ...*Registerer,
+) []*Route {
+	routes, projPath := getNamespaceRoutes(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 getNamespaceRoutes(
+	r chi.Router,
+	config *shared.Config,
+	basePath *types.Path,
+	factory shared.APIEndpointFactory,
+) ([]*Route, *types.Path) {
+	relPath := "/namespaces/{namespace}"
+
+	newPath := &types.Path{
+		Parent:       basePath,
+		RelativePath: relPath,
+	}
+
+	routes := make([]*Route, 0)
+
+	return routes, newPath
+}

+ 3 - 2
api/server/router/release.go

@@ -42,7 +42,7 @@ func getReleaseRoutes(
 	basePath *types.Path,
 	factory shared.APIEndpointFactory,
 ) ([]*Route, *types.Path) {
-	relPath := "/releases/{namespace}/{name}/{version}"
+	relPath := "/releases/{name}/{version}"
 
 	newPath := &types.Path{
 		Parent:       basePath,
@@ -51,7 +51,7 @@ func getReleaseRoutes(
 
 	routes := make([]*Route, 0)
 
-	// GET /api/projects/{project_id}/clusters/{cluster_id}/releases/{namespace}/{name}/{version} -> release.NewReleaseGetHandler
+	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases/{name}/{version} -> release.NewReleaseGetHandler
 	getEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
 			Verb:   types.APIVerbGet,
@@ -64,6 +64,7 @@ func getReleaseRoutes(
 				types.UserScope,
 				types.ProjectScope,
 				types.ClusterScope,
+				types.NamespaceScope,
 				types.ReleaseScope,
 			},
 		},

+ 2 - 1
api/server/router/router.go

@@ -21,7 +21,8 @@ func NewAPIRouter(config *shared.Config) *chi.Mux {
 	baseRegisterer := NewBaseRegisterer()
 
 	releaseRegisterer := NewReleaseScopedRegisterer()
-	clusterRegisterer := NewClusterScopedRegisterer(releaseRegisterer)
+	namespaceRegisterer := NewNamespaceScopedRegisterer(releaseRegisterer)
+	clusterRegisterer := NewClusterScopedRegisterer(namespaceRegisterer)
 	projRegisterer := NewProjectScopedRegisterer(clusterRegisterer)
 	userRegisterer := NewUserScopedRegisterer(projRegisterer)