-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
504 lines (440 loc) · 13.5 KB
/
server.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package grpcs
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/fullstorydev/grpchan/inprocgrpc"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/async"
"github.com/pubgo/funk/config"
"github.com/pubgo/funk/errors/errutil"
"github.com/pubgo/funk/generic"
"github.com/pubgo/funk/log"
"github.com/pubgo/funk/proto/errorpb"
"github.com/pubgo/funk/recovery"
"github.com/pubgo/funk/running"
"github.com/pubgo/funk/stack"
"github.com/pubgo/funk/vars"
"github.com/pubgo/funk/version"
"github.com/pubgo/lava/core/debug"
"github.com/pubgo/lava/core/lifecycle"
"github.com/pubgo/lava/core/metrics"
"github.com/pubgo/lava/core/signal"
"github.com/pubgo/lava/internal/consts"
"github.com/pubgo/lava/internal/logutil"
"github.com/pubgo/lava/internal/middlewares/middleware_accesslog"
"github.com/pubgo/lava/internal/middlewares/middleware_metric"
"github.com/pubgo/lava/internal/middlewares/middleware_recovery"
"github.com/pubgo/lava/internal/middlewares/middleware_service_info"
"github.com/pubgo/lava/lava"
"github.com/pubgo/lava/pkg/gateway"
"github.com/pubgo/lava/pkg/httputil"
"github.com/pubgo/lava/pkg/wsproxy"
"github.com/rs/xid"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
)
func New() lava.Service { return newService() }
func newService() *serviceImpl {
return &serviceImpl{
cc: new(inprocgrpc.Channel),
}
}
var _ lava.Service = (*serviceImpl)(nil)
type serviceImpl struct {
lc lifecycle.Getter
httpServer *fiber.App
grpcServer *grpc.Server
log log.Logger
cc *inprocgrpc.Channel
initList []func()
conf *Config
}
func (s *serviceImpl) Run() {
defer s.stop()
s.start()
signal.Wait()
}
func (s *serviceImpl) Start() { s.start() }
func (s *serviceImpl) Stop() { s.stop() }
func (s *serviceImpl) DixInject(
handlers []lava.GrpcRouter,
httpRouters []lava.HttpRouter,
dixMiddlewares []lava.Middleware,
getLifecycle lifecycle.Getter,
lifecycle lifecycle.Lifecycle,
metric metrics.Metric,
log log.Logger,
conf *Config,
gw []*gateway.Mux,
) {
s.conf = conf
if conf.HttpPort == nil {
conf.HttpPort = generic.Ptr(running.HttpPort)
}
if conf.GrpcPort == nil {
conf.GrpcPort = generic.Ptr(running.GrpcPort)
}
if conf.BaseUrl == "" {
conf.BaseUrl = "/" + version.Project()
}
s.lc = getLifecycle
conf = config.MergeR(defaultCfg(), conf).Unwrap()
conf.BaseUrl = "/" + strings.Trim(conf.BaseUrl, "/")
globalMiddlewares := lava.Middlewares{
middleware_service_info.New(),
middleware_metric.New(metric),
middleware_accesslog.New(log),
middleware_recovery.New(),
}
globalMiddlewares = append(globalMiddlewares, dixMiddlewares...)
log = log.WithName("grpc-server")
s.log = log
httpServer := fiber.New(fiber.Config{
EnableIPValidation: true,
EnablePrintRoutes: conf.EnablePrintRoutes,
AppName: version.Project(),
BodyLimit: 100 * 1024 * 1024,
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
if err == nil {
return nil
}
code := fiber.StatusBadRequest
errPb := errutil.ParseError(err)
if errPb == nil || errPb.Code.Code == 0 {
return nil
}
errPb.Trace.Operation = ctx.Route().Path
code = errutil.GrpcCodeToHTTP(codes.Code(errPb.Code.Code))
ctx.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSON)
return ctx.Status(code).JSON(errPb.Code)
},
})
if conf.EnableCors {
httpServer.Use(cors.New(cors.Config{
AllowOriginsFunc: func(origin string) bool {
return true
},
AllowMethods: strings.Join([]string{
fiber.MethodGet,
fiber.MethodPost,
fiber.MethodPut,
fiber.MethodDelete,
fiber.MethodPatch,
fiber.MethodHead,
fiber.MethodOptions,
}, ","),
AllowHeaders: "",
AllowCredentials: true,
ExposeHeaders: "",
MaxAge: 0,
}))
}
app := fiber.New()
app.Group("/debug", httputil.StripPrefix(filepath.Join(conf.BaseUrl, "/debug"), debug.Handler))
app.Use(handlerHttpMiddle(globalMiddlewares))
for _, h := range httpRouters {
//srv := doc.WithService()
//for _, an := range h.Annotation() {
// switch a := an.(type) {
// case *annotation.Openapi:
// if a.ServiceName != "" {
// srv.SetName(a.ServiceName)
// }
// }
//}
if h.Prefix() == "" {
panic("http handler prefix is required")
}
g := app.Group(h.Prefix(), handlerHttpMiddle(h.Middlewares()))
h.Router(g)
if m, ok := h.(lava.Close); ok {
lifecycle.BeforeStop(m.Close)
}
if m, ok := h.(lava.Init); ok {
s.initList = append(s.initList, m.Init)
}
}
for _, handler := range handlers {
//srv := doc.WithService()
//for _, an := range h.Annotation() {
// switch a := an.(type) {
// case *annotation.Openapi:
// if a.ServiceName != "" {
// srv.SetName(a.ServiceName)
// }
// }
//}
h, ok := handler.(lava.HttpRouter)
if !ok {
continue
}
if h.Prefix() == "" {
panic("http handler prefix is required")
}
g := app.Group(h.Prefix(), handlerHttpMiddle(h.Middlewares()))
h.Router(g)
if m, ok := h.(lava.Close); ok {
lifecycle.BeforeStop(m.Close)
}
if m, ok := h.(lava.Init); ok {
s.initList = append(s.initList, m.Init)
}
}
httpServer.Mount(conf.BaseUrl, app)
grpcGateway := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
EmitUnpopulated: true,
},
UnmarshalOptions: protojson.UnmarshalOptions{
DiscardUnknown: true,
},
},
}),
runtime.SetQueryParameterParser(new(DefaultQueryParser)),
runtime.WithIncomingHeaderMatcher(func(s string) (string, bool) {
return strings.ToLower(s), true
}),
runtime.WithOutgoingHeaderMatcher(func(s string) (string, bool) {
return strings.ToUpper(s), true
}),
runtime.WithMetadata(func(ctx context.Context, request *http.Request) metadata.MD {
path, ok := runtime.HTTPPathPattern(ctx)
if !ok {
return nil
}
return metadata.Pairs("http_path", path, "http_method", request.Method, "http_url", request.URL.Path)
}),
runtime.WithErrorHandler(func(ctx context.Context, mux *runtime.ServeMux, marshal runtime.Marshaler, w http.ResponseWriter, request *http.Request, err error) {
md, ok := runtime.ServerMetadataFromContext(ctx)
if ok && w != nil {
for k, v := range md.HeaderMD {
for i := range v {
w.Header().Add(k, v[i])
}
}
for k, v := range md.TrailerMD {
for i := range v {
w.Header().Add(k, v[i])
}
}
}
var pb *errorpb.ErrCode
sts, ok := status.FromError(err)
if !ok || sts == nil {
w.Header().Set("Content-Type", "application/json")
pb = &errorpb.ErrCode{
Message: err.Error(),
StatusCode: errorpb.Code_Internal,
Code: 500,
Name: "lava.grpc.status",
}
} else {
w.Header().Set("Content-Type", marshal.ContentType(sts))
if len(sts.Details()) > 0 {
if code, ok := sts.Details()[0].(*errorpb.Error); ok {
pb = code.Code
}
} else {
pb = &errorpb.ErrCode{
Message: sts.Message(),
Code: 500,
StatusCode: errorpb.Code(sts.Code()),
Name: "lava.grpc.status",
Details: sts.Proto().Details,
}
}
}
const fallback = `{"code":500, "name":"lava.grpc.status", "status_code": 13, "message": "failed to marshal error message"}`
// skip error
if pb.StatusCode == errorpb.Code_OK {
return
}
buf, mErr := marshal.Marshal(pb)
if mErr != nil {
grpclog.Infof("Failed to marshal error message %q: %v", pb, mErr)
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, fallback); err != nil {
grpclog.Infof("Failed to write response: %v", err)
}
return
}
w.WriteHeader(runtime.HTTPStatusFromCode(codes.Code(pb.StatusCode)))
if _, err := w.Write(buf); err != nil {
grpclog.Infof("Failed to write response: %v", err)
}
}),
)
mux := gateway.NewMux()
if len(gw) > 0 {
mux = gw[0]
}
srvMidMap := make(map[string][]lava.Middleware)
for _, h := range handlers {
desc := h.ServiceDesc()
assert.If(desc == nil, "desc is nil")
srvMidMap[desc.ServiceName] = append(srvMidMap[desc.ServiceName], globalMiddlewares...)
srvMidMap[desc.ServiceName] = append(srvMidMap[desc.ServiceName], h.Middlewares()...)
if m, ok := h.(lava.Close); ok {
lifecycle.BeforeStop(m.Close)
}
if m, ok := h.(lava.Initializer); ok {
s.initList = append(s.initList, m.Initialize)
}
if m, ok := h.(lava.Init); ok {
s.initList = append(s.initList, m.Init)
}
mux.RegisterService(desc, h)
s.cc.RegisterService(desc, h)
if m, ok := h.(lava.GrpcGatewayRouter); ok {
assert.Exit(m.RegisterGateway(context.Background(), grpcGateway, s.cc))
}
}
mux.SetUnaryInterceptor(handlerUnaryMiddle(srvMidMap))
mux.SetStreamInterceptor(handlerStreamMiddle(srvMidMap))
s.cc = s.cc.WithServerUnaryInterceptor(handlerUnaryMiddle(srvMidMap))
s.cc = s.cc.WithServerStreamInterceptor(handlerStreamMiddle(srvMidMap))
// grpc server初始化
grpcServer := conf.GrpcConfig.Build(
grpc.ChainUnaryInterceptor(handlerUnaryMiddle(srvMidMap)),
grpc.ChainStreamInterceptor(handlerStreamMiddle(srvMidMap)),
).Expect("failed to build grpc server")
for _, h := range handlers {
grpcServer.RegisterService(h.ServiceDesc(), h)
}
apiPrefix1 := assert.Must1(url.JoinPath(conf.BaseUrl, "gw"))
s.log.Info().Str("path", apiPrefix1).Msg("service grpc gateway base path")
httpServer.Group(apiPrefix1, httputil.StripPrefix(apiPrefix1, mux.Handler))
for _, m := range mux.GetRouteMethods() {
log.Info().
Str("method-name", m.GrpcMethodName).
Str("http-method", m.HttpMethod).
Str("http-path", apiPrefix1+"/"+strings.Join(m.HttpPath, "/")).
Str("verb", m.Verb).
Str("req-field-path", m.RequestBodyFieldPath).
Str("rsp-field-path", m.ResponseBodyFieldPath).
Any("path-vars", m.Vars).
Msg("grpc gateway method router")
}
apiPrefix := assert.Must1(url.JoinPath(conf.BaseUrl, "api"))
s.log.Info().Str("path", apiPrefix).Msg("service grpc gateway base path")
httpServer.Group(apiPrefix, httputil.HTTPHandler(http.StripPrefix(apiPrefix, wsproxy.WebsocketProxy(grpcGateway,
wsproxy.WithPingPong(conf.EnablePingPong),
wsproxy.WithTimeWait(conf.PingPongTime),
))))
s.httpServer = httpServer
s.grpcServer = grpcServer
vars.RegisterValue(fmt.Sprintf("%s-grpc-server-config-%s", version.Project(), xid.New()), &conf)
// vars.RegisterValue(fmt.Sprintf("%s-grpc-server-router-%s", version.Project(), xid.New()), mux.App().Stack())
}
func (s *serviceImpl) start() {
defer recovery.Exit()
logutil.OkOrFailed(s.log, "running before service starts", func() error {
defer recovery.Exit()
for _, run := range s.lc.GetBeforeStarts() {
s.log.Info().Msgf("running %s", stack.CallerWithFunc(run.Handler))
run.Handler()
}
return nil
})
logutil.OkOrFailed(s.log, "init handler before service starts", func() error {
defer recovery.Exit()
for _, ii := range s.initList {
s.log.Info().Msgf("init handler %s", stack.CallerWithFunc(ii))
ii()
}
return nil
})
s.log.Info().
Int("grpc-port", *s.conf.GrpcPort).
Int("http-port", *s.conf.HttpPort).
Msg("create network listener")
grpcLn := assert.Must1(net.Listen("tcp", fmt.Sprintf(":%d", *s.conf.GrpcPort)))
httpLn := assert.Must1(net.Listen("tcp", fmt.Sprintf(":%d", *s.conf.HttpPort)))
logutil.OkOrFailed(s.log, "service starts", func() error {
// 启动grpc服务
async.GoDelay(func() error {
s.log.Info().Msg("[grpc] Server Starting")
logutil.LogOrErr(s.log, "[grpc] Server Stop", func() error {
defer recovery.Exit()
if err := s.grpcServer.Serve(grpcLn); err != nil &&
!errors.Is(err, http.ErrServerClosed) &&
!errors.Is(err, net.ErrClosed) {
return err
}
return nil
})
return nil
})
// 启动grpc网关
async.GoDelay(func() error {
s.log.Info().Msg("[http] Server Starting")
logutil.LogOrErr(s.log, "[http] Server Stop", func() error {
defer recovery.Exit()
if err := s.httpServer.Listener(httpLn); err != nil &&
!errors.Is(err, http.ErrServerClosed) &&
!errors.Is(err, net.ErrClosed) {
return err
}
return nil
})
return nil
})
return nil
})
logutil.OkOrFailed(s.log, "running after service starts", func() error {
for _, run := range s.lc.GetAfterStarts() {
logutil.LogOrErr(
s.log,
fmt.Sprintf("running %s", stack.CallerWithFunc(run.Handler)),
func() error { run.Handler(); return nil },
)
}
return nil
})
}
func (s *serviceImpl) stop() {
defer recovery.Exit()
logutil.OkOrFailed(s.log, "running before service stops", func() error {
for _, run := range s.lc.GetBeforeStops() {
logutil.LogOrErr(
s.log,
fmt.Sprintf("running %s", stack.CallerWithFunc(run.Handler)),
func() error { run.Handler(); return nil },
)
}
return nil
})
logutil.LogOrErr(s.log, "[grpc] Server GracefulStop", func() error {
s.grpcServer.GracefulStop()
return nil
})
logutil.LogOrErr(s.log, "[http] Server Shutdown", func() error {
return s.httpServer.ShutdownWithTimeout(consts.DefaultTimeout)
})
logutil.OkOrFailed(s.log, "running after service stops", func() error {
for _, run := range s.lc.GetAfterStops() {
logutil.LogOrErr(
s.log,
fmt.Sprintf("running %s", stack.CallerWithFunc(run.Handler)),
func() error { run.Handler(); return nil },
)
}
return nil
})
}