Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: cors handlers to echo server (#16575)
p0ny6a01f4c9df76
diff --git a/backend/server/grpc_routes.go b/backend/server/grpc_routes.goindex 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] = actuatorHandlerauditLogPath, auditLogHandler := v1connect.NewAuditLogServiceHandler(auditLogService, handlerOpts)- connectHandlers[auditLogPath] = withCORS(auditLogHandler)+ connectHandlers[auditLogPath] = auditLogHandlerauthPath, authHandler := v1connect.NewAuthServiceHandler(authService, handlerOpts)- connectHandlers[authPath] = withCORS(authHandler)+ connectHandlers[authPath] = authHandlercelPath, celHandler := v1connect.NewCelServiceHandler(celService, handlerOpts)- connectHandlers[celPath] = withCORS(celHandler)+ connectHandlers[celPath] = celHandlerchangelistPath, changelistHandler := v1connect.NewChangelistServiceHandler(changelistService, handlerOpts)- connectHandlers[changelistPath] = withCORS(changelistHandler)+ connectHandlers[changelistPath] = changelistHandlerdatabaseCatalogPath, databaseCatalogHandler := v1connect.NewDatabaseCatalogServiceHandler(databaseCatalogService, handlerOpts)- connectHandlers[databaseCatalogPath] = withCORS(databaseCatalogHandler)+ connectHandlers[databaseCatalogPath] = databaseCatalogHandlerdatabaseGroupPath, databaseGroupHandler := v1connect.NewDatabaseGroupServiceHandler(databaseGroupService, handlerOpts)- connectHandlers[databaseGroupPath] = withCORS(databaseGroupHandler)+ connectHandlers[databaseGroupPath] = databaseGroupHandlerdatabasePath, databaseHandler := v1connect.NewDatabaseServiceHandler(databaseService, handlerOpts)- connectHandlers[databasePath] = withCORS(databaseHandler)+ connectHandlers[databasePath] = databaseHandlergroupPath, groupHandler := v1connect.NewGroupServiceHandler(groupService, handlerOpts)- connectHandlers[groupPath] = withCORS(groupHandler)+ connectHandlers[groupPath] = groupHandleridentityProviderPath, identityProviderHandler := v1connect.NewIdentityProviderServiceHandler(identityProviderService, handlerOpts)- connectHandlers[identityProviderPath] = withCORS(identityProviderHandler)+ connectHandlers[identityProviderPath] = identityProviderHandlerinstanceRolePath, instanceRoleHandler := v1connect.NewInstanceRoleServiceHandler(instanceRoleService, handlerOpts)- connectHandlers[instanceRolePath] = withCORS(instanceRoleHandler)+ connectHandlers[instanceRolePath] = instanceRoleHandlerinstancePath, instanceHandler := v1connect.NewInstanceServiceHandler(instanceService, handlerOpts)- connectHandlers[instancePath] = withCORS(instanceHandler)+ connectHandlers[instancePath] = instanceHandlerissuePath, issueHandler := v1connect.NewIssueServiceHandler(issueService, handlerOpts)- connectHandlers[issuePath] = withCORS(issueHandler)+ connectHandlers[issuePath] = issueHandlerorgPolicyPath, orgPolicyHandler := v1connect.NewOrgPolicyServiceHandler(orgPolicyService, handlerOpts)- connectHandlers[orgPolicyPath] = withCORS(orgPolicyHandler)+ connectHandlers[orgPolicyPath] = orgPolicyHandlerplanPath, planHandler := v1connect.NewPlanServiceHandler(planService, handlerOpts)- connectHandlers[planPath] = withCORS(planHandler)+ connectHandlers[planPath] = planHandlerprojectPath, projectHandler := v1connect.NewProjectServiceHandler(projectService, handlerOpts)- connectHandlers[projectPath] = withCORS(projectHandler)+ connectHandlers[projectPath] = projectHandlerreleasePath, releaseHandler := v1connect.NewReleaseServiceHandler(releaseService, handlerOpts)- connectHandlers[releasePath] = withCORS(releaseHandler)+ connectHandlers[releasePath] = releaseHandlerreviewConfigPath, reviewConfigHandler := v1connect.NewReviewConfigServiceHandler(reviewConfigService, handlerOpts)- connectHandlers[reviewConfigPath] = withCORS(reviewConfigHandler)+ connectHandlers[reviewConfigPath] = reviewConfigHandlerrevisionPath, revisionHandler := v1connect.NewRevisionServiceHandler(revisionService, handlerOpts)- connectHandlers[revisionPath] = withCORS(revisionHandler)+ connectHandlers[revisionPath] = revisionHandlerriskPath, riskHandler := v1connect.NewRiskServiceHandler(riskService, handlerOpts)- connectHandlers[riskPath] = withCORS(riskHandler)+ connectHandlers[riskPath] = riskHandlerrolePath, roleHandler := v1connect.NewRoleServiceHandler(roleService, handlerOpts)- connectHandlers[rolePath] = withCORS(roleHandler)+ connectHandlers[rolePath] = roleHandlerrolloutPath, rolloutHandler := v1connect.NewRolloutServiceHandler(rolloutService, handlerOpts)- connectHandlers[rolloutPath] = withCORS(rolloutHandler)+ connectHandlers[rolloutPath] = rolloutHandlersettingPath, settingHandler := v1connect.NewSettingServiceHandler(settingService, handlerOpts)- connectHandlers[settingPath] = withCORS(settingHandler)+ connectHandlers[settingPath] = settingHandlersheetPath, sheetHandler := v1connect.NewSheetServiceHandler(sheetService, handlerOpts)- connectHandlers[sheetPath] = withCORS(sheetHandler)+ connectHandlers[sheetPath] = sheetHandlersqlPath, sqlHandler := v1connect.NewSQLServiceHandler(sqlService, handlerOpts)- connectHandlers[sqlPath] = withCORS(sqlHandler)+ connectHandlers[sqlPath] = sqlHandlersubscriptionPath, subscriptionHandler := v1connect.NewSubscriptionServiceHandler(subscriptionService, handlerOpts)- connectHandlers[subscriptionPath] = withCORS(subscriptionHandler)+ connectHandlers[subscriptionPath] = subscriptionHandleruserPath, userHandler := v1connect.NewUserServiceHandler(userService, handlerOpts)- connectHandlers[userPath] = withCORS(userHandler)+ connectHandlers[userPath] = userHandlerworksheetPath, worksheetHandler := v1connect.NewWorksheetServiceHandler(worksheetService, handlerOpts)- connectHandlers[worksheetPath] = withCORS(worksheetHandler)+ connectHandlers[worksheetPath] = worksheetHandlerworkspacePath, 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.goindex 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.modindex 249127c9dd..fdd1b60354 100644--- a/go.mod+++ b/go.mod@@ -348,7 +348,6 @@ require (github.com/prometheus/common v0.63.0 // indirectgithub.com/prometheus/procfs v0.16.1 // indirectgithub.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect- github.com/rs/cors v1.7.0github.com/segmentio/asm v1.2.0 // indirectgithub.com/segmentio/backo-go v1.0.1 // indirectgithub.com/shirou/gopsutil/v3 v3.23.12 // indirectdiff --git a/go.sum b/go.sumindex 35264ca5eb..da381e3448 100644--- a/go.sum+++ b/go.sum@@ -1714,8 +1714,6 @@ github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4github.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=