2019-05-31 01:52:10 +03: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-31 01:52:10 +03:00
|
|
|
)
|
|
|
|
|
2021-02-14 16:16:01 +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 {
|
2020-07-06 15:55:17 +03:00
|
|
|
ctx := r.Context()
|
2021-02-09 01:08:45 +03:00
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
2020-07-06 15:55:17 +03:00
|
|
|
if !ok {
|
2021-02-09 01:08:45 +03:00
|
|
|
md = metadata.New(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-31 01:52:10 +03:00
|
|
|
}
|
2020-07-06 15:55:17 +03:00
|
|
|
// pass http host
|
|
|
|
md["Host"] = r.Host
|
|
|
|
// pass http method
|
|
|
|
md["Method"] = r.Method
|
2021-02-09 01:08:45 +03:00
|
|
|
return metadata.NewIncomingContext(ctx, md)
|
2019-05-31 01:52:10 +03:00
|
|
|
}
|