2019-05-30 23:52:10 +01:00
|
|
|
package ctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/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 {
|
2020-09-30 16:14:54 +03:00
|
|
|
// create needed map with specific len
|
|
|
|
md = make(metadata.Metadata, len(r.Header)+2)
|
2020-07-06 15:55:17 +03:00
|
|
|
}
|
2020-09-30 16:14:54 +03:00
|
|
|
for key, val := range r.Header {
|
|
|
|
md.Set(key, strings.Join(val, ","))
|
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)
|
|
|
|
}
|