micro/util/ctx/ctx.go

27 lines
583 B
Go
Raw Normal View History

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