project.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/project"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/types"
  7. )
  8. func NewProjectScopedRegisterer(children ...*Registerer) *Registerer {
  9. return &Registerer{
  10. GetRoutes: GetProjectScopedRoutes,
  11. Children: children,
  12. }
  13. }
  14. func GetProjectScopedRoutes(
  15. r chi.Router,
  16. config *shared.Config,
  17. basePath *types.Path,
  18. factory shared.APIEndpointFactory,
  19. children ...*Registerer,
  20. ) []*Route {
  21. routes, projPath := getProjectRoutes(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. // // Note that we can place the middleware r.Use below
  31. // // all project-scoped routes
  32. // // Create a new "project-scoped" factory which will create a new project-scoped request
  33. // // after authorization. Each subsequent http.Handler can lookup the project in context.
  34. // projFactory := authz.NewProjectScopedFactory(config)
  35. // // attach middleware to router
  36. // r.Use(projFactory.Middleware)
  37. return routes
  38. }
  39. func getProjectRoutes(
  40. r chi.Router,
  41. config *shared.Config,
  42. basePath *types.Path,
  43. factory shared.APIEndpointFactory,
  44. ) ([]*Route, *types.Path) {
  45. relPath := "/projects/{project_id}"
  46. newPath := &types.Path{
  47. Parent: basePath,
  48. RelativePath: relPath,
  49. }
  50. routes := make([]*Route, 0)
  51. // POST /api/projects -> project.NewProjectCreateHandler
  52. getEndpoint := factory.NewAPIEndpoint(
  53. &types.APIRequestMetadata{
  54. Verb: types.APIVerbCreate,
  55. Method: types.HTTPVerbGet,
  56. Path: &types.Path{
  57. Parent: basePath,
  58. RelativePath: relPath,
  59. },
  60. Scopes: []types.PermissionScope{
  61. types.UserScope,
  62. types.ProjectScope,
  63. },
  64. },
  65. )
  66. createHandler := project.NewProjectGetHandler(
  67. config,
  68. factory.GetResultWriter(),
  69. )
  70. routes = append(routes, &Route{
  71. Endpoint: getEndpoint,
  72. Handler: createHandler,
  73. Router: r,
  74. })
  75. return routes, newPath
  76. }