From 02e9080094c47f0411ad8bd15e99e22ccdfdf09c Mon Sep 17 00:00:00 2001 From: Nabarun Gogoi Date: Wed, 11 Jan 2023 13:06:42 +0530 Subject: [PATCH] Changes to run laconic-console app with laconicd (#75) * Add cors middleware for GQL requests (#65) * Implement loading query from URL in GQL playground (#66) * Implement loading GQL query from URL * Fix variable name lint error * Load record names in `getRecordsByIds` GQL query (#67) * Fix variable rename * Load names in getRecordsByIds GQL query * CI Review fixes * Make init script executable --- go.mod | 1 + go.sum | 2 + gql/graphiql.go | 165 ++++++++++++++++++++++++++++++++++++ gql/server.go | 25 ++++-- init.sh | 0 x/registry/keeper/keeper.go | 2 +- 6 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 gql/graphiql.go mode change 100644 => 100755 init.sh diff --git a/go.mod b/go.mod index 2e6912e9..7e368854 100644 --- a/go.mod +++ b/go.mod @@ -18,6 +18,7 @@ require ( github.com/deckarep/golang-set v1.8.0 github.com/ethereum/go-ethereum v1.10.26 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 26ee95c6..de7d249d 100644 --- a/go.sum +++ b/go.sum @@ -367,6 +367,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/graphiql.go b/gql/graphiql.go new file mode 100644 index 00000000..32f4b85b --- /dev/null +++ b/gql/graphiql.go @@ -0,0 +1,165 @@ +package gql + +import ( + "html/template" + "net/http" +) + +// GraphiQL is an in-browser IDE for exploring GraphiQL APIs. +// This handler returns GraphiQL when requested. +// +// For more information, see https://github.com/graphql/graphiql. + +func PlaygroundHandler(apiURL string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, "only GET requests are supported", http.StatusMethodNotAllowed) + return + } + w.Header().Set("Content-Type", "text/html") + err := page.Execute(w, map[string]interface{}{ + "apiURL": apiURL, + }) + if err != nil { + panic(err) + } + } +} + +// https://github.com/graphql/graphiql/blob/main/examples/graphiql-cdn/index.html +var page = template.Must(template.New("graphiql").Parse(` + + + + GraphiQL + + + + + + + + + + + +
Loading...
+ + + + +`)) diff --git a/gql/server.go b/gql/server.go index dd5fe5a7..32337856 100644 --- a/gql/server.go +++ b/gql/server.go @@ -5,9 +5,10 @@ import ( "net/http" "github.com/99designs/gqlgen/graphql/handler" - "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 +17,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 +36,20 @@ func Server(ctx client.Context) { logFile: logFile, }})) - http.Handle("/", playground.Handler("GraphQL playground", "/api")) + router.Handle("/", PlaygroundHandler("/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", PlaygroundHandler(apiBase+"/api")) + router.Handle("/console", PlaygroundHandler(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) } diff --git a/init.sh b/init.sh old mode 100644 new mode 100755 diff --git a/x/registry/keeper/keeper.go b/x/registry/keeper/keeper.go index eabfe20e..e7891658 100644 --- a/x/registry/keeper/keeper.go +++ b/x/registry/keeper/keeper.go @@ -111,7 +111,7 @@ func (k Keeper) GetRecord(ctx sdk.Context, id string) (record types.Record) { store := ctx.KVStore(k.storeKey) result := store.Get(GetRecordIndexKey(id)) k.cdc.MustUnmarshal(result, &record) - return record + return recordObjToRecord(store, record) } // ListRecords - get all records.