This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
/
main.go
66 lines (56 loc) · 1.5 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
package main
import (
"context"
"log"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/favecode/reflect-core/graph/generated"
graph "github.com/favecode/reflect-core/graph/resolver"
"github.com/favecode/reflect-core/pkg/db"
)
// Defining the Graphql handler
func graphqlHandler() gin.HandlerFunc {
h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
func GinContextToContextMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := context.WithValue(c.Request.Context(), "header", c)
c.Request = c.Request.WithContext(ctx)
c.Next()
}
}
// 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 init() {
db.Connect()
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
func main() {
// Setting up Gin
port := os.Getenv("PORT")
mode := os.Getenv("GIN_MODE")
gin.SetMode(mode)
r := gin.Default()
r.Use(GinContextToContextMiddleware())
config := cors.DefaultConfig()
config.AllowOrigins = []string{"http://localhost:3000"}
r.Use(cors.New(config))
r.POST("/query", graphqlHandler())
r.GET("/", playgroundHandler())
r.Run(":" + port)
}