Last active
March 11, 2022 15:50
-
-
Save harunpeksen/072003b7c721a67250ea1b033292ae9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type wrappedStream struct { 1 | |
grpc.ServerStream | |
} | |
func newWrappedStream(s grpc.ServerStream) grpc.ServerStream { | |
return &wrappedStream{s} | |
} | |
func (w *wrappedStream) RecvMsg(m interface{}) error { | |
------ | |
return w.ServerStream.RecvMsg(m) | |
} | |
func (w *wrappedStream) SendMsg(m interface{}) error { | |
------- | |
return w.ServerStream.SendMsg(m) | |
} | |
func exampleServerStreamInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { | |
// authentication (token verification) | |
md, ok := metadata.FromIncomingContext(ss.Context()) | |
if !ok { | |
return errMissingMetadata | |
} | |
/* | |
auth logic, for instance check token in metadata | |
*/ | |
err := handler(srv, newWrappedStream(ss)) | |
return err | |
} | |
... | |
// Registering gRPC server with ServerInterceptor | |
s := grpc.NewServer(grpc.StreamInterceptor(exampleServerStreamInterceptor)) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment