feat: 扩展 Config 结构体,新增 AuthConfig 配置

- 新增 AuthConfig 结构体(JWTSecret, AccessTTL, RefreshTTL)
- 在 Config 中添加 Auth 字段
- 设置默认值:access_ttl=15分钟,refresh_ttl=10080分钟(7天)
- JWTSecret 必须通过环境变量 CAMTALK_AUTH_JWT_SECRET 设置
This commit is contained in:
hhs
2026-06-14 16:40:06 +08:00
parent 07211675e9
commit 4b30e67c2e
4 changed files with 1640 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ type Config struct {
AI AIConfig `mapstructure:"ai"`
Storage StorageConfig `mapstructure:"storage"`
Log LogConfig `mapstructure:"log"`
Auth AuthConfig `mapstructure:"auth"`
}
// SessionConfig 会话管理配置。
@@ -99,6 +100,13 @@ type LogConfig struct {
Format string `mapstructure:"format"`
}
// AuthConfig 认证配置。
type AuthConfig struct {
JWTSecret string `mapstructure:"jwt_secret"` // JWT 签名密钥,必须通过环境变量 CAMTALK_AUTH_JWT_SECRET 设置
AccessTTL int `mapstructure:"access_ttl"` // Access Token 过期时间(分钟),默认 15
RefreshTTL int `mapstructure:"refresh_ttl"` // Refresh Token 过期时间(分钟),默认 100807天
}
// Load 加载配置。优先级:环境变量 > config.{env}.yaml > config.yaml。
func Load() (*Config, error) {
v := viper.New()
@@ -146,6 +154,8 @@ func Load() (*Config, error) {
v.SetDefault("storage.driver", "memory")
v.SetDefault("log.level", "info")
v.SetDefault("log.format", "console")
v.SetDefault("auth.access_ttl", 15)
v.SetDefault("auth.refresh_ttl", 10080)
// 读取基础配置文件
_ = v.ReadInConfig() // 文件不存在不报错