Skip to content

Instantly share code, notes, and snippets.

@r6m
Created January 22, 2023 07:49
Show Gist options
  • Save r6m/9f6d0876b2c5dc7d9a2377afedc75f38 to your computer and use it in GitHub Desktop.
Save r6m/9f6d0876b2c5dc7d9a2377afedc75f38 to your computer and use it in GitHub Desktop.
grpc gateway cors
import (
 // ...
 "github.com/rs/cors" 
)

// ...
mux := runtime.NewServeMux()
proto.RegisterXYZServiceHandler(ctx, mux, conn)

withCors := cors.New(cors.Options{
  AllowOriginFunc:  func(origin string) bool { return true },
  AllowedMethods:   []string{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"},
  AllowedHeaders:   []string{"ACCEPT", "Authorization", "Content-Type", "X-CSRF-Token"},
  ExposedHeaders:   []string{"Link"},
  AllowCredentials: true,
  MaxAge:           300,
}).Handler(mux)

s.httpServer = &http.Server{
  Addr:    ":8080",
  Handler: withCors,
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment