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"
|
2022-12-09 07:52:38 +00:00
|
|
|
"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
|
|
|
|
}
|
2022-12-09 07:52:38 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}}))
|
|
|
|
|
2022-12-12 08:21:53 +00:00
|
|
|
router.Handle("/", PlaygroundHandler("/api"))
|
2022-10-12 07:34:44 +00:00
|
|
|
|
|
|
|
if viper.GetBool("gql-playground") {
|
|
|
|
apiBase := viper.GetString("gql-playground-api-base")
|
|
|
|
|
2022-12-12 08:21:53 +00:00
|
|
|
router.Handle("/webui", PlaygroundHandler(apiBase+"/api"))
|
|
|
|
router.Handle("/console", PlaygroundHandler(apiBase+"/graphql"))
|
2022-10-12 07:34:44 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 07:52:38 +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))
|
2022-12-09 07:52:38 +00:00
|
|
|
err := http.ListenAndServe(":"+port, router) //nolint: all
|
2022-10-12 07:34:44 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|