project.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/authz"
  5. "github.com/porter-dev/porter/api/server/handlers/project"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. func RegisterProjectScopedRoutes(
  10. r chi.Router,
  11. config *shared.Config,
  12. basePath *types.Path,
  13. factory shared.APIEndpointFactory,
  14. ) chi.Router {
  15. // Create a new "project-scoped" factory which will create a new project-scoped request
  16. // after authorization. Each subsequent http.Handler can lookup the project in context.
  17. projFactory := authz.NewProjectScopedFactory(config.Repo.Project(), config)
  18. // attach middleware to router
  19. r.Use(projFactory.NewProjectScoped)
  20. registerProjectEndpoints(r, config, basePath, factory)
  21. return r
  22. }
  23. func registerProjectEndpoints(
  24. r chi.Router,
  25. config *shared.Config,
  26. basePath *types.Path,
  27. factory shared.APIEndpointFactory,
  28. ) {
  29. routes := make([]*Route, 0)
  30. projectPath := &types.Path{
  31. Parent: basePath,
  32. RelativePath: "/projects",
  33. }
  34. createEndpoint := factory.NewAPIEndpoint(
  35. &types.APIRequestMetadata{
  36. Verb: types.APIVerbCreate,
  37. Method: types.HTTPVerbPost,
  38. Path: &types.Path{
  39. Parent: projectPath,
  40. RelativePath: "",
  41. },
  42. },
  43. )
  44. createHandler := project.NewProjectCreateHandler(config, createEndpoint)
  45. routes = append(routes, &Route{
  46. Endpoint: createEndpoint,
  47. Handler: createHandler,
  48. })
  49. registerRoutes(r, routes)
  50. }