forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
42 lines (32 loc) · 1.07 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package auth
import (
"context"
"google.golang.org/grpc/metadata"
"github.com/infobloxopen/atlas-app-toolkit/v2/gateway"
"github.com/infobloxopen/atlas-app-toolkit/v2/requestid"
)
// OutgoingContext set to outgoing context request_id, auth_token, X-Forwarded-For, x-geo- and x-b3- headers value
func OutgoingContext(ctx context.Context) context.Context {
keys := []string{
AuthorizationHeader,
requestid.DefaultRequestIDKey,
gateway.XForwardedFor,
}
keys = append(keys, gateway.GetGeoHeaders()...)
keys = append(keys, gateway.GetXB3Headers()...)
return OutgoingContextWithCustomParams(ctx, keys...)
}
// OutgoingContextWithCustomParams set to outgoing context specified parameters from incoming context by keys
func OutgoingContextWithCustomParams(ctx context.Context, keys ...string) context.Context {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx
}
resultMD := make(metadata.MD, 0)
for _, key := range keys {
if params := md.Get(key); params != nil {
resultMD.Append(key, params...)
}
}
return metadata.NewOutgoingContext(ctx, resultMD)
}