From 474a4d89da24ec24e4021f04fc628085468714dd Mon Sep 17 00:00:00 2001 From: nikugogoi Date: Fri, 9 Dec 2022 13:22:38 +0530 Subject: [PATCH] Add cors middleware for GQL requests (#65) --- go.mod | 1 + go.sum | 2 ++ gql/server.go | 24 ++++++++++++++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 7d6be002..71a78a91 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/deckarep/golang-set v1.8.0 github.com/ethereum/go-ethereum v1.10.19 github.com/gibson042/canonicaljson-go v1.0.3 + github.com/go-chi/chi/v5 v5.0.7 github.com/gogo/protobuf v1.3.3 github.com/golang/protobuf v1.5.2 github.com/gorilla/mux v1.8.0 diff --git a/go.sum b/go.sum index 6b4c5a8e..29612579 100644 --- a/go.sum +++ b/go.sum @@ -393,6 +393,8 @@ github.com/gin-gonic/gin v1.7.0/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjX github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE= github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24= github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= +github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= +github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= diff --git a/gql/server.go b/gql/server.go index dd5fe5a7..7eb82c7e 100644 --- a/gql/server.go +++ b/gql/server.go @@ -8,6 +8,8 @@ import ( "github.com/99designs/gqlgen/graphql/playground" "github.com/cosmos/cosmos-sdk/client" "github.com/ethereum/go-ethereum/log" + "github.com/go-chi/chi/v5" + "github.com/rs/cors" "github.com/spf13/viper" ) @@ -16,6 +18,16 @@ func Server(ctx client.Context) { if !viper.GetBool("gql-server") { return } + + router := chi.NewRouter() + + // Add CORS middleware around every request + // See https://github.com/rs/cors for full option listing + router.Use(cors.New(cors.Options{ + AllowedOrigins: []string{"*"}, + Debug: false, + }).Handler) + logFile := viper.GetString("log-file") port := viper.GetString("gql-port") @@ -25,20 +37,20 @@ func Server(ctx client.Context) { logFile: logFile, }})) - http.Handle("/", playground.Handler("GraphQL playground", "/api")) + router.Handle("/", playground.Handler("GraphQL playground", "/api")) if viper.GetBool("gql-playground") { apiBase := viper.GetString("gql-playground-api-base") - http.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api")) - http.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql")) + router.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api")) + router.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql")) } - http.Handle("/api", srv) - http.Handle("/graphql", srv) + router.Handle("/api", srv) + router.Handle("/graphql", srv) log.Info("Connect to GraphQL playground", "url", fmt.Sprintf("http://localhost:%s", port)) - err := http.ListenAndServe(":"+port, nil) //nolint: all + err := http.ListenAndServe(":"+port, router) //nolint: all if err != nil { panic(err) }