Vasiliy Tolstov
97ae2979ad
http middleware can add additional metadata to context, for example tracing wrappers, pass down it to underlining services Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
27 lines
494 B
Go
27 lines
494 B
Go
package ctx
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/textproto"
|
|
"strings"
|
|
|
|
"github.com/micro/go-micro/v2/metadata"
|
|
)
|
|
|
|
func FromRequest(r *http.Request) context.Context {
|
|
ctx := r.Context()
|
|
md, ok := metadata.FromContext(ctx)
|
|
if !ok {
|
|
md = make(metadata.Metadata)
|
|
}
|
|
for k, v := range r.Header {
|
|
md[textproto.CanonicalMIMEHeaderKey(k)] = strings.Join(v, ",")
|
|
}
|
|
// pass http host
|
|
md["Host"] = r.Host
|
|
// pass http method
|
|
md["Method"] = r.Method
|
|
return metadata.NewContext(ctx, md)
|
|
}
|