-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (57 loc) · 1.72 KB
/
main.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
package main
import (
"github.com/RobusK/GoCtaApi/api"
"github.com/RobusK/GoCtaApi/dataloaders"
"github.com/RobusK/GoCtaApi/services"
"github.com/graphql-go/handler"
"net/http"
"os"
"time"
)
var httpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 15,
IdleConnTimeout: 1 * time.Second,
TLSHandshakeTimeout: 1 * time.Second,
},
Timeout: time.Second * 10,
}
var (
client = api.NewAPIClient(getAPIKey(), httpClient)
directionService = services.NewDirectionsService(client)
routeService = services.NewRoutesService(client)
stopService = services.NewStopsService(client)
//predictionService = services.NewPredictionsService(client)
)
func getAPIKey() string {
if len(CtaAPIKey) == 0 {
return os.Getenv("CTA_KEY")
}
return CtaAPIKey
}
func main() {
schema := gqlSchema(dataloaders.NewRetriever())
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
})
staticFileHandler := http.FileServer(http.Dir("./static/"))
dlMiddleware := dataloaders.Middleware(*client)
http.Handle("/graphql", dlMiddleware(disableCors(h)))
http.Handle("/", staticFileHandler)
http.ListenAndServe(serverHostname+":"+serverPort, nil)
}
func disableCors(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Content-Length, Accept-Encoding")
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Max-Age", "86400")
w.WriteHeader(http.StatusOK)
return
}
h.ServeHTTP(w, r)
})
}