1 回答

TA貢獻2019條經(jīng)驗 獲得超9個贊
gRPC-Gateway 通過 gRPC 元數(shù)據(jù)傳遞來自原始 HTTP 請求的各種信息。但是,我不相信提供了原始路徑。仍然可以通過注冊元數(shù)據(jù)注釋器來獲取傳遞的路徑。
調(diào)用時github.com/grpc-ecosystem/grpc-gateway/v2/runtime.NewServeMux(),利用WithMetadata選項 func:
mux := runtime.NewServeMux(runtime.WithMetadata(func(_ context.Context, req *http.Request) metadata.MD {
return metadata.New(map[string]string{
"grpcgateway-http-path": req.URL.Path,
})
}))
然后在您的 gRPC 服務(wù)實現(xiàn)中,您可以通過傳入的上下文檢索值:
func (s *server) AllPath(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) {
md, _ := metadata.FromIncomingContext(ctx)
log.Printf("path: %s", md["grpcgateway-http-path"][0])
return &emptypb.Empty{}, nil
}
當點擊時,例如/foo,這應(yīng)該記錄:
2022/10/25 15:31:42 path: /foo
- 1 回答
- 0 關(guān)注
- 200 瀏覽
添加回答
舉報