重构项目的实现,优化使用方法与使用逻辑
This commit is contained in:
41
requestctx/requestctx.go
Normal file
41
requestctx/requestctx.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package requestctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.toowon.com/jimmy/go-common/tools"
|
||||
)
|
||||
|
||||
type languageKey struct{}
|
||||
type timezoneKey struct{}
|
||||
|
||||
const (
|
||||
DefaultLanguage = "zh-CN"
|
||||
DefaultTimezone = tools.AsiaShanghai
|
||||
)
|
||||
|
||||
// WithLanguage 写入语言到 context
|
||||
func WithLanguage(ctx context.Context, lang string) context.Context {
|
||||
return context.WithValue(ctx, languageKey{}, lang)
|
||||
}
|
||||
|
||||
// Language 从 context 读取语言
|
||||
func Language(ctx context.Context) string {
|
||||
if lang, ok := ctx.Value(languageKey{}).(string); ok && lang != "" {
|
||||
return lang
|
||||
}
|
||||
return DefaultLanguage
|
||||
}
|
||||
|
||||
// WithTimezone 写入时区到 context
|
||||
func WithTimezone(ctx context.Context, tz string) context.Context {
|
||||
return context.WithValue(ctx, timezoneKey{}, tz)
|
||||
}
|
||||
|
||||
// Timezone 从 context 读取时区
|
||||
func Timezone(ctx context.Context) string {
|
||||
if tz, ok := ctx.Value(timezoneKey{}).(string); ok && tz != "" {
|
||||
return tz
|
||||
}
|
||||
return DefaultTimezone
|
||||
}
|
||||
Reference in New Issue
Block a user