project.go 3.6 KB

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