|
|
@@ -29,17 +29,23 @@ package server
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
|
|
|
+ apicontext "github.com/docker/api/context"
|
|
|
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
|
|
"google.golang.org/grpc"
|
|
|
"google.golang.org/grpc/health"
|
|
|
"google.golang.org/grpc/health/grpc_health_v1"
|
|
|
+ "google.golang.org/grpc/metadata"
|
|
|
)
|
|
|
|
|
|
// New returns a new GRPC server.
|
|
|
func New() *grpc.Server {
|
|
|
s := grpc.NewServer(
|
|
|
- grpc.UnaryInterceptor(unary),
|
|
|
+ grpc.ChainUnaryInterceptor(
|
|
|
+ unaryMeta,
|
|
|
+ unary,
|
|
|
+ ),
|
|
|
grpc.StreamInterceptor(stream),
|
|
|
)
|
|
|
hs := health.NewServer()
|
|
|
@@ -54,3 +60,16 @@ func unary(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, han
|
|
|
func stream(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
|
return grpc_prometheus.StreamServerInterceptor(srv, ss, info, handler)
|
|
|
}
|
|
|
+
|
|
|
+func unaryMeta(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
+ md, ok := metadata.FromIncomingContext(ctx)
|
|
|
+ if !ok {
|
|
|
+ return nil, errors.New("missing metadata")
|
|
|
+ }
|
|
|
+ key := md[apicontext.KEY]
|
|
|
+ if len(key) == 1 {
|
|
|
+ ctx = apicontext.WithCurrentContext(ctx, key[0])
|
|
|
+ }
|
|
|
+ m, err := handler(ctx, req)
|
|
|
+ return m, err
|
|
|
+}
|