micro/util/ctx/ctx.go
Vasiliy Tolstov 9c55b1d06a fix metadata issues with uppercase letters
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-09-30 16:14:54 +03:00

27 lines
516 B
Go

package ctx
import (
"context"
"net/http"
"strings"
"github.com/unistack-org/micro/v3/metadata"
)
func FromRequest(r *http.Request) context.Context {
ctx := r.Context()
md, ok := metadata.FromContext(ctx)
if !ok {
// create needed map with specific len
md = make(metadata.Metadata, len(r.Header)+2)
}
for key, val := range r.Header {
md.Set(key, strings.Join(val, ","))
}
// pass http host
md["Host"] = r.Host
// pass http method
md["Method"] = r.Method
return metadata.NewContext(ctx, md)
}