release.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/release"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/types"
  7. )
  8. func NewReleaseScopedRegisterer(children ...*Registerer) *Registerer {
  9. return &Registerer{
  10. GetRoutes: GetReleaseScopedRoutes,
  11. Children: children,
  12. }
  13. }
  14. func GetReleaseScopedRoutes(
  15. r chi.Router,
  16. config *shared.Config,
  17. basePath *types.Path,
  18. factory shared.APIEndpointFactory,
  19. children ...*Registerer,
  20. ) []*Route {
  21. routes, projPath := getReleaseRoutes(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 getReleaseRoutes(
  33. r chi.Router,
  34. config *shared.Config,
  35. basePath *types.Path,
  36. factory shared.APIEndpointFactory,
  37. ) ([]*Route, *types.Path) {
  38. relPath := "/releases/{name}/{version}"
  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/{name}/{version} -> release.NewReleaseGetHandler
  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,
  52. },
  53. Scopes: []types.PermissionScope{
  54. types.UserScope,
  55. types.ProjectScope,
  56. types.ClusterScope,
  57. types.NamespaceScope,
  58. types.ReleaseScope,
  59. },
  60. },
  61. )
  62. getHandler := release.NewReleaseGetHandler(
  63. config,
  64. factory.GetResultWriter(),
  65. )
  66. routes = append(routes, &Route{
  67. Endpoint: getEndpoint,
  68. Handler: getHandler,
  69. Router: r,
  70. })
  71. return routes, newPath
  72. }