/
serve.go
68 lines (54 loc) · 1.71 KB
/
serve.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
package server
import (
"context"
"log"
"net/http"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
graphql "github.com/graph-gophers/graphql-go"
// "github.com/GibJob-ai/GObjob/utils"
"github.com/GibJob-ai/GObjob/config"
"github.com/GibJob-ai/GObjob/db"
"github.com/GibJob-ai/GObjob/handler"
"github.com/GibJob-ai/GObjob/resolvers"
"github.com/GibJob-ai/GObjob/schema"
)
func Serve(db *db.DB) {
context.Background()
opts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}
schema := graphql.MustParseSchema(*schema.NewSchema(), &resolvers.Resolvers{DB: db}, opts...)
// Set the router as the default one shipped with Gin
router := gin.Default()
router.Use(NoFlocMiddleware())
//////////////////
// serving single page application stuff
//////////////////
// Serve frontend static files
router.Use(static.Serve("/", static.LocalFile("./frontend/public", true)))
// if no route then serve the root file
router.NoRoute(func(c *gin.Context) {
c.File("./frontend/public/index.html")
})
// graphql routes and route handling
// only enable graphql playground on dev mode
if gin.Mode() == gin.DebugMode {
router.GET("/graphql", gin.WrapH(handler.GraphiQL{}))
}
router.POST("/graphql", gin.WrapH(handler.Authenticate(&handler.GraphQL{Schema: schema})))
// Setup route group for the API
api := router.Group("/api")
{
api.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
}
// Start and run the server depending on mode
if gin.Mode() == gin.DebugMode {
log.Fatal(router.Run(":" + config.CONFIG.Port))
} else if gin.Mode() == gin.ReleaseMode {
log.Fatal(autotls.Run(router, "gibjob.engineer"))
}
}