Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: cors handlers to echo server (#16575)

p0ny6a01f4c9df76
diff --git a/backend/server/grpc_routes.go b/backend/server/grpc_routes.go
index 215c000d74..5e6191fc79 100644
--- a/backend/server/grpc_routes.go
+++ b/backend/server/grpc_routes.go
@@ -10,11 +10,9 @@ import (
"google.golang.org/grpc/credentials/insecure"
"connectrpc.com/connect"
- connectcors "connectrpc.com/cors"
"connectrpc.com/grpcreflect"
grpcruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/pkg/errors"
- "github.com/rs/cors"
"github.com/bytebase/bytebase/backend/api/auth"
apiv1 "github.com/bytebase/bytebase/backend/api/v1"
@@ -34,20 +32,6 @@ import (
"github.com/bytebase/bytebase/proto/generated-go/v1/v1connect"
)
-// withCORS adds CORS support to a Connect HTTP handler following Connect RPC documentation.
-func withCORS(h http.Handler) http.Handler {
- middleware := cors.New(cors.Options{
- AllowOriginFunc: func(string) bool {
- return true
- },
- AllowedMethods: connectcors.AllowedMethods(),
- AllowedHeaders: connectcors.AllowedHeaders(),
- ExposedHeaders: connectcors.ExposedHeaders(),
- AllowCredentials: true,
- })
- return middleware.Handler(h)
-}
-
func configureGrpcRouters(
ctx context.Context,
mux *grpcruntime.ServeMux,
@@ -113,91 +97,91 @@ func configureGrpcRouters(
connectHandlers := make(map[string]http.Handler)
actuatorPath, actuatorHandler := v1connect.NewActuatorServiceHandler(actuatorService, handlerOpts)
- connectHandlers[actuatorPath] = withCORS(actuatorHandler)
+ connectHandlers[actuatorPath] = actuatorHandler
auditLogPath, auditLogHandler := v1connect.NewAuditLogServiceHandler(auditLogService, handlerOpts)
- connectHandlers[auditLogPath] = withCORS(auditLogHandler)
+ connectHandlers[auditLogPath] = auditLogHandler
authPath, authHandler := v1connect.NewAuthServiceHandler(authService, handlerOpts)
- connectHandlers[authPath] = withCORS(authHandler)
+ connectHandlers[authPath] = authHandler
celPath, celHandler := v1connect.NewCelServiceHandler(celService, handlerOpts)
- connectHandlers[celPath] = withCORS(celHandler)
+ connectHandlers[celPath] = celHandler
changelistPath, changelistHandler := v1connect.NewChangelistServiceHandler(changelistService, handlerOpts)
- connectHandlers[changelistPath] = withCORS(changelistHandler)
+ connectHandlers[changelistPath] = changelistHandler
databaseCatalogPath, databaseCatalogHandler := v1connect.NewDatabaseCatalogServiceHandler(databaseCatalogService, handlerOpts)
- connectHandlers[databaseCatalogPath] = withCORS(databaseCatalogHandler)
+ connectHandlers[databaseCatalogPath] = databaseCatalogHandler
databaseGroupPath, databaseGroupHandler := v1connect.NewDatabaseGroupServiceHandler(databaseGroupService, handlerOpts)
- connectHandlers[databaseGroupPath] = withCORS(databaseGroupHandler)
+ connectHandlers[databaseGroupPath] = databaseGroupHandler
databasePath, databaseHandler := v1connect.NewDatabaseServiceHandler(databaseService, handlerOpts)
- connectHandlers[databasePath] = withCORS(databaseHandler)
+ connectHandlers[databasePath] = databaseHandler
groupPath, groupHandler := v1connect.NewGroupServiceHandler(groupService, handlerOpts)
- connectHandlers[groupPath] = withCORS(groupHandler)
+ connectHandlers[groupPath] = groupHandler
identityProviderPath, identityProviderHandler := v1connect.NewIdentityProviderServiceHandler(identityProviderService, handlerOpts)
- connectHandlers[identityProviderPath] = withCORS(identityProviderHandler)
+ connectHandlers[identityProviderPath] = identityProviderHandler
instanceRolePath, instanceRoleHandler := v1connect.NewInstanceRoleServiceHandler(instanceRoleService, handlerOpts)
- connectHandlers[instanceRolePath] = withCORS(instanceRoleHandler)
+ connectHandlers[instanceRolePath] = instanceRoleHandler
instancePath, instanceHandler := v1connect.NewInstanceServiceHandler(instanceService, handlerOpts)
- connectHandlers[instancePath] = withCORS(instanceHandler)
+ connectHandlers[instancePath] = instanceHandler
issuePath, issueHandler := v1connect.NewIssueServiceHandler(issueService, handlerOpts)
- connectHandlers[issuePath] = withCORS(issueHandler)
+ connectHandlers[issuePath] = issueHandler
orgPolicyPath, orgPolicyHandler := v1connect.NewOrgPolicyServiceHandler(orgPolicyService, handlerOpts)
- connectHandlers[orgPolicyPath] = withCORS(orgPolicyHandler)
+ connectHandlers[orgPolicyPath] = orgPolicyHandler
planPath, planHandler := v1connect.NewPlanServiceHandler(planService, handlerOpts)
- connectHandlers[planPath] = withCORS(planHandler)
+ connectHandlers[planPath] = planHandler
projectPath, projectHandler := v1connect.NewProjectServiceHandler(projectService, handlerOpts)
- connectHandlers[projectPath] = withCORS(projectHandler)
+ connectHandlers[projectPath] = projectHandler
releasePath, releaseHandler := v1connect.NewReleaseServiceHandler(releaseService, handlerOpts)
- connectHandlers[releasePath] = withCORS(releaseHandler)
+ connectHandlers[releasePath] = releaseHandler
reviewConfigPath, reviewConfigHandler := v1connect.NewReviewConfigServiceHandler(reviewConfigService, handlerOpts)
- connectHandlers[reviewConfigPath] = withCORS(reviewConfigHandler)
+ connectHandlers[reviewConfigPath] = reviewConfigHandler
revisionPath, revisionHandler := v1connect.NewRevisionServiceHandler(revisionService, handlerOpts)
- connectHandlers[revisionPath] = withCORS(revisionHandler)
+ connectHandlers[revisionPath] = revisionHandler
riskPath, riskHandler := v1connect.NewRiskServiceHandler(riskService, handlerOpts)
- connectHandlers[riskPath] = withCORS(riskHandler)
+ connectHandlers[riskPath] = riskHandler
rolePath, roleHandler := v1connect.NewRoleServiceHandler(roleService, handlerOpts)
- connectHandlers[rolePath] = withCORS(roleHandler)
+ connectHandlers[rolePath] = roleHandler
rolloutPath, rolloutHandler := v1connect.NewRolloutServiceHandler(rolloutService, handlerOpts)
- connectHandlers[rolloutPath] = withCORS(rolloutHandler)
+ connectHandlers[rolloutPath] = rolloutHandler
settingPath, settingHandler := v1connect.NewSettingServiceHandler(settingService, handlerOpts)
- connectHandlers[settingPath] = withCORS(settingHandler)
+ connectHandlers[settingPath] = settingHandler
sheetPath, sheetHandler := v1connect.NewSheetServiceHandler(sheetService, handlerOpts)
- connectHandlers[sheetPath] = withCORS(sheetHandler)
+ connectHandlers[sheetPath] = sheetHandler
sqlPath, sqlHandler := v1connect.NewSQLServiceHandler(sqlService, handlerOpts)
- connectHandlers[sqlPath] = withCORS(sqlHandler)
+ connectHandlers[sqlPath] = sqlHandler
subscriptionPath, subscriptionHandler := v1connect.NewSubscriptionServiceHandler(subscriptionService, handlerOpts)
- connectHandlers[subscriptionPath] = withCORS(subscriptionHandler)
+ connectHandlers[subscriptionPath] = subscriptionHandler
userPath, userHandler := v1connect.NewUserServiceHandler(userService, handlerOpts)
- connectHandlers[userPath] = withCORS(userHandler)
+ connectHandlers[userPath] = userHandler
worksheetPath, worksheetHandler := v1connect.NewWorksheetServiceHandler(worksheetService, handlerOpts)
- connectHandlers[worksheetPath] = withCORS(worksheetHandler)
+ connectHandlers[worksheetPath] = worksheetHandler
workspacePath, workspaceHandler := v1connect.NewWorkspaceServiceHandler(workspaceService, handlerOpts)
- connectHandlers[workspacePath] = withCORS(workspaceHandler)
+ connectHandlers[workspacePath] = workspaceHandler
// grpc reflection handlers.
reflector := grpcreflect.NewStaticReflector(
diff --git a/backend/server/routers.go b/backend/server/routers.go
index 046b20e55c..7572dcb2f8 100644
--- a/backend/server/routers.go
+++ b/backend/server/routers.go
@@ -11,6 +11,8 @@ import (
"github.com/pkg/errors"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
+ connectcors "connectrpc.com/cors"
+
directorysync "github.com/bytebase/bytebase/backend/api/directory-sync"
"github.com/bytebase/bytebase/backend/api/lsp"
"github.com/bytebase/bytebase/backend/common/log"
@@ -27,6 +29,16 @@ func configureEchoRouters(
) {
e.Use(recoverMiddleware)
+ e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
+ AllowOriginFunc: func(string) (bool, error) {
+ return true, nil
+ },
+ AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodOptions},
+ AllowHeaders: connectcors.AllowedHeaders(),
+ ExposeHeaders: connectcors.ExposedHeaders(),
+ AllowCredentials: true,
+ }))
+
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogMethod: true,
diff --git a/go.mod b/go.mod
index 249127c9dd..fdd1b60354 100644
--- a/go.mod
+++ b/go.mod
@@ -348,7 +348,6 @@ require (
github.com/prometheus/common v0.63.0 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
- github.com/rs/cors v1.7.0
github.com/segmentio/asm v1.2.0 // indirect
github.com/segmentio/backo-go v1.0.1 // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
diff --git a/go.sum b/go.sum
index 35264ca5eb..da381e3448 100644
--- a/go.sum
+++ b/go.sum
@@ -1714,8 +1714,6 @@ github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
-github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
-github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=