-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathserver.go
48 lines (38 loc) · 1.58 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
package main
import (
gcontext "github.com/OscarYuen/go-graphql-starter/context"
h "github.com/OscarYuen/go-graphql-starter/handler"
"github.com/OscarYuen/go-graphql-starter/resolver"
"github.com/OscarYuen/go-graphql-starter/schema"
"github.com/OscarYuen/go-graphql-starter/service"
"log"
"net/http"
"github.com/OscarYuen/go-graphql-starter/loader"
graphql "github.com/graph-gophers/graphql-go"
"golang.org/x/net/context"
)
func main() {
config := gcontext.LoadConfig(".")
db, err := gcontext.OpenDB(config)
if err != nil {
log.Fatalf("Unable to connect to db: %s \n", err)
}
ctx := context.Background()
log := service.NewLogger(config)
roleService := service.NewRoleService(db, log)
userService := service.NewUserService(db, roleService, log)
authService := service.NewAuthService(config, log)
ctx = context.WithValue(ctx, "config", config)
ctx = context.WithValue(ctx, "log", log)
ctx = context.WithValue(ctx, "roleService", roleService)
ctx = context.WithValue(ctx, "userService", userService)
ctx = context.WithValue(ctx, "authService", authService)
graphqlSchema := graphql.MustParseSchema(schema.GetRootSchema(), &resolver.Resolver{})
http.Handle("/login", h.AddContext(ctx, h.Login()))
loggerHandler := &h.LoggerHandler{config.DebugMode}
http.Handle("/query", h.AddContext(ctx, loggerHandler.Logging(h.Authenticate(&h.GraphQL{Schema: graphqlSchema, Loaders: loader.NewLoaderCollection()}))))
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "graphiql.html")
}))
log.Fatal(http.ListenAndServe(":3000", nil))
}