-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
156 lines (130 loc) · 4.58 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/alphagov/router/handlers"
router "github.com/alphagov/router/lib"
"github.com/getsentry/sentry-go"
sentryzerolog "github.com/getsentry/sentry-go/zerolog"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
)
func usage() {
helpstring := `
GOV.UK Router %s
Usage: %s [-version]
The following environment variables and defaults are available:
ROUTER_PUBADDR=:8080 Address on which to serve public requests
ROUTER_APIADDR=:8081 Address on which to receive reload requests
ROUTER_ERROR_LOG=STDERR File to log errors to (in JSON format)
ROUTER_DEBUG= Enable debug output if non-empty
Timeouts: (values must be parseable by https://pkg.go.dev/time#ParseDuration)
ROUTER_BACKEND_CONNECT_TIMEOUT=1s Connect timeout when connecting to backends
ROUTER_BACKEND_HEADER_TIMEOUT=15s Timeout for backend response headers to be returned
ROUTER_FRONTEND_READ_TIMEOUT=60s See https://cs.opensource.google/go/go/+/master:src/net/http/server.go?q=symbol:ReadTimeout
ROUTER_FRONTEND_WRITE_TIMEOUT=60s See https://cs.opensource.google/go/go/+/master:src/net/http/server.go?q=symbol:WriteTimeout
ROUTER_ROUTE_RELOAD_INTERVAL=1m Interval for periodic route reloads
`
fmt.Fprintf(os.Stderr, helpstring, router.VersionInfo(), os.Args[0])
const ErrUsage = 64
os.Exit(ErrUsage)
}
func getenv(key string, defaultVal string) string {
if s := os.Getenv(key); s != "" {
return s
}
return defaultVal
}
func getenvDuration(key string, defaultVal string) time.Duration {
s := getenv(key, defaultVal)
return mustParseDuration(s)
}
func mustParseDuration(s string) (d time.Duration) {
d, err := time.ParseDuration(s)
if err != nil {
log.Fatal(err)
}
return
}
func listenAndServeOrFatal(addr string, handler http.Handler, rTimeout time.Duration, wTimeout time.Duration) {
srv := &http.Server{
Addr: addr,
Handler: handler,
ReadTimeout: rTimeout,
WriteTimeout: wTimeout,
}
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func main() {
returnVersion := flag.Bool("version", false, "")
flag.Usage = usage
flag.Parse()
fmt.Printf("GOV.UK Router %s\n", router.VersionInfo())
if *returnVersion {
os.Exit(0)
}
// Initialize Sentry
if err := sentry.Init(sentry.ClientOptions{}); err != nil {
panic(err)
}
defer sentry.Flush(2 * time.Second)
// Configure Sentry Zerolog Writer
writer, err := sentryzerolog.New(sentryzerolog.Config{
ClientOptions: sentry.ClientOptions{},
Options: sentryzerolog.Options{
Levels: []zerolog.Level{zerolog.ErrorLevel, zerolog.FatalLevel},
FlushTimeout: 3 * time.Second,
WithBreadcrumbs: true,
},
})
if err != nil {
panic(err)
}
defer writer.Close()
// Initialize Zerolog
m := zerolog.MultiLevelWriter(os.Stderr, writer)
logger := zerolog.New(m).With().Timestamp().Logger()
var (
pubAddr = getenv("ROUTER_PUBADDR", ":8080")
apiAddr = getenv("ROUTER_APIADDR", ":8081")
tlsSkipVerify = os.Getenv("ROUTER_TLS_SKIP_VERIFY") != ""
beConnTimeout = getenvDuration("ROUTER_BACKEND_CONNECT_TIMEOUT", "1s")
beHeaderTimeout = getenvDuration("ROUTER_BACKEND_HEADER_TIMEOUT", "20s")
feReadTimeout = getenvDuration("ROUTER_FRONTEND_READ_TIMEOUT", "60s")
feWriteTimeout = getenvDuration("ROUTER_FRONTEND_WRITE_TIMEOUT", "60s")
routeReloadInterval = getenvDuration("ROUTER_ROUTE_RELOAD_INTERVAL", "1m")
)
logger.Info().Msgf("frontend read timeout: %v", feReadTimeout)
logger.Info().Msgf("frontend write timeout: %v", feWriteTimeout)
logger.Info().Msgf("GOMAXPROCS value of %d", runtime.GOMAXPROCS(0))
if tlsSkipVerify {
handlers.TLSSkipVerify = true
logger.Warn().Msg("skipping verification of TLS certificates; Do not use this option in a production environment.")
}
router.RegisterMetrics(prometheus.DefaultRegisterer)
rout, err := router.NewRouter(router.Options{
BackendConnTimeout: beConnTimeout,
BackendHeaderTimeout: beHeaderTimeout,
RouteReloadInterval: routeReloadInterval,
Logger: logger,
})
if err != nil {
logger.Fatal().Err(err).Msg("failed to create router")
}
go rout.PeriodicRouteUpdates()
go listenAndServeOrFatal(pubAddr, rout, feReadTimeout, feWriteTimeout)
logger.Info().Msgf("listening for requests on %v", pubAddr)
api, err := router.NewAPIHandler(rout)
if err != nil {
logger.Fatal().Err(err).Msg("failed to create API handler")
}
logger.Info().Msgf("listening for API requests on %v", apiAddr)
listenAndServeOrFatal(apiAddr, api, feReadTimeout, feWriteTimeout)
}