1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

fix api metadata extract from context (#1452)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Vasiliy Tolstov 2020-03-31 22:36:51 +03:00 committed by GitHub
parent d6bef84de0
commit 18061723bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -118,6 +118,17 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// create context
cx := ctx.FromRequest(r)
// get context from http handler wrappers
md, ok := r.Context().Value(metadata.MetadataKey{}).(metadata.Metadata)
if !ok {
md = make(metadata.Metadata)
}
// merge context with overwrite
cx = metadata.MergeContext(cx, md, true)
// set merged context to request
*r = *r.WithContext(cx)
// if stream we currently only support json
if isStream(r, service) {
@ -287,6 +298,7 @@ func requestPayload(r *http.Request) ([]byte, error) {
// allocate maximum
matches := make(map[string]string, len(md))
for k, v := range md {
// filter own keys
if strings.HasPrefix(k, "x-api-field-") {
matches[strings.TrimPrefix(k, "x-api-field-")] = v
delete(md, k)
@ -294,8 +306,8 @@ func requestPayload(r *http.Request) ([]byte, error) {
}
// restore context without fields
ctx = metadata.NewContext(ctx, md)
*r = *r.WithContext(ctx)
*r = *r.WithContext(metadata.NewContext(ctx, md))
req := make(map[string]interface{}, len(md))
for k, v := range matches {
ps := strings.Split(k, ".")

View File

@ -255,7 +255,7 @@ func (r *staticRouter) endpoint(req *http.Request) (*endpoint, error) {
}
pMatch = true
ctx := req.Context()
md, ok := metadata.FromContext(ctx)
md, ok := ctx.Value(metadata.MetadataKey{}).(metadata.Metadata)
if !ok {
md = make(metadata.Metadata)
}