micro/util/ctx/ctx.go
Vasiliy Tolstov 0e51a79bb6 metadata: split context to incoming and outgoing
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-09 01:08:45 +03:00

26 lines
482 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.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)
}