datastore.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package router
  2. import (
  3. "github.com/go-chi/chi/v5"
  4. "github.com/porter-dev/porter/api/server/shared"
  5. "github.com/porter-dev/porter/api/server/shared/config"
  6. "github.com/porter-dev/porter/api/server/shared/router"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. // NewDatastoreScopedRegisterer returns a scoped route registerer for Datastore routes
  10. func NewDatastoreScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  11. return &router.Registerer{
  12. GetRoutes: GetDatastoreScopedRoutes,
  13. Children: children,
  14. }
  15. }
  16. // GetDatastoreScopedRoutes returns scoped Datastore routes with mounted child registerers
  17. func GetDatastoreScopedRoutes(
  18. r chi.Router,
  19. config *config.Config,
  20. basePath *types.Path,
  21. factory shared.APIEndpointFactory,
  22. children ...*router.Registerer,
  23. ) []*router.Route {
  24. routes, projPath := getDatastoreRoutes(r, config, basePath, factory)
  25. if len(children) > 0 {
  26. r.Route(projPath.RelativePath, func(r chi.Router) {
  27. for _, child := range children {
  28. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  29. routes = append(routes, childRoutes...)
  30. }
  31. })
  32. }
  33. return routes
  34. }
  35. // getDatastoreRoutes returns Datastore routes
  36. func getDatastoreRoutes(
  37. r chi.Router,
  38. config *config.Config,
  39. basePath *types.Path,
  40. factory shared.APIEndpointFactory,
  41. ) ([]*router.Route, *types.Path) {
  42. // empty path as this is mounted onto the datastore endpoints
  43. relPath := ""
  44. newPath := &types.Path{
  45. Parent: basePath,
  46. RelativePath: relPath,
  47. }
  48. routes := make([]*router.Route, 0)
  49. return routes, newPath
  50. }