-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (104 loc) · 2.99 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"github.com/ChicagoDSA/DSA-Events/api"
"github.com/ChicagoDSA/DSA-Events/auth"
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/dgraph-io/dgo"
protosAPI "github.com/dgraph-io/dgo/protos/api"
"github.com/gin-gonic/gin"
"github.com/justinas/nosurf"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
var (
host string = "localhost"
port string = "5000"
log string = "debug"
grpcHost string = "localhost"
grpcPort string = "9080"
)
func init() {
flag.StringVar(&host, "host", host, "")
flag.StringVar(&port, "port", port, "")
flag.StringVar(&log, "log", log, "")
flag.StringVar(&grpcHost, "grpcHost", grpcHost, "")
flag.StringVar(&grpcPort, "grpcPort", grpcPort, "")
}
func setUpRouter(logger *logrus.Logger, dGraphClient *dgo.Dgraph) *gin.Engine {
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(func(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "text")
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(200)
return
}
})
router.Use(func(c *gin.Context) {
c.Set("host", host)
c.Set("port", port)
c.Set("dGraphClient", dGraphClient)
c.Set("log", logger)
})
router.POST("/query", api.QueryHandler)
router.POST("/mutate", api.MutationHandler)
router.POST("/alter", api.AlterationHandler)
// GitHub OAuth2
router.GET("/account/github/callback", auth.GithubCallback)
router.GET("/link/github", auth.GithubInit)
return router
}
func csrfFailHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s\n", nosurf.Reason(r))
}
func main() {
flag.Parse()
logLevel, err := logrus.ParseLevel(log)
if err != nil {
logrus.WithError(err).Fatal("Error parsing log level.")
}
logger := logrus.New()
logger.Level = logLevel
if logLevel != logrus.DebugLevel {
gin.SetMode(gin.ReleaseMode)
}
logger.WithField("level", logLevel.String()).Debug("Log Level Set")
// Establish DGraph connection via gRPC
conn, err := grpc.Dial(grpcHost+":"+grpcPort, grpc.WithInsecure())
if err != nil {
logrus.WithError(err).Fatal("Error establishing gRPC connection with DGraph instance.")
}
logrus.RegisterExitHandler(func() {
conn.Close()
})
dgc := protosAPI.NewDgraphClient(conn)
dGraphClient := dgo.NewDgraphClient(dgc)
router := setUpRouter(logger, dGraphClient)
server := &http.Server{
Addr: ":" + port,
Handler: router,
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
_ = (<-c)
logger.Info("Shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := server.Shutdown(ctx)
if err != nil {
logger.WithError(server.Shutdown(ctx)).Fatal("Server shutdown")
}
os.Exit(0)
}()
logger.WithError(router.Run(host + ":" + port)).Fatal("Error in setting up HTTP server.")
}