-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
56 lines (45 loc) · 1.33 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
54
55
56
package main // import "github.com/mattdamon108/go-graphql-api-boilerplate"
import (
"context"
"log"
"net/http"
"os"
graphql "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
"github.com/joho/godotenv"
"github.com/rs/cors"
"github.com/mattdamon108/go-graphql-api-boilerplate/db"
"github.com/mattdamon108/go-graphql-api-boilerplate/handler"
"github.com/mattdamon108/go-graphql-api-boilerplate/resolvers"
"github.com/mattdamon108/go-graphql-api-boilerplate/schema"
)
func main() {
db, err := db.ConnectDB()
if err != nil {
panic(err)
}
context.Background()
opts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}
schema := graphql.MustParseSchema(*schema.NewSchema(), &resolvers.Resolvers{DB: db}, opts...)
mux := http.NewServeMux()
mux.Handle("/", handler.GraphiQL{})
mux.Handle("/query", handler.Authenticate(&relay.Handler{Schema: schema}))
err = godotenv.Load()
if err != nil {
panic(err)
}
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:8280"},
AllowedHeaders: []string{"Authorization", "Content-Type"},
AllowCredentials: true,
Debug: os.Getenv("STAGE") != "PROD",
})
s := &http.Server{
Addr: ":8080",
Handler: c.Handler(mux),
}
log.Println("Listening to... port 8080")
if err = s.ListenAndServe(); err != nil {
panic(err)
}
}