34 lines
594 B
Go
34 lines
594 B
Go
package appctx
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
DefaultAppCode = "lalu"
|
|
HeaderAppCode = "X-App-Code"
|
|
)
|
|
|
|
type contextKey struct{}
|
|
|
|
func Normalize(value string) string {
|
|
value = strings.ToLower(strings.TrimSpace(value))
|
|
if value == "" {
|
|
return DefaultAppCode
|
|
}
|
|
return value
|
|
}
|
|
|
|
func WithContext(ctx context.Context, value string) context.Context {
|
|
return context.WithValue(ctx, contextKey{}, Normalize(value))
|
|
}
|
|
|
|
func FromContext(ctx context.Context) string {
|
|
if ctx == nil {
|
|
return DefaultAppCode
|
|
}
|
|
value, _ := ctx.Value(contextKey{}).(string)
|
|
return Normalize(value)
|
|
}
|