我有個問題。是否可以通過元資料路徑從用戶請求中提取。
在這里,我有我的原型檔案和定義的方法。
rpc AllPath(google.protobuf.Empty) returns (google.protobuf.Empty) {
option (google.api.http) = {
get: "/*",
};
}
rpc Auth(google.protobuf.Empty) returns (TokenRender) {
option (google.api.http) = {
get: "/auth"
};
}
}
在我的服務器檔案中的 AllPath 函式中,我使用了類似的東西,可以在 grpc-gateway 生態系統網站上找到。
path := make(map[string]string)
if pattern, ok := runtime.HTTPPathPattern(ctx); ok {
path["pattern"] = pattern // /v1/example/login
}
fmt.Printf("Current path is: %v", path["pattern"])
但我當前的模式/路徑就像我在 proto 檔案中定義的那樣:Current path is: /*
如果有人知道如何處理這件事,我將不勝感激:)
最好的,卡珀
uj5u.com熱心網友回復:
gRPC-Gateway 通過 gRPC 元資料傳遞來自原始 HTTP 請求的各種資訊。但是,我不相信提供了原始路徑。通過注冊元資料注釋器仍然可以獲取通過的路徑。
呼叫時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 服務實作中,您可以通過傳入的背景關系檢索該值:
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,這應該記錄:
2022/10/25 15:31:42 path: /foo
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/520328.html
下一篇:如何訪問go主檔案中的子目錄?
