-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.go
104 lines (84 loc) · 2.71 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package bramble
import (
"context"
"flag"
"fmt"
log "log/slog"
"net/http"
"os"
"os/signal"
"sync"
"time"
)
// Main runs the gateway. This function is exported so that it can be reused
// when building Bramble with custom plugins.
func Main() {
ctx := context.Background()
var configFiles arrayFlags
level := new(log.LevelVar)
flag.Var(&configFiles, "config", "Config file (can appear multiple times)")
flag.Var(&configFiles, "conf", "deprecated, use -config instead")
flag.TextVar(level, "loglevel", level, "log level: debug, info, warn, error")
flag.Parse()
logger := log.New(log.NewJSONHandler(os.Stderr, &log.HandlerOptions{Level: level}))
log.SetDefault(logger)
cfg, err := GetConfig(configFiles)
if err != nil {
log.With("error", err).Error("failed to load config")
os.Exit(1)
}
go cfg.Watch()
shutdown, err := InitTelemetry(ctx, cfg.Telemetry)
if err != nil {
log.With("error", err).Error("failed initializing telemetry")
}
defer func() {
log.Info("flushing and shutting down telemetry")
if err := shutdown(context.Background()); err != nil {
log.With("error", err).Error("shutting down telemetry")
}
}()
err = cfg.Init()
if err != nil {
log.With("error", err).Error("failed to configure")
os.Exit(1)
}
log.With("config", cfg).Debug("configuration")
gtw := NewGateway(cfg.executableSchema, cfg.plugins)
RegisterMetrics()
go gtw.UpdateSchemas(cfg.PollIntervalDuration)
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
var wg sync.WaitGroup
wg.Add(3)
go runHandler(ctx, &wg, "metrics", cfg.MetricAddress(), cfg.DefaultTimeouts, NewMetricsHandler())
go runHandler(ctx, &wg, "private", cfg.PrivateAddress(), cfg.PrivateTimeouts, gtw.PrivateRouter())
go runHandler(ctx, &wg, "public", cfg.GatewayAddress(), cfg.GatewayTimeouts, gtw.Router(cfg))
wg.Wait()
}
func runHandler(ctx context.Context, wg *sync.WaitGroup, name, addr string, timeouts TimeoutConfig, handler http.Handler) {
srv := &http.Server{
Addr: addr,
Handler: handler,
ReadTimeout: timeouts.ReadTimeoutDuration,
WriteTimeout: timeouts.WriteTimeoutDuration,
IdleTimeout: timeouts.IdleTimeoutDuration,
}
go func() {
log.With("addr", addr).Info(fmt.Sprintf("serving %s handler", name))
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.With("error", err).Error("server terminated unexpectedly")
os.Exit(1)
}
}()
<-ctx.Done()
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
log.Info(fmt.Sprintf("shutting down %s handler", name))
err := srv.Shutdown(timeoutCtx)
if err != nil {
log.With("error", err).Error("failed shutting down server")
}
log.Info(fmt.Sprintf("shut down %s handler", name))
wg.Done()
}