-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
346 lines (283 loc) · 8.26 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"sync/atomic"
"syscall"
"time"
"cloud.google.com/go/logging"
"github.com/go-redis/redis/v8"
"github.com/hellofresh/health-go/v4"
"github.com/kelseyhightower/envconfig"
"github.com/mpraski/api-gateway/app/proxy"
"github.com/mpraski/api-gateway/app/ratelimit"
"github.com/mpraski/api-gateway/app/secret"
"github.com/mpraski/api-gateway/app/token"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type config struct {
Debug bool
Delay time.Duration `default:"1s"`
Config string `required:"true"`
Server struct {
Address struct {
Public string `default:":8080"`
Observability string `default:":9090"`
}
ReadTimeout time.Duration `split_words:"true" default:"30s"`
WriteTimeout time.Duration `split_words:"true" default:"30s"`
IdleTimeout time.Duration `split_words:"true" default:"120s"`
ReadyTimeout time.Duration `split_words:"true" default:"5s"`
ShutdownTimeout time.Duration `split_words:"true" default:"10s"`
ReadHeaderTimeout time.Duration `split_words:"true" default:"5s"`
}
Identity struct {
BaseURL string `required:"true" split_words:"true"`
Timeout time.Duration `default:"15s"`
}
Redis struct {
Address string
Database int `default:"0"`
}
Secrets struct {
RedisCertificate string `split_words:"true"`
}
Project struct {
ID string `required:"true"`
}
}
var (
// Health check
ready int32
app = "api_gateway"
// Errors
errShutdown = errors.New("shutdown in progress")
errTooManyGoroutines = errors.New("too many goroutines")
errRedisMisconfigured = errors.New("redis is misconfigured")
errCertificateInvalid = errors.New("failed to decode PEM certificate")
)
func main() {
ctx := context.Background()
var cfg config
if err := envconfig.Process(app, &cfg); err != nil {
log.Fatalf("failed to load config: %v", err)
}
time.Sleep(cfg.Delay)
var opts []option.ClientOption
if cfg.Debug {
opts = append(opts,
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
)
}
c, err := logging.NewClient(ctx, cfg.Project.ID, opts...)
if err != nil {
log.Fatalf("failed to setup logging client: %v", err)
}
defer func() {
if err := c.Close(); err != nil {
log.Fatalf("failed to close logging client: %v", err)
}
}()
l := c.Logger(app, logging.RedirectAsJSON(os.Stdout))
if err := run(ctx, &cfg, l); err != nil {
l.StandardLogger(logging.Critical).Fatalf("failed to run app: %v", err)
}
}
func run(ctx context.Context, cfg *config, lg *logging.Logger) error {
var (
appLog = lg.StandardLogger(logging.Info)
errLog = lg.StandardLogger(logging.Critical)
client = token.NewClient(cfg.Identity.BaseURL, &http.Client{Timeout: cfg.Identity.Timeout})
)
rateLimiter, closer, err := newRateLimiter(ctx, cfg)
if err != nil {
return fmt.Errorf("failed to initialize rate limiter: %w", err)
}
defer func() {
if err = closer(); err != nil {
errLog.Fatalf("failed to close rate limiter: %v", err)
}
}()
if rateLimiter == nil {
appLog.Println("not using rate limiting")
} else {
appLog.Println("using rate limiting")
}
p, err := proxy.New(cfg.Config, client, lg, rateLimiter)
if err != nil {
return fmt.Errorf("failed to initialize proxy: %w", err)
}
checks, err := newHealthChecks()
if err != nil {
return fmt.Errorf("failed to setup health checks: %w", err)
}
var (
warm sync.WaitGroup
done = make(chan struct{})
quit = make(chan os.Signal, 1)
publicServer = &http.Server{
Addr: cfg.Server.Address.Public,
ReadTimeout: cfg.Server.ReadTimeout,
WriteTimeout: cfg.Server.WriteTimeout,
IdleTimeout: cfg.Server.IdleTimeout,
ReadHeaderTimeout: cfg.Server.ReadHeaderTimeout,
Handler: p.Handler(),
BaseContext: func(net.Listener) context.Context {
return ctx
},
}
observabilityServer = newServer(ctx, cfg, cfg.Server.Address.Observability, func(m *http.ServeMux) {
m.Handle("/livez", checks[0])
m.Handle("/readyz", checks[1])
})
runServer = func(server *http.Server) {
warm.Done()
appLog.Println("starting server at", server.Addr)
if errs := server.ListenAndServe(); errs != nil && errs != http.ErrServerClosed {
errLog.Fatalf("failed to start server at %s: %v", server.Addr, errs)
}
}
)
warm.Add(2)
go runServer(publicServer)
go runServer(observabilityServer)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
go func() {
<-quit
appLog.Println("app is shutting down...")
atomic.StoreInt32(&ready, 0)
publicServer.SetKeepAlivesEnabled(false)
observabilityServer.SetKeepAlivesEnabled(false)
time.Sleep(cfg.Server.ReadyTimeout)
c, cancel := context.WithTimeout(ctx, cfg.Server.ShutdownTimeout)
defer cancel()
if err := publicServer.Shutdown(c); err != nil {
errLog.Fatalf("failed to gracefully shutdown public server: %v", err)
}
if err := observabilityServer.Shutdown(c); err != nil {
errLog.Fatalf("failed to gracefully shutdown observability server: %v", err)
}
close(done)
}()
warm.Wait()
atomic.StoreInt32(&ready, 1)
appLog.Println("app started")
<-done
appLog.Println("app stopped")
return nil
}
func newServer(ctx context.Context, cfg *config, address string, f func(*http.ServeMux)) *http.Server {
r := http.NewServeMux()
f(r)
return &http.Server{
Addr: address,
ReadTimeout: cfg.Server.ReadTimeout,
WriteTimeout: cfg.Server.WriteTimeout,
IdleTimeout: cfg.Server.IdleTimeout,
ReadHeaderTimeout: cfg.Server.ReadHeaderTimeout,
Handler: r,
BaseContext: func(net.Listener) context.Context {
return ctx
},
}
}
const maxGoroutines = 1000
func newHealthChecks() ([2]http.Handler, error) {
l, err := health.New(health.WithChecks(
health.Config{
Name: "goroutine",
Timeout: time.Second * 5,
Check: func(_ context.Context) error {
if runtime.NumGoroutine() > maxGoroutines {
return errTooManyGoroutines
}
return nil
},
},
))
if err != nil {
return [2]http.Handler{}, fmt.Errorf("failed to set up health checks: %w", err)
}
r, err := health.New(health.WithChecks(
health.Config{
Name: "shutdown",
Timeout: time.Second,
Check: func(_ context.Context) error {
if atomic.LoadInt32(&ready) == 0 {
return errShutdown
}
return nil
},
},
))
if err != nil {
return [2]http.Handler{}, fmt.Errorf("failed to set up health checks: %w", err)
}
return [2]http.Handler{l.Handler(), r.Handler()}, nil
}
var emptyCloseFunc = func() error { return nil }
func newRateLimiter(ctx context.Context, cfg *config) (ratelimit.HandleFunc, func() error, error) {
if cfg.Debug {
return nil, emptyCloseFunc, nil
}
if cfg.Redis.Address == "" || cfg.Secrets.RedisCertificate == "" {
return nil, emptyCloseFunc, errRedisMisconfigured
}
gsm, gerr := secret.NewGoogleSecretManager(ctx, cfg.Project.ID)
if gerr != nil {
return nil, emptyCloseFunc, fmt.Errorf("failed to connect to GSM: %w", gerr)
}
defer gsm.Close()
redisCert, rerr := gsm.Get(ctx, cfg.Secrets.RedisCertificate)
if rerr != nil {
return nil, emptyCloseFunc, fmt.Errorf("failed to fetch redis certificate: %w", rerr)
}
b, _ := pem.Decode(redisCert)
if b == nil {
return nil, emptyCloseFunc, errCertificateInvalid
}
c, err := x509.ParseCertificate(b.Bytes)
if err != nil {
return nil, emptyCloseFunc, fmt.Errorf("failed to parse PEM certificate: %w", err)
}
roots := x509.NewCertPool()
roots.AddCert(c)
redisClient := redis.NewClient(&redis.Options{
Addr: cfg.Redis.Address,
DB: cfg.Redis.Database,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: roots,
},
})
if _, err := redisClient.Ping(ctx).Result(); err != nil {
return nil, emptyCloseFunc, fmt.Errorf("failed to ping redis: %w", err)
}
var (
rateLimiter = ratelimit.NewHandler(
ratelimit.NewSortedSetStrategy(redisClient),
ratelimit.KeyFromHeader("X-Forwarded-For"),
)
closeFunc = func() error {
if err := redisClient.Close(); err != nil {
return fmt.Errorf("failed to close redis client: %w", err)
}
return nil
}
)
return rateLimiter, closeFunc, nil
}