diff --git a/backend/.gitignore b/backend/.gitignore index 8304260..a230058 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -4,8 +4,6 @@ bin/ # 环境配置 .env -config.dev.yaml -config.prod.yaml # 临时文件 tmp/ diff --git a/backend/config/config.dev.yaml b/backend/config/config.dev.yaml new file mode 100644 index 0000000..4670be1 --- /dev/null +++ b/backend/config/config.dev.yaml @@ -0,0 +1,55 @@ +# CamTalk 开发环境配置 +# 通过 APP_ENV=dev 加载此文件,覆盖 config.yaml 中的配置 + +server: + host: "0.0.0.0" + port: 8080 + heartbeat_interval: 30 + heartbeat_timeout: 60 + allowed_origins: [] # 开发环境允许所有来源 + +session: + ttl: 30 # 开发环境会话较短,方便测试过期逻辑 + max_history: 20 + +ai: + stt: + timeout: 10 # 开发环境超时较长,方便调试 + http_client_timeout: 30 + llm: + timeout: 60 # 开发环境 LLM 超时较长 + http_client_timeout: 120 + tts: + timeout: 10 + http_client_timeout: 30 + +storage: + redis: + enabled: true # 开发环境启用 Redis,测试三级存储 + persistence: + enabled: true # 开发环境启用持久化 + +redis: + addr: "localhost:6379" # 本地 Redis + password: "" + db: 0 + +auth: + access_ttl: 120 # 开发环境 Access Token 2 小时,方便调试 + refresh_ttl: 10080 # 7 天 + +ratelimit: + enabled: false # 开发环境关闭限流,方便测试 + query: + capacity: 10 + rate: 0.2 + login: + capacity: 5 + rate: 0.1 + register: + capacity: 3 + rate: 0.05 + +log: + level: debug # 开发环境 debug 日志 + format: console # 控制台格式,易读 diff --git a/backend/config/config.prod.yaml b/backend/config/config.prod.yaml new file mode 100644 index 0000000..5d015d3 --- /dev/null +++ b/backend/config/config.prod.yaml @@ -0,0 +1,74 @@ +# CamTalk 生产环境配置 +# 通过 APP_ENV=prod 加载此文件,覆盖 config.yaml 中的配置 + +server: + host: "0.0.0.0" + port: 8080 + read_timeout: 30 + write_timeout: 30 + shutdown_timeout: 15 # 生产环境优雅关闭时间稍长 + heartbeat_interval: 30 + heartbeat_timeout: 60 + allowed_origins: # 生产环境严格 CORS 白名单 + - "https://camtalk.example.com" + - "https://www.camtalk.example.com" + +session: + ttl: 60 # 生产环境会话 1 小时 + max_history: 20 + +ai: + stt: + provider: mimo # 生产环境推荐 MiMo,性价比高 + model: mimo-v2.5-asr + endpoint: "https://api.xiaomimimo.com/v1" + timeout: 5 # 生产环境严格超时控制 + http_client_timeout: 30 + llm: + provider: dashscope # 生产环境推荐通义千问,稳定性好 + model: qwen3-vl-plus + endpoint: "https://dashscope.aliyuncs.com/compatible-mode/v1" + timeout: 30 + http_client_timeout: 60 + tts: + provider: mimo # 生产环境推荐 MiMo TTS + model: mimo-v2.5-tts + voice: mimo_default + speed: 1.0 + endpoint: "https://token-plan-cn.xiaomimimo.com/v1" + timeout: 5 + http_client_timeout: 30 + output_format: mp3 + sample_rate: 24000 + +storage: + redis: + enabled: true # 生产环境必须启用 Redis + persistence: + enabled: true # 生产环境必须启用持久化 + driver: postgres + +redis: + addr: "redis:6379" # Docker Compose 内部服务名 + password: "" # 密码通过 CAMTALK_REDIS_PASSWORD 环境变量设置 + db: 0 + +auth: + access_ttl: 120 # 生产环境 Access Token 2 小时 + refresh_ttl: 10080 # Refresh Token 7 天 + +ratelimit: + enabled: true # 生产环境启用限流 + query: + capacity: 10 # 允许突发 10 个请求 + rate: 0.2 # 每 5 秒恢复 1 个令牌 + login: + capacity: 5 # 防暴力破解 + rate: 0.1 # 每 10 秒恢复 1 次 + register: + capacity: 3 # 防批量注册 + rate: 0.05 # 每 20 秒恢复 1 次 + +log: + level: info # 生产环境 info 级别 + format: json # JSON 格式,便于日志收集和分析 diff --git a/backend/config.yaml b/backend/config/config.yaml similarity index 100% rename from backend/config.yaml rename to backend/config/config.yaml diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go index dd62719..e0ba227 100644 --- a/backend/internal/config/config.go +++ b/backend/internal/config/config.go @@ -136,7 +136,7 @@ type BucketConfig struct { } // Load 加载配置。优先级:环境变量 > config.{env}.yaml > config.yaml > 默认值。 -// workDir 为项目根目录或 backend 目录,用于定位 .env 和 config.yaml。 +// workDir 为项目根目录或 backend 目录,用于定位 .env 和 config/config.yaml。 func Load(workDir string) (*Config, error) { // 1. 加载 .env 文件(敏感信息) envFile := filepath.Join(workDir, ".env") @@ -145,7 +145,8 @@ func Load(workDir string) (*Config, error) { v := viper.New() v.SetConfigName("config") v.SetConfigType("yaml") - v.AddConfigPath(workDir) + v.AddConfigPath(filepath.Join(workDir, "config")) // 配置文件在 config/ 目录下 + v.AddConfigPath(workDir) // 兼容旧路径 // 2. 设置默认值(与 config.yaml 保持一致,仅作为兜底) setDefaults(v)