109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package context
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// 定义Context键的类型,避免键冲突
|
|
type contextKey string
|
|
|
|
const (
|
|
// LanguageKey 存储语言信息的上下文键
|
|
LanguageKey contextKey = "language"
|
|
// RequestIDKey 存储请求ID的上下文键
|
|
RequestIDKey contextKey = "request_id"
|
|
// UserIDKey 存储用户ID的上下文键
|
|
UserIDKey contextKey = "user_id"
|
|
// TraceIDKey 存储链路追踪ID的上下文键
|
|
TraceIDKey contextKey = "trace_id"
|
|
)
|
|
|
|
// DefaultLanguage 默认语言
|
|
const DefaultLanguage = "zh-CN"
|
|
|
|
// WithLanguage 将语言信息添加到Context中
|
|
func WithLanguage(ctx context.Context, lang string) context.Context {
|
|
if lang == "" {
|
|
lang = DefaultLanguage
|
|
}
|
|
return context.WithValue(ctx, LanguageKey, lang)
|
|
}
|
|
|
|
// GetLanguageFromContext 从Context中获取语言信息
|
|
func GetLanguageFromContext(ctx context.Context) string {
|
|
if lang, ok := ctx.Value(LanguageKey).(string); ok && lang != "" {
|
|
return lang
|
|
}
|
|
return DefaultLanguage
|
|
}
|
|
|
|
// WithRequestID 将请求ID添加到Context中
|
|
func WithRequestID(ctx context.Context, requestID string) context.Context {
|
|
return context.WithValue(ctx, RequestIDKey, requestID)
|
|
}
|
|
|
|
// GetRequestIDFromContext 从Context中获取请求ID
|
|
func GetRequestIDFromContext(ctx context.Context) string {
|
|
if requestID, ok := ctx.Value(RequestIDKey).(string); ok {
|
|
return requestID
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// WithUserID 将用户ID添加到Context中
|
|
func WithUserID(ctx context.Context, userID string) context.Context {
|
|
return context.WithValue(ctx, UserIDKey, userID)
|
|
}
|
|
|
|
// GetUserIDFromContext 从Context中获取用户ID
|
|
func GetUserIDFromContext(ctx context.Context) string {
|
|
if userID, ok := ctx.Value(UserIDKey).(string); ok {
|
|
return userID
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// WithTraceID 将链路追踪ID添加到Context中
|
|
func WithTraceID(ctx context.Context, traceID string) context.Context {
|
|
return context.WithValue(ctx, TraceIDKey, traceID)
|
|
}
|
|
|
|
// GetTraceIDFromContext 从Context中获取链路追踪ID
|
|
func GetTraceIDFromContext(ctx context.Context) string {
|
|
if traceID, ok := ctx.Value(TraceIDKey).(string); ok {
|
|
return traceID
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ContextInfo 上下文信息结构体
|
|
type ContextInfo struct {
|
|
Language string
|
|
RequestID string
|
|
UserID string
|
|
TraceID string
|
|
}
|
|
|
|
// GetContextInfo 获取Context中的所有信息
|
|
func GetContextInfo(ctx context.Context) *ContextInfo {
|
|
return &ContextInfo{
|
|
Language: GetLanguageFromContext(ctx),
|
|
RequestID: GetRequestIDFromContext(ctx),
|
|
UserID: GetUserIDFromContext(ctx),
|
|
TraceID: GetTraceIDFromContext(ctx),
|
|
}
|
|
}
|
|
|
|
// WithContextInfo 将ContextInfo添加到Context中
|
|
func WithContextInfo(ctx context.Context, info *ContextInfo) context.Context {
|
|
if info == nil {
|
|
return ctx
|
|
}
|
|
|
|
ctx = WithLanguage(ctx, info.Language)
|
|
ctx = WithRequestID(ctx, info.RequestID)
|
|
ctx = WithUserID(ctx, info.UserID)
|
|
ctx = WithTraceID(ctx, info.TraceID)
|
|
|
|
return ctx
|
|
} |