-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (35 loc) · 1.03 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
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/cors"
"github.com/graphql-go/handler"
"github.com/chafikchaban/greenheat-backend/weather"
)
func main() {
r := gin.Default()
r.Use(cors.Default())
lc := weather.LocationController{}
wc := weather.WeatherController{}
db := weather.BootstrapDatabase("./")
lc.InitializeLocations(db)
// Create GraphQL handler
h := handler.New(&handler.Config{
Schema: &weather.Schema,
Pretty: true,
GraphiQL: true,
})
// GraphQL endpoint
r.POST("/graphql", func(c *gin.Context) {
ctx := c.Request.Context()
ctx = context.WithValue(ctx, "db", db)
ctx = context.WithValue(ctx, "lc", lc)
ctx = context.WithValue(ctx, "wc", wc)
h.ContextHandler(ctx, c.Writer, c.Request)
})
// GraphiQL endpoint for testing
r.GET("/graphiql", func(c *gin.Context) {
h.ContextHandler(c.Request.Context(), c.Writer, c.Request)
})
r.Run(":3000")
}