Skip to content

Instantly share code, notes, and snippets.

@harunpeksen
Last active March 11, 2022 15:50
Show Gist options
  • Save harunpeksen/072003b7c721a67250ea1b033292ae9b to your computer and use it in GitHub Desktop.
Save harunpeksen/072003b7c721a67250ea1b033292ae9b to your computer and use it in GitHub Desktop.
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