2
0

slack_integration.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package router
  2. import (
  3. "github.com/go-chi/chi/v5"
  4. "github.com/porter-dev/porter/api/server/handlers/slack_integration"
  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/server/shared/router"
  8. "github.com/porter-dev/porter/api/types"
  9. )
  10. func NewSlackIntegrationScopedRegisterer(children ...*router.Registerer) *router.Registerer {
  11. return &router.Registerer{
  12. GetRoutes: GetSlackIntegrationScopedRoutes,
  13. Children: children,
  14. }
  15. }
  16. func GetSlackIntegrationScopedRoutes(
  17. r chi.Router,
  18. config *config.Config,
  19. basePath *types.Path,
  20. factory shared.APIEndpointFactory,
  21. children ...*router.Registerer,
  22. ) []*router.Route {
  23. routes, projPath := getSlackIntegrationRoutes(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 getSlackIntegrationRoutes(
  35. r chi.Router,
  36. config *config.Config,
  37. basePath *types.Path,
  38. factory shared.APIEndpointFactory,
  39. ) ([]*router.Route, *types.Path) {
  40. relPath := "/slack_integrations"
  41. newPath := &types.Path{
  42. Parent: basePath,
  43. RelativePath: relPath,
  44. }
  45. routes := make([]*router.Route, 0)
  46. // GET /api/projects/{project_id}/slack_integrations -> slack_integration.NewListHandler
  47. listEndpoint := 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. listHandler := slack_integration.NewSlackIntegrationListHandler(
  62. config,
  63. factory.GetResultWriter(),
  64. )
  65. routes = append(routes, &router.Route{
  66. Endpoint: listEndpoint,
  67. Handler: listHandler,
  68. Router: r,
  69. })
  70. // GET /api/projects/{project_id}/slack_integrations/exists -> slack_integration.NewListHandler
  71. existsEndpoint := factory.NewAPIEndpoint(
  72. &types.APIRequestMetadata{
  73. Verb: types.APIVerbGet,
  74. Method: types.HTTPVerbGet,
  75. Path: &types.Path{
  76. Parent: basePath,
  77. RelativePath: relPath + "/exists",
  78. },
  79. Scopes: []types.PermissionScope{
  80. types.UserScope,
  81. types.ProjectScope,
  82. },
  83. },
  84. )
  85. existsHandler := slack_integration.NewSlackIntegrationExists(config)
  86. routes = append(routes, &router.Route{
  87. Endpoint: existsEndpoint,
  88. Handler: existsHandler,
  89. Router: r,
  90. })
  91. // GET /api/projects/{project_id}/slack_integrations/exists -> slack_integration.NewListHandler
  92. deleteEndpoint := factory.NewAPIEndpoint(
  93. &types.APIRequestMetadata{
  94. Verb: types.APIVerbDelete,
  95. Method: types.HTTPVerbDelete,
  96. Path: &types.Path{
  97. Parent: basePath,
  98. RelativePath: relPath + "/{slack_integration_id}",
  99. },
  100. Scopes: []types.PermissionScope{
  101. types.UserScope,
  102. types.ProjectScope,
  103. },
  104. },
  105. )
  106. deleteHandler := slack_integration.NewSlackIntegrationDelete(config)
  107. routes = append(routes, &router.Route{
  108. Endpoint: deleteEndpoint,
  109. Handler: deleteHandler,
  110. Router: r,
  111. })
  112. return routes, newPath
  113. }