-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (45 loc) · 1.3 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/earthcubearchitecture-project418/mercantile/graph"
"github.com/earthcubearchitecture-project418/mercantile/graph/generated"
"github.com/spf13/viper"
)
const defaultPort = "8080"
var viperVal string
func main() {
var v1 *viper.Viper
v1, err := readConfig(viperVal, map[string]interface{}{
"address": "localhost",
"port": "8080",
})
if err != nil {
panic(fmt.Errorf("error when reading config: %v", err))
}
mcfg := v1.GetStringMapString("server")
port := os.Getenv(mcfg["port"])
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://0.0.0.0:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func readConfig(filename string, defaults map[string]interface{}) (*viper.Viper, error) {
v := viper.New()
for key, value := range defaults {
v.SetDefault(key, value)
}
v.SetConfigName(filename)
v.AddConfigPath(".")
v.AutomaticEnv()
err := v.ReadInConfig()
return v, err
}