Add cors middleware for GQL requests (#65)

This commit is contained in:
nikugogoi 2022-12-09 13:22:38 +05:30 committed by GitHub
parent 72ec75a857
commit 474a4d89da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 6 deletions

1
go.mod
View File

@ -15,6 +15,7 @@ require (
github.com/deckarep/golang-set v1.8.0 github.com/deckarep/golang-set v1.8.0
github.com/ethereum/go-ethereum v1.10.19 github.com/ethereum/go-ethereum v1.10.19
github.com/gibson042/canonicaljson-go v1.0.3 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/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.2 github.com/golang/protobuf v1.5.2
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0

2
go.sum
View File

@ -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/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/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.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 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-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=

View File

@ -8,6 +8,8 @@ import (
"github.com/99designs/gqlgen/graphql/playground" "github.com/99designs/gqlgen/graphql/playground"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/go-chi/chi/v5"
"github.com/rs/cors"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -16,6 +18,16 @@ func Server(ctx client.Context) {
if !viper.GetBool("gql-server") { if !viper.GetBool("gql-server") {
return 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") logFile := viper.GetString("log-file")
port := viper.GetString("gql-port") port := viper.GetString("gql-port")
@ -25,20 +37,20 @@ func Server(ctx client.Context) {
logFile: logFile, logFile: logFile,
}})) }}))
http.Handle("/", playground.Handler("GraphQL playground", "/api")) router.Handle("/", playground.Handler("GraphQL playground", "/api"))
if viper.GetBool("gql-playground") { if viper.GetBool("gql-playground") {
apiBase := viper.GetString("gql-playground-api-base") apiBase := viper.GetString("gql-playground-api-base")
http.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api")) router.Handle("/webui", playground.Handler("GraphQL playground", apiBase+"/api"))
http.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql")) router.Handle("/console", playground.Handler("GraphQL playground", apiBase+"/graphql"))
} }
http.Handle("/api", srv) router.Handle("/api", srv)
http.Handle("/graphql", srv) router.Handle("/graphql", srv)
log.Info("Connect to GraphQL playground", "url", fmt.Sprintf("http://localhost:%s", port)) 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 { if err != nil {
panic(err) panic(err)
} }