Enhance AI and Redis integration for smoke logging features
- Added AI configuration options to .env.example and config.go for OpenAI integration. - Implemented Redis caching for session management in main.go and auth middleware. - Updated smoke logging service to support real smoking time (`smoke_at`) and AI advice retrieval. - Enhanced API routes to include endpoints for AI advice and unlock functionality for non-members. - Improved database schema with new tables for AI advice and unlock records. - Expanded documentation to cover new AI features and Redis caching implementation.
This commit is contained in:
@@ -14,8 +14,11 @@ type Config struct {
|
||||
Database DatabaseConfig
|
||||
JWT JWTConfig
|
||||
ShortVideo ShortVideoConfig
|
||||
AI AIConfig
|
||||
Admin AdminConfig
|
||||
Qiniu QiniuConfig
|
||||
WeChatOA WeChatOfficialConfig
|
||||
Redis RedisConfig
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -42,6 +45,19 @@ type ShortVideoConfig struct {
|
||||
RequestTimeout time.Duration
|
||||
}
|
||||
|
||||
// AIConfig 用于“AI 建议/问答”等能力的通用配置(OpenAI-compatible)。
|
||||
type AIConfig struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
Model string
|
||||
RequestTimeout time.Duration
|
||||
}
|
||||
|
||||
// AdminConfig 用于简单的后台/运维接口鉴权(如生成兑换码)。
|
||||
type AdminConfig struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
// QiniuConfig 用于七牛云(Kodo)直传相关配置。
|
||||
// 前端通常会向后端请求 upload token,然后直传文件到七牛。
|
||||
type QiniuConfig struct {
|
||||
@@ -61,6 +77,15 @@ type WeChatOfficialConfig struct {
|
||||
RequestTimeout time.Duration
|
||||
}
|
||||
|
||||
// RedisConfig 用于可选的 Redis 缓存(例如缓存 session_key -> user,减少 DB 查询)。
|
||||
type RedisConfig struct {
|
||||
Addr string
|
||||
Password string
|
||||
DB int
|
||||
KeyPrefix string
|
||||
SessionTTLSeconds int
|
||||
}
|
||||
|
||||
var AppConfig *Config
|
||||
|
||||
func LoadConfig() {
|
||||
@@ -90,6 +115,15 @@ func LoadConfig() {
|
||||
FreeDailyQuota: getEnvAsInt("SHORT_VIDEO_FREE_QUOTA", 20),
|
||||
RequestTimeout: time.Duration(getEnvAsInt("SHORT_VIDEO_TIMEOUT_SECONDS", 5)) * time.Second,
|
||||
},
|
||||
AI: AIConfig{
|
||||
BaseURL: getEnv("AI_BASE_URL", "https://api.openai.com/v1"),
|
||||
APIKey: getEnv("AI_API_KEY", ""),
|
||||
Model: getEnv("AI_MODEL", "gpt-4o-mini"),
|
||||
RequestTimeout: time.Duration(getEnvAsInt("AI_TIMEOUT_SECONDS", 15)) * time.Second,
|
||||
},
|
||||
Admin: AdminConfig{
|
||||
Token: getEnv("ADMIN_API_TOKEN", ""),
|
||||
},
|
||||
Qiniu: QiniuConfig{
|
||||
AccessKey: getEnv("QINIU_ACCESS_KEY", ""),
|
||||
SecretKey: getEnv("QINIU_SECRET_KEY", ""),
|
||||
@@ -104,6 +138,13 @@ func LoadConfig() {
|
||||
AppSecret: getEnv("WECHAT_OA_APP_SECRET", ""),
|
||||
RequestTimeout: time.Duration(getEnvAsInt("WECHAT_OA_TIMEOUT_SECONDS", 5)) * time.Second,
|
||||
},
|
||||
Redis: RedisConfig{
|
||||
Addr: getEnv("REDIS_ADDR", ""),
|
||||
Password: getEnv("REDIS_PASSWORD", ""),
|
||||
DB: getEnvAsInt("REDIS_DB", 0),
|
||||
KeyPrefix: getEnv("REDIS_KEY_PREFIX", "wx_service:"),
|
||||
SessionTTLSeconds: getEnvAsInt("REDIS_SESSION_TTL_SECONDS", 86400),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user