/
middleware.go
44 lines (36 loc) · 1.23 KB
/
middleware.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
package main
import (
"fmt"
"runtime/debug"
zlog "github.com/rs/zerolog/log"
"google.golang.org/grpc"
grpcmdlw "github.com/grpc-ecosystem/go-grpc-middleware"
grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpcctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpcprom "github.com/grpc-ecosystem/go-grpc-prometheus"
)
func StdUnaryMiddleware(interceptors ...grpc.UnaryServerInterceptor) grpc.ServerOption {
arr := []grpc.UnaryServerInterceptor{
grpcctxtags.UnaryServerInterceptor(),
grpcprom.UnaryServerInterceptor,
grpcrecovery.UnaryServerInterceptor(grpcrecovery.WithRecoveryHandler(func(i interface{}) error {
zlog.Error().Str("stack", string(debug.Stack())).Msg("gRPC handler panicked")
return fmt.Errorf("%#v", i)
})),
}
arr = append(arr, interceptors...)
return grpc.UnaryInterceptor(
grpcmdlw.ChainUnaryServer(arr...),
)
}
func StdStreamMiddleware(interceptors ...grpc.StreamServerInterceptor) grpc.ServerOption {
arr := []grpc.StreamServerInterceptor{
grpcctxtags.StreamServerInterceptor(),
grpcprom.StreamServerInterceptor,
grpcrecovery.StreamServerInterceptor(),
}
arr = append(arr, interceptors...)
return grpc.StreamInterceptor(
grpcmdlw.ChainStreamServer(arr...),
)
}