forked from movio/bramble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.go
87 lines (70 loc) · 2.1 KB
/
gateway.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package bramble
import (
"net/http"
"time"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
log "github.com/sirupsen/logrus"
)
// Gateway contains the public and private routers
type Gateway struct {
ExecutableSchema *ExecutableSchema
plugins []Plugin
}
// NewGateway returns the graphql gateway server mux
func NewGateway(executableSchema *ExecutableSchema, plugins []Plugin) *Gateway {
return &Gateway{
ExecutableSchema: executableSchema,
plugins: plugins,
}
}
// UpdateSchemas periodically updates the execute schema
func (g *Gateway) UpdateSchemas(interval time.Duration) {
for range time.Tick(interval) {
err := g.ExecutableSchema.UpdateSchema(false)
if err != nil {
log.WithError(err).Error("error updating schemas")
}
}
}
// Router returns the public http handler
func (g *Gateway) Router(cfg *Config) http.Handler {
mux := http.NewServeMux()
// Duplicated from `handler.NewDefaultServer` minus
// the websocket transport and persisted query extension
gatewayHandler := handler.New(g.ExecutableSchema)
gatewayHandler.AddTransport(transport.Options{})
gatewayHandler.AddTransport(transport.GET{})
gatewayHandler.AddTransport(transport.POST{})
gatewayHandler.AddTransport(transport.MultipartForm{})
if !cfg.DisableIntrospection {
gatewayHandler.Use(extension.Introspection{})
}
mux.Handle("/query",
applyMiddleware(
gatewayHandler,
debugMiddleware,
),
)
for _, plugin := range g.plugins {
plugin.SetupPublicMux(mux)
}
var result http.Handler = mux
for i := len(g.plugins) - 1; i >= 0; i-- {
result = g.plugins[i].ApplyMiddlewarePublicMux(result)
}
return applyMiddleware(result, monitoringMiddleware)
}
// PrivateRouter returns the private http handler
func (g *Gateway) PrivateRouter() http.Handler {
mux := http.NewServeMux()
for _, plugin := range g.plugins {
plugin.SetupPrivateMux(mux)
}
var result http.Handler = mux
for i := len(g.plugins) - 1; i >= 0; i-- {
result = g.plugins[i].ApplyMiddlewarePrivateMux(result)
}
return result
}