forked from howtographql/graphql-golang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
40 lines (31 loc) · 1.06 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
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi/v5"
"github.com/AndriyKalashnykov/graphlq-golang/graph"
"github.com/AndriyKalashnykov/graphlq-golang/graph/generated"
"github.com/AndriyKalashnykov/graphlq-golang/internal/auth"
_ "github.com/AndriyKalashnykov/graphlq-golang/internal/auth"
database "github.com/AndriyKalashnykov/graphlq-golang/internal/pkg/db/mysql"
)
const defaultPort = "8080"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
router := chi.NewRouter()
router.Use(auth.Middleware())
database.InitDB()
defer database.CloseDB()
database.Migrate()
server := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}}))
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
router.Handle("/query", server)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}