-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
53 lines (40 loc) · 1.13 KB
/
server.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
package main
import (
"log"
"os"
"github.com/bottlehub/unboard/auth"
"github.com/bottlehub/unboard/graph"
"github.com/bottlehub/unboard/routes"
"github.com/gin-gonic/gin"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
)
const defaultPort = "8080"
func graphqlHandler() gin.HandlerFunc {
handle := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
return func(c *gin.Context) {
handle.ServeHTTP(c.Writer, c.Request)
}
}
func playgroundHandler() gin.HandlerFunc {
handle := playground.Handler("GraphQL", "/query")
return func(c *gin.Context) {
handle.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
router := chi.NewRouter()
router.Use(auth.Middleware("phrase"))
route := gin.Default()
route.GET("/")
routes.Route(route)
route.GET("/graphql", playgroundHandler())
route.POST("/query", graphqlHandler())
log.Printf("Connect to http://localhost:%s/graphql for GraphQL playground", port)
log.Fatal(route.Run("localhost:" + port))
}