infra.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package router
  2. import (
  3. "github.com/go-chi/chi"
  4. "github.com/porter-dev/porter/api/server/handlers/infra"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/config"
  7. "github.com/porter-dev/porter/api/types"
  8. )
  9. func NewInfraScopedRegisterer(children ...*Registerer) *Registerer {
  10. return &Registerer{
  11. GetRoutes: GetInfraScopedRoutes,
  12. Children: children,
  13. }
  14. }
  15. func GetInfraScopedRoutes(
  16. r chi.Router,
  17. config *config.Config,
  18. basePath *types.Path,
  19. factory shared.APIEndpointFactory,
  20. children ...*Registerer,
  21. ) []*Route {
  22. routes, projPath := getInfraRoutes(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 getInfraRoutes(
  34. r chi.Router,
  35. config *config.Config,
  36. basePath *types.Path,
  37. factory shared.APIEndpointFactory,
  38. ) ([]*Route, *types.Path) {
  39. relPath := "/infras/{infra_id}"
  40. newPath := &types.Path{
  41. Parent: basePath,
  42. RelativePath: relPath,
  43. }
  44. routes := make([]*Route, 0)
  45. // GET /api/projects/{project_id}/infra -> project.NewInfraListHandler
  46. listInfraEndpoint := factory.NewAPIEndpoint(
  47. &types.APIRequestMetadata{
  48. Verb: types.APIVerbGet,
  49. Method: types.HTTPVerbGet,
  50. Path: &types.Path{
  51. Parent: basePath,
  52. RelativePath: "/infra",
  53. },
  54. Scopes: []types.PermissionScope{
  55. types.UserScope,
  56. types.ProjectScope,
  57. },
  58. },
  59. )
  60. listInfraHandler := infra.NewInfraListHandler(
  61. config,
  62. factory.GetResultWriter(),
  63. )
  64. routes = append(routes, &Route{
  65. Endpoint: listInfraEndpoint,
  66. Handler: listInfraHandler,
  67. Router: r,
  68. })
  69. // GET /api/projects/{project_id}/infras/{infra_id} -> infra.NewInfraGetHandler
  70. getEndpoint := factory.NewAPIEndpoint(
  71. &types.APIRequestMetadata{
  72. Verb: types.APIVerbGet,
  73. Method: types.HTTPVerbGet,
  74. Path: &types.Path{
  75. Parent: basePath,
  76. RelativePath: relPath,
  77. },
  78. Scopes: []types.PermissionScope{
  79. types.UserScope,
  80. types.ProjectScope,
  81. types.InfraScope,
  82. },
  83. },
  84. )
  85. getHandler := infra.NewInfraGetHandler(
  86. config,
  87. factory.GetResultWriter(),
  88. )
  89. routes = append(routes, &Route{
  90. Endpoint: getEndpoint,
  91. Handler: getHandler,
  92. Router: r,
  93. })
  94. // GET /api/projects/{project_id}/infras/{infra_id}/logs -> infra.NewInfraStreamLogsHandler
  95. streamLogsEndpoint := factory.NewAPIEndpoint(
  96. &types.APIRequestMetadata{
  97. Verb: types.APIVerbGet,
  98. Method: types.HTTPVerbGet,
  99. Path: &types.Path{
  100. Parent: basePath,
  101. RelativePath: relPath + "/logs",
  102. },
  103. Scopes: []types.PermissionScope{
  104. types.UserScope,
  105. types.ProjectScope,
  106. types.InfraScope,
  107. },
  108. IsWebsocket: true,
  109. },
  110. )
  111. streamLogsHandler := infra.NewInfraStreamLogsHandler(
  112. config,
  113. factory.GetResultWriter(),
  114. )
  115. routes = append(routes, &Route{
  116. Endpoint: streamLogsEndpoint,
  117. Handler: streamLogsHandler,
  118. Router: r,
  119. })
  120. // GET /api/projects/{project_id}/infras/{infra_id}/current -> infra.NewInfraGetHandler
  121. getCurrentEndpoint := factory.NewAPIEndpoint(
  122. &types.APIRequestMetadata{
  123. Verb: types.APIVerbGet,
  124. Method: types.HTTPVerbGet,
  125. Path: &types.Path{
  126. Parent: basePath,
  127. RelativePath: relPath + "/current",
  128. },
  129. Scopes: []types.PermissionScope{
  130. types.UserScope,
  131. types.ProjectScope,
  132. types.InfraScope,
  133. },
  134. },
  135. )
  136. getCurrentHandler := infra.NewInfraGetCurrentHandler(
  137. config,
  138. factory.GetResultWriter(),
  139. )
  140. routes = append(routes, &Route{
  141. Endpoint: getCurrentEndpoint,
  142. Handler: getCurrentHandler,
  143. Router: r,
  144. })
  145. // GET /api/projects/{project_id}/infras/{infra_id}/desired -> infra.NewInfraGetHandler
  146. getDesiredEndpoint := factory.NewAPIEndpoint(
  147. &types.APIRequestMetadata{
  148. Verb: types.APIVerbGet,
  149. Method: types.HTTPVerbGet,
  150. Path: &types.Path{
  151. Parent: basePath,
  152. RelativePath: relPath + "/desired",
  153. },
  154. Scopes: []types.PermissionScope{
  155. types.UserScope,
  156. types.ProjectScope,
  157. types.InfraScope,
  158. },
  159. },
  160. )
  161. getDesiredHandler := infra.NewInfraGetDesiredHandler(
  162. config,
  163. factory.GetResultWriter(),
  164. )
  165. routes = append(routes, &Route{
  166. Endpoint: getDesiredEndpoint,
  167. Handler: getDesiredHandler,
  168. Router: r,
  169. })
  170. // DELETE /api/projects/{project_id}/infras/{infra_id} -> infra.NewInfraDeleteHandler
  171. deleteEndpoint := factory.NewAPIEndpoint(
  172. &types.APIRequestMetadata{
  173. Verb: types.APIVerbDelete,
  174. Method: types.HTTPVerbDelete,
  175. Path: &types.Path{
  176. Parent: basePath,
  177. RelativePath: relPath,
  178. },
  179. Scopes: []types.PermissionScope{
  180. types.UserScope,
  181. types.ProjectScope,
  182. types.InfraScope,
  183. },
  184. },
  185. )
  186. deleteHandler := infra.NewInfraDeleteHandler(
  187. config,
  188. factory.GetDecoderValidator(),
  189. factory.GetResultWriter(),
  190. )
  191. routes = append(routes, &Route{
  192. Endpoint: deleteEndpoint,
  193. Handler: deleteHandler,
  194. Router: r,
  195. })
  196. return routes, newPath
  197. }