-
Notifications
You must be signed in to change notification settings - Fork 12
/
webserver.factory.go
419 lines (372 loc) · 15 KB
/
webserver.factory.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webserver
import (
"context"
"crypto/tls"
"fmt"
"log"
"log/slog"
"math"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime/debug"
"strings"
"time"
"github.com/gin-gonic/gin"
grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery"
"github.com/rs/cors"
slog_ "github.com/searKing/golang/go/log/slog"
net_ "github.com/searKing/golang/go/net"
"github.com/searKing/golang/pkg/webserver/healthz"
gin_ "github.com/searKing/golang/third_party/github.com/gin-gonic/gin"
grpc_ "github.com/searKing/golang/third_party/github.com/grpc-ecosystem/grpc-gateway-v2/grpc"
"github.com/searKing/golang/third_party/google.golang.org/grpc/interceptors/burstlimit"
"github.com/searKing/golang/third_party/google.golang.org/grpc/interceptors/timeoutlimit"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// FactoryConfigFunc is an alias for a function that will take in a pointer to an FactoryConfig and modify it
type FactoryConfigFunc func(os *FactoryConfig) error
// FactoryConfig Config of Factory
type FactoryConfig struct {
// Name is the human-readable server name, optional
Name string
// BindAddress is the host port to bind to (local internet)
// Will default to a value based on secure serving info and available ipv4 IPs.
BindAddress string
// ExternalAddress is the address advertised, even if BindAddress is a loopback. By default, this
// is set to BindAddress if the later no loopback, or to the first host interface address.
ExternalAddress string
// ShutdownDelayDuration allows to block shutdown for some time, e.g. until endpoints pointing to this API server
// have converged on all node. During this time, the API server keeps serving, /healthz will return 200,
// but /readyz will return failure.
ShutdownDelayDuration time.Duration
TlsConfig *tls.Config
Cors cors.Options // for cors
ForceDisableTls bool // disable tls
LocalIpResolver *LocalIpResolver // for resolve local ip to expose, used if advertise_addr is empty
NoGrpcProxy bool // disable http proxy for grpc client to connect grpc server
PreferRegisterHTTPFromEndpoint bool // prefer register http handler from endpoint
// grpc middlewares
MaxConcurrencyUnary int // for concurrent parallel requests of unary server, The default is 0 (no limit is given)
MaxConcurrencyStream int // for concurrent parallel requests of stream server, The default is 0 (no limit is given)
BurstLimitTimeoutUnary time.Duration // for concurrent parallel requests of unary server, The default is 0 (no limit is given)
BurstLimitTimeoutStream time.Duration // for concurrent parallel requests of stream server, The default is 0 (no limit is given)
HandledTimeoutUnary time.Duration // for max handing time of unary server, The default is 0 (no limit is given)
HandledTimeoutStream time.Duration // for max handing time of unary server, The default is 0 (no limit is given)
MaxReceiveMessageSizeInBytes int // sets the maximum message size in bytes the grpc server can receive, The default is 0 (no limit is given).
MaxSendMessageSizeInBytes int // sets the maximum message size in bytes the grpc server can send, The default is 0 (no limit is given).
// Deprecated: takes no effect, use slog instead.
EnableLogrusMiddleware bool // disable logrus middleware
GatewayOptions []grpc_.GatewayOption
GinMiddlewares []gin.HandlerFunc
}
// SetDefaults sets sensible values for unset fields in config. This is
// exported for testing: Configs passed to repository functions are copied and have
// default values set automatically.
func (fc *FactoryConfig) SetDefaults() {
fc.Cors = cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{
http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete},
AllowedHeaders: []string{"*"},
}
}
// Validate inspects the fields of the type to determine if they are valid.
func (fc *FactoryConfig) Validate() error {
return nil
}
type Factory struct {
// it's better to keep FactoryConfig as a private attribute,
// thanks to that we are always sure that our configuration is not changed in the not allowed way
fc FactoryConfig
}
func NewFactory(fc FactoryConfig, configs ...FactoryConfigFunc) (Factory, error) {
// Apply all Configurations passed in
for _, config := range configs {
// Pass the FactoryConfig into the configuration function
err := config(&fc)
if err != nil {
return Factory{}, fmt.Errorf("failed to apply configuration function: %w", err)
}
}
if err := fc.Validate(); err != nil {
return Factory{}, fmt.Errorf("invalid config passed to factory: %w", err)
}
f := Factory{fc: fc}
return f, nil
}
func (f *Factory) Config() FactoryConfig {
return f.fc
}
// New creates a new server which logically combines the handling chain with the passed server.
// name is used to differentiate for logging. The handler chain in particular can be difficult as it starts delgating.
func (f *Factory) New() (*WebServer, error) {
f.fc.BindAddress = f.GetBackendBindHostPort()
f.fc.ExternalAddress = f.GetBackendServeHostPort(true)
// if there is no port, and we listen on one securely, use that one
if _, _, err := net.SplitHostPort(f.fc.ExternalAddress); err != nil {
if f.fc.BindAddress == "" {
slog.Error("cannot derive external address port without listening on a secure port.", slog_.Error(err))
os.Exit(1)
}
_, port, err := net.SplitHostPort(f.fc.BindAddress)
if err != nil {
slog.Error("cannot derive external address from the secure port", slog_.Error(err))
os.Exit(1)
}
f.fc.ExternalAddress = net.JoinHostPort(f.fc.ExternalAddress, port)
}
opts := grpc_.WithDefault()
if f.fc.NoGrpcProxy {
opts = append(opts, grpc_.WithGrpcDialOption(grpc.WithNoProxy()))
}
{
// 设置GRPC最大消息大小
opts = append(opts, grpc_.WithGrpcDialOption(grpc.WithNoProxy()))
// http -> grpc client -> grpc server
if f.fc.MaxReceiveMessageSizeInBytes > 0 {
opts = append(opts, grpc_.WithGrpcServerOption(grpc.MaxRecvMsgSize(f.fc.MaxReceiveMessageSizeInBytes)))
opts = append(opts, grpc_.WithGrpcDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(f.fc.MaxReceiveMessageSizeInBytes))))
} else {
opts = append(opts, grpc_.WithGrpcServerOption(grpc.MaxRecvMsgSize(defaultMaxReceiveMessageSize)))
opts = append(opts, grpc_.WithGrpcDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaultMaxReceiveMessageSize))))
}
// http <- grpc client <- grpc server
if f.fc.MaxSendMessageSizeInBytes > 0 {
opts = append(opts, grpc_.WithGrpcServerOption(grpc.MaxSendMsgSize(f.fc.MaxSendMessageSizeInBytes)))
opts = append(opts, grpc_.WithGrpcDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(f.fc.MaxSendMessageSizeInBytes))))
} else {
opts = append(opts, grpc_.WithGrpcServerOption(grpc.MaxSendMsgSize(defaultMaxSendMessageSize)))
opts = append(opts, grpc_.WithGrpcDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaultMaxSendMessageSize))))
}
}
{
// recover
opts = append(opts, grpc_.WithGrpcUnaryServerChain(grpcrecovery.UnaryServerInterceptor(grpcrecovery.WithRecoveryHandler(func(p any) (err error) {
slog.Error("recovered in grpc", slog_.Error(status.Errorf(codes.Internal, "%s at %s", p, debug.Stack())))
{
_, _ = os.Stderr.Write([]byte(fmt.Sprintf("panic: %s", p)))
debug.PrintStack()
_, _ = os.Stderr.Write([]byte(" [recovered]"))
_, _ = os.Stderr.Write([]byte("\n"))
}
return status.Errorf(codes.Internal, "%s", p)
}))))
opts = append(opts, grpc_.WithGrpcStreamServerChain(grpcrecovery.StreamServerInterceptor(grpcrecovery.WithRecoveryHandler(func(p any) (err error) {
slog.Error("recovered in grpc", slog_.Error(status.Errorf(codes.Internal, "%s at %s", p, debug.Stack())))
{
_, _ = os.Stderr.Write([]byte(fmt.Sprintf("panic: %s", p)))
debug.PrintStack()
_, _ = os.Stderr.Write([]byte(" [recovered]"))
_, _ = os.Stderr.Write([]byte("\n"))
}
return status.Errorf(codes.Internal, "%s", p)
}))))
}
{
// handle request timeout
opts = append(opts, grpc_.WithGrpcUnaryServerChain(
timeoutlimit.UnaryServerInterceptor(f.fc.HandledTimeoutUnary)))
opts = append(opts, grpc_.WithGrpcStreamServerChain(
timeoutlimit.StreamServerInterceptor(f.fc.HandledTimeoutStream)))
}
{
// burst limit
opts = append(opts, grpc_.WithGrpcUnaryServerChain(
burstlimit.UnaryServerInterceptor(f.fc.MaxConcurrencyUnary, f.fc.BurstLimitTimeoutUnary)))
opts = append(opts, grpc_.WithGrpcStreamServerChain(
burstlimit.StreamServerInterceptor(f.fc.MaxConcurrencyStream, f.fc.BurstLimitTimeoutStream)))
}
// cors
opts = append(opts, grpc_.WithHttpWrapper(cors.New(f.fc.Cors).Handler))
opts = append(opts, f.fc.GatewayOptions...)
opts = append(opts, grpc_.WithSlogLoggerConfig(slog.Default().Handler(), grpc_.ExtractLoggingOptions(opts...))...)
grpcBackend := grpc_.NewGatewayTLS(f.fc.BindAddress, f.fc.TlsConfig, opts...)
grpcBackend.ApplyOptions()
{
l := slog.NewLogLogger(slog.Default().Handler(), slog.LevelError)
l.SetFlags(log.Lshortfile)
grpcBackend.ErrorLog = l
}
ginBackend := gin.New()
{
l := slog.NewLogLogger(slog.Default().Handler(), slog.LevelInfo)
l.SetFlags(log.Lshortfile)
ginBackend.Use(gin.LoggerWithConfig(gin.LoggerConfig{
Formatter: GinLogFormatter("GIN over HTTP"),
Output: l.Writer(),
}))
}
ginBackend.Use(gin_.RecoveryWithWriter(grpcBackend.ErrorLog.Writer()))
ginBackend.Use(gin_.UseHTTPPreflight())
ginBackend.Use(f.fc.GinMiddlewares...)
defaultHealthChecks := []healthz.HealthChecker{healthz.PingHealthzCheck, healthz.LogHealthCheck}
s := &WebServer{
Name: f.fc.Name,
BindAddress: f.fc.BindAddress,
ExternalAddress: f.fc.ExternalAddress,
PreferRegisterHTTPFromEndpoint: f.fc.PreferRegisterHTTPFromEndpoint,
ShutdownDelayDuration: f.fc.ShutdownDelayDuration,
grpcBackend: grpcBackend,
ginBackend: ginBackend,
postStartHooks: map[string]postStartHookEntry{},
preShutdownHooks: map[string]preShutdownHookEntry{},
healthzChecks: defaultHealthChecks,
livezChecks: defaultHealthChecks,
readyzChecks: defaultHealthChecks,
readinessStopCh: make(chan struct{}),
}
err := s.AddBootSequencePostStartHook("__bind_addr__", func(ctx context.Context) error {
host, port, err := net.SplitHostPort(s.ExternalAddress)
if err != nil {
return fmt.Errorf("malformed external address: %w", err)
}
_, bindPort, err := net.SplitHostPort(s.grpcBackend.BindAddr())
if err != nil {
return fmt.Errorf("malformed bind address: %w", err)
}
if bindPort == "" || bindPort == "0" {
return nil
}
if port == "0" {
logger := slog.With("old_external_address", s.ExternalAddress).
With("bind_address", s.BindAddress).
With("bind_port", bindPort)
s.ExternalAddress = net.JoinHostPort(host, bindPort)
logger.With("new_external_address", s.ExternalAddress).
Info("update external address with bind port")
}
return nil
})
if err != nil {
return nil, err
}
return s, nil
}
// ClientMaxReceiveMessageSize use 4GB as the default message size limit.
// grpc library default is 4MB
var defaultMaxReceiveMessageSize = math.MaxInt32 // 1024 * 1024 * 4
var defaultMaxSendMessageSize = math.MaxInt32
type Net struct {
Host string
Domains []string // service name to register to consul for dns
Port int32
}
// CertKey a public/private key pair
type CertKey struct {
Cert string // public key, containing a PEM-encoded certificate, and possibly the complete certificate chain
Key string // private key, containing a PEM-encoded private key for the certificate specified by CertFile
}
//type CertKey struct {
// // CertFile is a file containing a PEM-encoded certificate, and possibly the complete certificate chain
// CertFile string
// // KeyFile is a file containing a PEM-encoded private key for the certificate specified by CertFile
// KeyFile string
//}
type TLS struct {
Enable bool
KeyPairBase64 *CertKey // key pair in base64 format encoded from pem
KeyPairPath *CertKey // key pair stored in file from pem
// service_name is used to verify the hostname on the returned
// certificates unless InsecureSkipVerify is given. It is also included
// in the client's handshake to support virtual hosting unless it is
// an IP address.
ServiceName string
AllowedTlsCidrs []string //"127.0.0.1/24"
WhitelistedPaths []string
}
type LocalIpResolver struct {
Networks []string
Addresses []string
Timeout time.Duration
}
func (f *Factory) HTTPScheme() string {
if f.fc.ForceDisableTls {
return "http"
}
return "https"
}
func (f *Factory) ResolveLocalIp() string {
resolver := f.fc.LocalIpResolver
if resolver != nil {
ip, err := net_.ServeIP(resolver.Networks, resolver.Addresses, resolver.Timeout)
if err != nil && len(ip) > 0 {
return ip.String()
}
}
// use local ip
localIP, err := net_.ListenIP()
if err == nil && len(localIP) > 0 {
return localIP.String()
}
return "localhost"
}
// GetBackendBindHostPort returns a address to listen.
func (f *Factory) GetBackendBindHostPort() string {
host, port, _ := net_.SplitHostPort(f.fc.BindAddress)
return getHostPort(host, port)
}
// GetBackendExternalHostPort returns an address to expose with domain, if not set, use host instead.
func (f *Factory) GetBackendExternalHostPort() string {
host, port, _ := net_.SplitHostPort(f.fc.ExternalAddress)
if host == "" {
return f.GetBackendBindHostPort()
}
return getHostPort(host, port)
}
// GetBackendServeHostPort returns an address to expose without domain, if not set, use resolver to resolve an ip
func (f *Factory) GetBackendServeHostPort(external bool) string {
if external {
host, _, _ := net_.SplitHostPort(f.fc.ExternalAddress)
if host != "" {
return f.GetBackendExternalHostPort()
}
}
host, port, _ := net_.SplitHostPort(f.fc.BindAddress)
if host != "" && host != "0.0.0.0" {
return f.GetBackendBindHostPort()
}
return getHostPort(f.ResolveLocalIp(), port)
}
func (f *Factory) ResolveBackendLocalUrl(relativePaths ...string) string {
return resolveLocalUrl(
f.HTTPScheme(),
f.GetBackendServeHostPort(true),
filepath.Join(relativePaths...)).String()
}
func getHostPort(hostname string, port string) string {
if strings.HasPrefix(hostname, "unix:") {
return hostname
}
return net.JoinHostPort(hostname, port)
}
func resolveLocalUrl(scheme, hostport, path string) *url.URL {
u := &url.URL{
Scheme: scheme,
Host: hostport,
Path: path,
}
if u.Hostname() == "" {
// use local host
localHost := "localhost"
// use local ip
localIP, err := net_.ListenIP()
if err == nil && len(localIP) > 0 {
localHost = localIP.String()
}
u.Host = net.JoinHostPort(localHost, u.Port())
}
return u
}