2019-05-30 23:52:10 +01:00
|
|
|
package ctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2020-07-06 15:55:17 +03:00
|
|
|
"net/textproto"
|
2019-05-30 23:52:10 +01:00
|
|
|
"strings"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/metadata"
|
2019-05-30 23:52:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func FromRequest(r *http.Request) context.Context {
|
2020-07-06 15:55:17 +03:00
|
|
|
ctx := r.Context()
|
|
|
|
md, ok := metadata.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = make(metadata.Metadata)
|
|
|
|
}
|
2019-05-30 23:52:10 +01:00
|
|
|
for k, v := range r.Header {
|
2020-07-06 15:55:17 +03:00
|
|
|
md[textproto.CanonicalMIMEHeaderKey(k)] = strings.Join(v, ",")
|
2019-05-30 23:52:10 +01:00
|
|
|
}
|
2020-07-06 15:55:17 +03:00
|
|
|
// pass http host
|
|
|
|
md["Host"] = r.Host
|
|
|
|
// pass http method
|
|
|
|
md["Method"] = r.Method
|
2019-05-30 23:52:10 +01:00
|
|
|
return metadata.NewContext(ctx, md)
|
|
|
|
}
|