namespace.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/shared"
  5. "github.com/porter-dev/porter/api/types"
  6. )
  7. func NewNamespaceScopedRegisterer(children ...*Registerer) *Registerer {
  8. return &Registerer{
  9. GetRoutes: GetNamespaceScopedRoutes,
  10. Children: children,
  11. }
  12. }
  13. func GetNamespaceScopedRoutes(
  14. r chi.Router,
  15. config *shared.Config,
  16. basePath *types.Path,
  17. factory shared.APIEndpointFactory,
  18. children ...*Registerer,
  19. ) []*Route {
  20. routes, projPath := getNamespaceRoutes(r, config, basePath, factory)
  21. if len(children) > 0 {
  22. r.Route(projPath.RelativePath, func(r chi.Router) {
  23. for _, child := range children {
  24. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  25. routes = append(routes, childRoutes...)
  26. }
  27. })
  28. }
  29. return routes
  30. }
  31. func getNamespaceRoutes(
  32. r chi.Router,
  33. config *shared.Config,
  34. basePath *types.Path,
  35. factory shared.APIEndpointFactory,
  36. ) ([]*Route, *types.Path) {
  37. relPath := "/namespaces/{namespace}"
  38. newPath := &types.Path{
  39. Parent: basePath,
  40. RelativePath: relPath,
  41. }
  42. routes := make([]*Route, 0)
  43. return routes, newPath
  44. }