slack_integration.go 3.1 KB

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