1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-22 03:38:41 +02:00

add grpc interceptor option (#987)

This commit is contained in:
Tony Chen 2021-06-01 19:59:35 +08:00 committed by GitHub
parent 42b60381c9
commit 29e387a39c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -61,6 +61,14 @@ func Middleware(m ...middleware.Middleware) ServerOption {
}
}
// UnaryInterceptor returns a ServerOption that sets the UnaryServerInterceptor for the
// server.
func UnaryInterceptor(in ...grpc.UnaryServerInterceptor) ServerOption {
return func(s *Server) {
s.ints = in
}
}
// Options with grpc options.
func Options(opts ...grpc.ServerOption) ServerOption {
return func(s *Server) {
@ -81,6 +89,7 @@ type Server struct {
timeout time.Duration
log *log.Helper
middleware middleware.Middleware
ints []grpc.UnaryServerInterceptor
grpcOpts []grpc.ServerOption
health *health.Server
metadata *metadata.Server
@ -101,10 +110,14 @@ func NewServer(opts ...ServerOption) *Server {
for _, o := range opts {
o(srv)
}
var ints = []grpc.UnaryServerInterceptor{
srv.unaryServerInterceptor(),
}
if len(srv.ints) > 0 {
ints = append(ints, srv.ints...)
}
var grpcOpts = []grpc.ServerOption{
grpc.ChainUnaryInterceptor(
srv.unaryServerInterceptor(),
),
grpc.ChainUnaryInterceptor(ints...),
}
if len(srv.grpcOpts) > 0 {
grpcOpts = append(grpcOpts, srv.grpcOpts...)