namespace.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/namespace"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/types"
  7. )
  8. func NewNamespaceScopedRegisterer(children ...*Registerer) *Registerer {
  9. return &Registerer{
  10. GetRoutes: GetNamespaceScopedRoutes,
  11. Children: children,
  12. }
  13. }
  14. func GetNamespaceScopedRoutes(
  15. r chi.Router,
  16. config *shared.Config,
  17. basePath *types.Path,
  18. factory shared.APIEndpointFactory,
  19. children ...*Registerer,
  20. ) []*Route {
  21. routes, projPath := getNamespaceRoutes(r, config, basePath, factory)
  22. if len(children) > 0 {
  23. r.Route(projPath.RelativePath, func(r chi.Router) {
  24. for _, child := range children {
  25. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  26. routes = append(routes, childRoutes...)
  27. }
  28. })
  29. }
  30. return routes
  31. }
  32. func getNamespaceRoutes(
  33. r chi.Router,
  34. config *shared.Config,
  35. basePath *types.Path,
  36. factory shared.APIEndpointFactory,
  37. ) ([]*Route, *types.Path) {
  38. relPath := "/namespaces/{namespace}"
  39. newPath := &types.Path{
  40. Parent: basePath,
  41. RelativePath: relPath,
  42. }
  43. routes := make([]*Route, 0)
  44. // GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/releases -> namespace.NewListReleasesHandler
  45. getEndpoint := factory.NewAPIEndpoint(
  46. &types.APIRequestMetadata{
  47. Verb: types.APIVerbGet,
  48. Method: types.HTTPVerbGet,
  49. Path: &types.Path{
  50. Parent: basePath,
  51. RelativePath: relPath + "/releases",
  52. },
  53. Scopes: []types.PermissionScope{
  54. types.UserScope,
  55. types.ProjectScope,
  56. types.ClusterScope,
  57. types.NamespaceScope,
  58. },
  59. },
  60. )
  61. getHandler := namespace.NewListReleasesHandler(
  62. config,
  63. factory.GetDecoderValidator(),
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &Route{
  67. Endpoint: getEndpoint,
  68. Handler: getHandler,
  69. Router: r,
  70. })
  71. return routes, newPath
  72. }