micro/util/ctx/ctx.go

27 lines
516 B
Go
Raw Normal View History

2019-05-31 01:52:10 +03:00
package ctx
import (
"context"
"net/http"
"strings"
"github.com/unistack-org/micro/v3/metadata"
2019-05-31 01:52:10 +03:00
)
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, ","))
2019-05-31 01:52:10 +03:00
}
// pass http host
md["Host"] = r.Host
// pass http method
md["Method"] = r.Method
2019-05-31 01:52:10 +03:00
return metadata.NewContext(ctx, md)
}