-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
81 lines (65 loc) · 1.99 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Copyright 2019 Wireline, Inc.
//
package gql
import (
"net/http"
"github.com/spf13/viper"
"github.com/99designs/gqlgen/handler"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/vulcanize/dxns/x/auction"
"github.com/vulcanize/dxns/x/bond"
"github.com/vulcanize/dxns/x/nameservice"
"github.com/go-chi/chi"
"github.com/rs/cors"
)
// Server configures and starts the GQL server.
func Server(baseApp *bam.BaseApp,
cdc *codec.Codec,
keeper nameservice.Keeper,
bondKeeper bond.Keeper,
accountKeeper auth.AccountKeeper,
auctionKeeper auction.Keeper) {
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")
if viper.GetBool("gql-playground") {
apiBase := viper.GetString("gql-playground-api-base")
router.Handle("/webui", handler.Playground("Wireline Naming Service", apiBase+"/api"))
// TODO(ashwin): Kept for backward compat.
router.Handle("/console", handler.Playground("Wireline Naming Service", apiBase+"/graphql"))
}
router.Handle("/api", handler.GraphQL(NewExecutableSchema(Config{Resolvers: &Resolver{
baseApp: baseApp,
codec: cdc,
keeper: keeper,
bondKeeper: bondKeeper,
accountKeeper: accountKeeper,
auctionKeeper: auctionKeeper,
logFile: logFile,
}})))
// TODO(ashwin): Kept for backward compat.
router.Handle("/graphql", handler.GraphQL(NewExecutableSchema(Config{Resolvers: &Resolver{
baseApp: baseApp,
codec: cdc,
keeper: keeper,
bondKeeper: bondKeeper,
accountKeeper: accountKeeper,
auctionKeeper: auctionKeeper,
logFile: logFile,
}})))
err := http.ListenAndServe(":"+viper.GetString("gql-port"), router)
if err != nil {
panic(err)
}
}