project.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/cluster"
  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 NewProjectScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetProjectScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetProjectScopedRoutes(
  16. r chi.Router,
  17. config *shared.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getProjectRoutes(r, config, basePath, factory)
  23. if len(children) > 0 {
  24. r.Route(projPath.RelativePath, func(r chi.Router) {
  25. for _, child := range children {
  26. childRoutes := child.GetRoutes(r, config, basePath, factory, child.Children...)
  27. routes = append(routes, childRoutes...)
  28. }
  29. })
  30. }
  31. return routes
  32. }
  33. func getProjectRoutes(
  34. r chi.Router,
  35. config *shared.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/projects/{project_id}"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id} -> project.NewProjectGetHandler
  46. getEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: relPath,
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. },
  58. },
  59. )
  60. getHandler := project.NewProjectGetHandler(
  61. config,
  62. factory.GetResultWriter(),
  63. )
  64. routes = append(routes, &Route{
  65. Endpoint: getEndpoint,
  66. Handler: getHandler,
  67. Router: r,
  68. })
  69. // GET /api/projects/{project_id}/policy -> project.NewProjectGetPolicyHandler
  70. getPolicyEndpoint := factory.NewAPIEndpoint(
  71. &types.APIRequestMetadata{
  72. Verb: types.APIVerbGet,
  73. Method: types.HTTPVerbGet,
  74. Path: &types.Path{
  75. Parent: basePath,
  76. RelativePath: relPath + "/policy",
  77. },
  78. Scopes: []types.PermissionScope{
  79. types.UserScope,
  80. types.ProjectScope,
  81. },
  82. },
  83. )
  84. getPolicyHandler := project.NewProjectGetPolicyHandler(
  85. config,
  86. factory.GetResultWriter(),
  87. )
  88. routes = append(routes, &Route{
  89. Endpoint: getPolicyEndpoint,
  90. Handler: getPolicyHandler,
  91. Router: r,
  92. })
  93. // GET /api/projects/{project_id}/infra -> project.NewListProjectInfraHandler
  94. listInfraEndpoint := factory.NewAPIEndpoint(
  95. &types.APIRequestMetadata{
  96. Verb: types.APIVerbGet,
  97. Method: types.HTTPVerbGet,
  98. Path: &types.Path{
  99. Parent: basePath,
  100. RelativePath: relPath + "/infra",
  101. },
  102. Scopes: []types.PermissionScope{
  103. types.UserScope,
  104. types.ProjectScope,
  105. },
  106. },
  107. )
  108. listInfraHandler := project.NewProjectListInfraHandler(
  109. config,
  110. factory.GetResultWriter(),
  111. )
  112. routes = append(routes, &Route{
  113. Endpoint: listInfraEndpoint,
  114. Handler: listInfraHandler,
  115. Router: r,
  116. })
  117. // GET /api/projects/{project_id}/clusters -> cluster.NewClusterListHandler
  118. listClusterEndpoint := factory.NewAPIEndpoint(
  119. &types.APIRequestMetadata{
  120. Verb: types.APIVerbList,
  121. Method: types.HTTPVerbGet,
  122. Path: &types.Path{
  123. Parent: basePath,
  124. RelativePath: relPath + "/clusters",
  125. },
  126. Scopes: []types.PermissionScope{
  127. types.UserScope,
  128. types.ProjectScope,
  129. },
  130. },
  131. )
  132. listClusterHandler := cluster.NewClusterListHandler(
  133. config,
  134. factory.GetResultWriter(),
  135. )
  136. routes = append(routes, &Route{
  137. Endpoint: listClusterEndpoint,
  138. Handler: listClusterHandler,
  139. Router: r,
  140. })
  141. return routes, newPath
  142. }