laconicd/gql/server.go

57 lines
1.3 KiB
Go
Raw Normal View History

2022-10-12 07:34:44 +00:00
package gql
import (
"fmt"
"net/http"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/cosmos/cosmos-sdk/client"
"github.com/ethereum/go-ethereum/log"
"github.com/go-chi/chi/v5"
"github.com/rs/cors"
2022-10-12 07:34:44 +00:00
"github.com/spf13/viper"
)
// Server configures and starts the GQL server.
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)
2022-10-12 07:34:44 +00:00
logFile := viper.GetString("log-file")
port := viper.GetString("gql-port")
srv := handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: &Resolver{
ctx: ctx,
logFile: logFile,
}}))
router.Handle("/", PlaygroundHandler("/api"))
2022-10-12 07:34:44 +00:00
if viper.GetBool("gql-playground") {
apiBase := viper.GetString("gql-playground-api-base")
router.Handle("/webui", PlaygroundHandler(apiBase+"/api"))
router.Handle("/console", PlaygroundHandler(apiBase+"/graphql"))
2022-10-12 07:34:44 +00:00
}
router.Handle("/api", srv)
router.Handle("/graphql", srv)
2022-10-12 07:34:44 +00:00
log.Info("Connect to GraphQL playground", "url", fmt.Sprintf("http://localhost:%s", port))
2023-01-27 15:48:00 +00:00
err := http.ListenAndServe(":"+port, router) //nolint: all
2022-10-12 07:34:44 +00:00
if err != nil {
panic(err)
}
}