|
|
|
@ -1,14 +1,12 @@
|
|
|
|
|
package context |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"github.com/go-kratos/kratos/v2/metadata" |
|
|
|
|
"golang.org/x/net/context" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func SetContext(ctx context.Context, key, value string) context.Context { |
|
|
|
|
ctx = context.WithValue(ctx, key, value) |
|
|
|
|
fmt.Println(key, " set:", value) |
|
|
|
|
return metadata.AppendToClientContext(ctx, key, value) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -16,12 +14,10 @@ func GetContext(ctx context.Context, key string) string {
|
|
|
|
|
if ctx == nil { |
|
|
|
|
return "" |
|
|
|
|
} |
|
|
|
|
//fmt.Println(key, " value:", ctx.Value(key))
|
|
|
|
|
if ctx.Value(key) != nil { |
|
|
|
|
return ctx.Value(key).(string) |
|
|
|
|
} |
|
|
|
|
if md, ok := metadata.FromServerContext(ctx); ok { |
|
|
|
|
fmt.Println(key, " metadata:", md.Get(key)) |
|
|
|
|
return md.Get(key) |
|
|
|
|
} |
|
|
|
|
return "" |
|
|
|
|