-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (62 loc) · 1.67 KB
/
main.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
82
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/bamboooo-dev/meshi-api/app/interface/mysql"
"github.com/bamboooo-dev/meshi-api/auth"
"github.com/bamboooo-dev/meshi-api/graph"
"github.com/bamboooo-dev/meshi-api/graph/generated"
"github.com/bamboooo-dev/meshi-api/pkg/env"
"github.com/urfave/negroni"
"go.uber.org/zap"
)
var revision = "undefined"
func main() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Panic '%v' captured\n", err)
}
}()
fmt.Printf("Version is %s\n", revision)
cfg, err := env.LoadConfig()
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger, _ := zap.NewProduction()
defer func() {
if err := logger.Sync(); err != nil {
logger.Error(err.Error())
return
}
}()
sugar := logger.Sugar()
meshiDB, err := mysql.NewDB(cfg.MeshiMySQL)
if err != nil {
sugar.Error(ctx, err)
return
}
defer func() {
if err := meshiDB.Close(); err != nil {
sugar.Error(ctx, err)
return
}
}()
resolver := graph.NewResolver(sugar, meshiDB)
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: resolver}))
jwtMiddleware := auth.NewJWTMiddleware()
mux := http.NewServeMux()
mux.Handle("/", playground.Handler("GraphQL playground", "/query"))
mux.Handle("/query", srv)
n := negroni.New(negroni.HandlerFunc(jwtMiddleware.HandlerWithNext))
n.UseHandler(mux)
log.Print("connect to http://localhost:8080/ for GraphQL playground")
if err := http.ListenAndServe(":8080", n); err != nil {
sugar.Error(ctx, err)
}
}