-
Notifications
You must be signed in to change notification settings - Fork 55
/
gateway.go
90 lines (73 loc) · 2.34 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
88
89
90
package bramble
import (
"context"
log "log/slog"
"net/http"
"time"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
// 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) {
time.Sleep(interval)
for range time.Tick(interval) {
err := g.ExecutableSchema.UpdateSchema(context.Background(), false)
if err != nil {
log.With("error", err).Error("failed updating schemas")
}
}
}
// Router returns the public http handler
func (g *Gateway) Router(cfg *Config) http.Handler {
mux := http.NewServeMux()
gatewayHandler := handler.New(g.ExecutableSchema)
for _, plugin := range g.plugins {
plugin.SetupGatewayHandler(gatewayHandler)
}
// Duplicated from `handler.NewDefaultServer` minus
// the websocket transport and persisted query extension
gatewayHandler.AddTransport(transport.Options{})
gatewayHandler.AddTransport(transport.GET{})
gatewayHandler.AddTransport(transport.POST{})
gatewayHandler.AddTransport(transport.MultipartForm{
MaxUploadSize: cfg.MaxFileUploadSize,
})
if !cfg.DisableIntrospection {
gatewayHandler.Use(extension.Introspection{})
}
mux.Handle("/query", applyMiddleware(otelhttp.NewHandler(gatewayHandler, "/query"), 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
}