Skip to content

Commit db606e0

Browse files
committed
feat(grpcs): add metadata handlers for gRPC gateway
1 parent 714c537 commit db606e0

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lava/middleware.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package lava
22

3-
import "context"
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"google.golang.org/grpc/metadata"
8+
)
9+
10+
type GrpcGatewayMetadata func(ctx context.Context, req *http.Request, rpcPath string, httpPattern string) metadata.MD
411

512
type HandlerFunc func(ctx context.Context, req Request) (Response, error)
613

servers/grpcs/server.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (s *serviceImpl) DixInject(
9292
log log.Logger,
9393
conf *Config,
9494
gw []*gateway.Mux,
95+
metadataHandlers []lava.GrpcGatewayMetadata,
9596
) {
9697
s.conf = conf
9798
if conf.HttpPort == nil {
@@ -239,11 +240,17 @@ func (s *serviceImpl) DixInject(
239240
return strings.ToUpper(s), true
240241
}),
241242
runtime.WithMetadata(func(ctx context.Context, request *http.Request) metadata.MD {
242-
path, ok := runtime.HTTPPathPattern(ctx)
243-
if !ok {
244-
return nil
243+
rpcPath, _ := runtime.RPCMethod(ctx)
244+
path, _ := runtime.HTTPPathPattern(ctx)
245+
246+
md := metadata.Pairs("http_path", path, "http_method", request.Method, "http_url", request.URL.Path)
247+
for _, h := range metadataHandlers {
248+
for k, v := range h(ctx, request, rpcPath, path) {
249+
md.Append(k, v...)
250+
}
245251
}
246-
return metadata.Pairs("http_path", path, "http_method", request.Method, "http_url", request.URL.Path)
252+
253+
return md
247254
}),
248255
runtime.WithErrorHandler(func(ctx context.Context, mux *runtime.ServeMux, marshal runtime.Marshaler, w http.ResponseWriter, request *http.Request, err error) {
249256
md, ok := runtime.ServerMetadataFromContext(ctx)

0 commit comments

Comments
 (0)