mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 09:12:28 +00:00
add metadata interceptors, auth client interceptors
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
@ -1,48 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type mdInterceptors struct {
|
||||
k, v string
|
||||
}
|
||||
|
||||
func (i mdInterceptors) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
|
||||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||
if err := grpc.SetHeader(ctx, metadata.Pairs(i.k, i.v)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler(ctx, req)
|
||||
}
|
||||
}
|
||||
|
||||
func (i mdInterceptors) StreamServerInterceptor() grpc.StreamServerInterceptor {
|
||||
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
||||
if err := grpc.SetHeader(ss.Context(), metadata.Pairs(i.k, i.v)); err != nil {
|
||||
return err
|
||||
}
|
||||
return handler(srv, ss)
|
||||
}
|
||||
}
|
||||
|
||||
func (i mdInterceptors) UnaryClientInterceptor() grpc.UnaryClientInterceptor {
|
||||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||
if err := grpc.SetHeader(ctx, metadata.Pairs(i.k, i.v)); err != nil {
|
||||
return err
|
||||
}
|
||||
return invoker(ctx, method, req, reply, cc, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
func (i mdInterceptors) StreamClientInterceptor() grpc.StreamClientInterceptor {
|
||||
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
||||
if err := grpc.SetHeader(ctx, metadata.Pairs(i.k, i.v)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return streamer(ctx, desc, cc, method, opts...)
|
||||
}
|
||||
}
|
@ -28,6 +28,7 @@ import (
|
||||
"google.golang.org/grpc/health/grpc_health_v1"
|
||||
greflect "google.golang.org/grpc/reflection"
|
||||
|
||||
"go.linka.cloud/grpc/interceptors/metadata"
|
||||
"go.linka.cloud/grpc/registry"
|
||||
"go.linka.cloud/grpc/registry/noop"
|
||||
)
|
||||
@ -74,32 +75,18 @@ func newService(opts ...Option) (*service, error) {
|
||||
f(s.opts)
|
||||
}
|
||||
if s.opts.name != "" {
|
||||
s.opts.unaryServerInterceptors = append([]grpc.UnaryServerInterceptor{mdInterceptors{
|
||||
k: "grpc-service-name", v: s.opts.name,
|
||||
}.UnaryServerInterceptor()}, s.opts.unaryServerInterceptors...)
|
||||
s.opts.unaryClientInterceptors = append([]grpc.UnaryClientInterceptor{mdInterceptors{
|
||||
k: "grpc-service-name", v: s.opts.name,
|
||||
}.UnaryClientInterceptor()}, s.opts.unaryClientInterceptors...)
|
||||
s.opts.streamServerInterceptors = append([]grpc.StreamServerInterceptor{mdInterceptors{
|
||||
k: "grpc-service-name", v: s.opts.name,
|
||||
}.StreamServerInterceptor()}, s.opts.streamServerInterceptors...)
|
||||
s.opts.streamClientInterceptors = append([]grpc.StreamClientInterceptor{mdInterceptors{
|
||||
k: "grpc-service-name", v: s.opts.name,
|
||||
}.StreamClientInterceptor()}, s.opts.streamClientInterceptors...)
|
||||
i := metadata.NewInterceptors("grpc-service-name", s.opts.name)
|
||||
s.opts.unaryServerInterceptors = append([]grpc.UnaryServerInterceptor{i.UnaryServerInterceptor()}, s.opts.unaryServerInterceptors...)
|
||||
s.opts.unaryClientInterceptors = append([]grpc.UnaryClientInterceptor{i.UnaryClientInterceptor()}, s.opts.unaryClientInterceptors...)
|
||||
s.opts.streamServerInterceptors = append([]grpc.StreamServerInterceptor{i.StreamServerInterceptor()}, s.opts.streamServerInterceptors...)
|
||||
s.opts.streamClientInterceptors = append([]grpc.StreamClientInterceptor{i.StreamClientInterceptor()}, s.opts.streamClientInterceptors...)
|
||||
}
|
||||
if s.opts.version != "" {
|
||||
s.opts.unaryServerInterceptors = append([]grpc.UnaryServerInterceptor{mdInterceptors{
|
||||
k: "grpc-service-version", v: s.opts.version,
|
||||
}.UnaryServerInterceptor()}, s.opts.unaryServerInterceptors...)
|
||||
s.opts.unaryClientInterceptors = append([]grpc.UnaryClientInterceptor{mdInterceptors{
|
||||
k: "grpc-service-version", v: s.opts.version,
|
||||
}.UnaryClientInterceptor()}, s.opts.unaryClientInterceptors...)
|
||||
s.opts.streamServerInterceptors = append([]grpc.StreamServerInterceptor{mdInterceptors{
|
||||
k: "grpc-service-version", v: s.opts.version,
|
||||
}.StreamServerInterceptor()}, s.opts.streamServerInterceptors...)
|
||||
s.opts.streamClientInterceptors = append([]grpc.StreamClientInterceptor{mdInterceptors{
|
||||
k: "grpc-service-version", v: s.opts.version,
|
||||
}.StreamClientInterceptor()}, s.opts.streamClientInterceptors...)
|
||||
i := metadata.NewInterceptors("grpc-service-version", s.opts.version)
|
||||
s.opts.unaryServerInterceptors = append([]grpc.UnaryServerInterceptor{i.UnaryServerInterceptor()}, s.opts.unaryServerInterceptors...)
|
||||
s.opts.unaryClientInterceptors = append([]grpc.UnaryClientInterceptor{i.UnaryClientInterceptor()}, s.opts.unaryClientInterceptors...)
|
||||
s.opts.streamServerInterceptors = append([]grpc.StreamServerInterceptor{i.StreamServerInterceptor()}, s.opts.streamServerInterceptors...)
|
||||
s.opts.streamClientInterceptors = append([]grpc.StreamClientInterceptor{i.StreamClientInterceptor()}, s.opts.streamClientInterceptors...)
|
||||
}
|
||||
if s.opts.mux == nil {
|
||||
s.opts.mux = http.NewServeMux()
|
||||
|
Reference in New Issue
Block a user