github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/middleware/servicespecific/servicespecific.go (about) 1 package servicespecific 2 3 import ( 4 "context" 5 6 "google.golang.org/grpc" 7 ) 8 9 // ExtraUnaryInterceptor is an interface for a service which has its own bundled 10 // unary interceptors that must be run. 11 type ExtraUnaryInterceptor interface { 12 UnaryInterceptor() grpc.UnaryServerInterceptor 13 } 14 15 // ExtraStreamInterceptor is an interface for a service which has its own bundled 16 // stream interceptors that must be run. 17 type ExtraStreamInterceptor interface { 18 StreamInterceptor() grpc.StreamServerInterceptor 19 } 20 21 // UnaryServerInterceptor returns a new unary server interceptor that runs bundled interceptors. 22 func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { 23 if hasExtraInterceptor, ok := info.Server.(ExtraUnaryInterceptor); ok { 24 interceptor := hasExtraInterceptor.UnaryInterceptor() 25 return interceptor(ctx, req, info, handler) 26 } 27 28 return handler(ctx, req) 29 } 30 31 // StreamServerInterceptor returns a new stream server interceptor that runs bundled interceptors. 32 func StreamServerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { 33 if hasExtraInterceptor, ok := srv.(ExtraStreamInterceptor); ok { 34 interceptor := hasExtraInterceptor.StreamInterceptor() 35 return interceptor(srv, stream, info, handler) 36 } 37 38 return handler(srv, stream) 39 }