micro/util/ctx/ctx.go
Vasiliy Tolstov 5eb0e56373 move all imports to own domain
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-10-02 19:55:07 +03:00

27 lines
583 B
Go

package ctx // import "go.unistack.org/micro/v3/util/ctx"
import (
"context"
"net/http"
"strings"
"go.unistack.org/micro/v3/metadata"
)
// FromRequest creates context with metadata from http.Request
func FromRequest(r *http.Request) context.Context {
ctx := r.Context()
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
md = metadata.New(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.NewIncomingContext(ctx, md)
}