From ab120d21dc6c017e27ac37771ba7dceb7f42105c Mon Sep 17 00:00:00 2001 From: yakud Date: Wed, 8 May 2024 18:30:54 +0300 Subject: [PATCH] Add CORS headers --- internal/query/server.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/query/server.go b/internal/query/server.go index aa2984f..2d99d56 100644 --- a/internal/query/server.go +++ b/internal/query/server.go @@ -28,6 +28,7 @@ import ( "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/swaggest/swgui/v5cdn" "google.golang.org/grpc" + "google.golang.org/protobuf/proto" merklegen "github.com/Galactica-corp/merkle-proof-service/gen/galactica/merkle" merkleswagger "github.com/Galactica-corp/merkle-proof-service/gen/openapiv2/galactica/merkle" @@ -106,7 +107,18 @@ func (s *Server) RunGateway(ctx context.Context, address string) error { gwmux := runtime.NewServeMux( runtime.WithIncomingHeaderMatcher(runtime.DefaultHeaderMatcher), + + // CORS middleware for all requests + runtime.WithForwardResponseOption(func(ctx context.Context, w http.ResponseWriter, msg proto.Message) error { + header := w.Header() + header.Set("Access-Control-Allow-Origin", "*") + header.Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") + header.Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Request-ID, X-Client-ID") + + return nil + }), ) + if err := merklegen.RegisterQueryHandlerServer(ctx, gwmux, s); err != nil { return fmt.Errorf("failed to register gRPC gateway: %v", err) }