project.go 4.3 KB

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