-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (48 loc) · 1.22 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
package main
import (
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/trackhub/api/graph"
"github.com/trackhub/api/graph/generated"
)
// Defining the Graphql handler
func graphqlHandler() gin.HandlerFunc {
// NewExecutableSchema and Config are in the generated.go file
// Resolver is in the resolver.go file
h := handler.NewDefaultServer(
generated.NewExecutableSchema(
generated.Config{Resolvers: &graph.Resolver{}},
),
)
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
// Defining the Playground handler
func playgroundHandler() gin.HandlerFunc {
h := playground.Handler("GraphQL", "/query")
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
godotenv.Load()
r := gin.Default()
hf := func(ctx *gin.Context) {
auth := ctx.Request.Header.Get("auth")
if auth != "test123" {
// ctx.JSON(500, map[string]string{"test123": "da"})
// do ctx.Abort() to cancel the request
return
}
ctx.Next()
}
g := r.Group("/")
g.Use(hf)
g.POST("/query", graphqlHandler())
r.GET("/", playgroundHandler())
r.Run(":" + os.Getenv("PORT"))
}