mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-27 21:36:25 +00:00
add metadata forwarder server interceptors
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
parent
6e86120943
commit
4de0ec6a3b
@ -22,5 +22,5 @@ func makeTokenAuthFunc(v TokenValidator) grpc_auth.AuthFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewBearerClientInterceptors(token string) interceptors.ClientInterceptors {
|
func NewBearerClientInterceptors(token string) interceptors.ClientInterceptors {
|
||||||
return metadata.NewInterceptors("authorization", "Bearer "+token)
|
return metadata.NewInterceptors("authorization", "bearer "+token)
|
||||||
}
|
}
|
||||||
|
53
interceptors/metadata/forward.go
Normal file
53
interceptors/metadata/forward.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package metadata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
|
|
||||||
|
"go.linka.cloud/grpc/interceptors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewForwardInterceptors() interceptors.ServerInterceptors {
|
||||||
|
return &forward{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type forward struct{}
|
||||||
|
|
||||||
|
func (f *forward) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
|
||||||
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
|
if md, ok := metadata.FromIncomingContext(ctx); ok {
|
||||||
|
ctx = metadata.NewOutgoingContext(ctx, md.Copy())
|
||||||
|
}
|
||||||
|
return handler(ctx, req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *forward) StreamServerInterceptor() grpc.StreamServerInterceptor {
|
||||||
|
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||||
|
ctx := ss.Context()
|
||||||
|
md1, ok := metadata.FromIncomingContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
return handler(srv, ss)
|
||||||
|
}
|
||||||
|
o := md1.Copy()
|
||||||
|
if md2, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
|
o = metadata.Join(o, md2.Copy())
|
||||||
|
}
|
||||||
|
return handler(srv, NewContextServerStream(metadata.NewOutgoingContext(ctx, o), ss))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewContextServerStream(ctx context.Context, ss grpc.ServerStream) grpc.ServerStream {
|
||||||
|
return &ContextWrapper{ServerStream: ss, ctx: ctx}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ContextWrapper struct {
|
||||||
|
grpc.ServerStream
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ContextWrapper) Context() context.Context {
|
||||||
|
return w.ctx
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user