commit
This commit is contained in:
@@ -11,10 +11,13 @@ import (
|
||||
type Config struct {
|
||||
Server ServerConfig `mapstructure:"server"`
|
||||
Database DatabaseConfig `mapstructure:"database"`
|
||||
Redis RedisConfig `mapstructure:"redis"`
|
||||
JWT JWTConfig `mapstructure:"jwt"`
|
||||
Wechat WechatConfig `mapstructure:"wechat"`
|
||||
XHS XHSConfig `mapstructure:"xhs"`
|
||||
Scheduler SchedulerConfig `mapstructure:"scheduler"`
|
||||
Upload UploadConfig `mapstructure:"upload"`
|
||||
AliSms AliSmsConfig `mapstructure:"ali_sms"`
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
@@ -36,6 +39,14 @@ type DatabaseConfig struct {
|
||||
ConnMaxLifetime int `mapstructure:"conn_max_lifetime"`
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
Password string `mapstructure:"password"`
|
||||
DB int `mapstructure:"db"`
|
||||
PoolSize int `mapstructure:"pool_size"`
|
||||
}
|
||||
|
||||
type JWTConfig struct {
|
||||
Secret string `mapstructure:"secret"`
|
||||
ExpireHours int `mapstructure:"expire_hours"`
|
||||
@@ -64,6 +75,34 @@ type SchedulerConfig struct {
|
||||
ProxyFetchURL string `mapstructure:"proxy_fetch_url"` // 动态获取代理的接口地址(可选)
|
||||
}
|
||||
|
||||
// UploadConfig 文件上传配置
|
||||
type UploadConfig struct {
|
||||
MaxImageSize int64 `mapstructure:"max_image_size"` // 图片最大大小(字节)
|
||||
MaxFileSize int64 `mapstructure:"max_file_size"` // 文件最大大小(字节)
|
||||
ImageTypes []string `mapstructure:"image_types"` // 允许的图片类型
|
||||
StaticPath string `mapstructure:"static_path"` // 静态文件路径(本地存储)
|
||||
BaseURL string `mapstructure:"base_url"` // 静态文件访问基础URL(本地存储)
|
||||
StorageType string `mapstructure:"storage_type"` // 存储类型:local(本地) 或 oss(阿里云OSS)
|
||||
OSS OSSConfig `mapstructure:"oss"` // OSS配置
|
||||
}
|
||||
|
||||
type OSSConfig struct {
|
||||
Endpoint string `mapstructure:"endpoint"` // OSS访问域名
|
||||
AccessKeyID string `mapstructure:"access_key_id"` // AccessKey ID
|
||||
AccessKeySecret string `mapstructure:"access_key_secret"` // AccessKey Secret
|
||||
BucketName string `mapstructure:"bucket_name"` // Bucket名称
|
||||
BasePath string `mapstructure:"base_path"` // 文件存储基础路径
|
||||
Domain string `mapstructure:"domain"` // 自定义域名(可选)
|
||||
}
|
||||
|
||||
// AliSmsConfig 阿里云短信配置
|
||||
type AliSmsConfig struct {
|
||||
AccessKeyID string `mapstructure:"access_key_id"` // AccessKey ID
|
||||
AccessKeySecret string `mapstructure:"access_key_secret"` // AccessKey Secret
|
||||
SignName string `mapstructure:"sign_name"` // 短信签名
|
||||
TemplateCode string `mapstructure:"template_code"` // 短信模板CODE
|
||||
}
|
||||
|
||||
var AppConfig *Config
|
||||
|
||||
// LoadConfig 加载配置文件
|
||||
@@ -100,8 +139,16 @@ func LoadConfig(env string) error {
|
||||
return fmt.Errorf("解析配置文件失败: %w", err)
|
||||
}
|
||||
|
||||
// 打印OSS配置来源调试信息
|
||||
log.Printf("\n=== OSS配置来源检查 ===")
|
||||
log.Printf("upload.oss.access_key_secret 配置值: [%s]", AppConfig.Upload.OSS.AccessKeySecret)
|
||||
log.Printf("环境变量 OSS_ACCESS_KEY_SECRET: [%s]", os.Getenv("OSS_ACCESS_KEY_SECRET"))
|
||||
log.Printf("环境变量 OSS_TEST_ACCESS_KEY_SECRET: [%s]", os.Getenv("OSS_TEST_ACCESS_KEY_SECRET"))
|
||||
log.Printf("====================\n")
|
||||
|
||||
log.Printf("配置加载成功: %s 环境", env)
|
||||
log.Printf("数据库配置: %s@%s:%d/%s", AppConfig.Database.Username, AppConfig.Database.Host, AppConfig.Database.Port, AppConfig.Database.DBName)
|
||||
log.Printf("Python服务地址: %s", AppConfig.XHS.PythonServiceURL)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -119,6 +166,13 @@ func bindEnvVariables() {
|
||||
viper.BindEnv("database.dbname", "DB_NAME")
|
||||
viper.BindEnv("database.charset", "DB_CHARSET")
|
||||
|
||||
// Redis 配置
|
||||
viper.BindEnv("redis.host", "REDIS_HOST")
|
||||
viper.BindEnv("redis.port", "REDIS_PORT")
|
||||
viper.BindEnv("redis.password", "REDIS_PASSWORD")
|
||||
viper.BindEnv("redis.db", "REDIS_DB")
|
||||
viper.BindEnv("redis.pool_size", "REDIS_POOL_SIZE")
|
||||
|
||||
// JWT 配置
|
||||
viper.BindEnv("jwt.secret", "JWT_SECRET")
|
||||
viper.BindEnv("jwt.expire_hours", "JWT_EXPIRE_HOURS")
|
||||
@@ -142,6 +196,27 @@ func bindEnvVariables() {
|
||||
viper.BindEnv("scheduler.proxy", "SCHEDULER_PROXY")
|
||||
viper.BindEnv("scheduler.user_agent", "SCHEDULER_USER_AGENT")
|
||||
viper.BindEnv("scheduler.proxy_fetch_url", "SCHEDULER_PROXY_FETCH_URL")
|
||||
|
||||
// OSS 配置 - 强制从配置文件读取,不使用环境变量
|
||||
// viper.BindEnv("upload.oss.endpoint", "OSS_ENDPOINT")
|
||||
// viper.BindEnv("upload.oss.access_key_id", "OSS_ACCESS_KEY_ID")
|
||||
// viper.BindEnv("upload.oss.access_key_secret", "OSS_ACCESS_KEY_SECRET")
|
||||
// viper.BindEnv("upload.oss.bucket_name", "OSS_BUCKET_NAME")
|
||||
// viper.BindEnv("upload.oss.base_path", "OSS_BASE_PATH")
|
||||
// viper.BindEnv("upload.oss.domain", "OSS_DOMAIN")
|
||||
|
||||
// Upload 配置
|
||||
viper.BindEnv("upload.max_image_size", "UPLOAD_MAX_IMAGE_SIZE")
|
||||
viper.BindEnv("upload.max_file_size", "UPLOAD_MAX_FILE_SIZE")
|
||||
viper.BindEnv("upload.static_path", "UPLOAD_STATIC_PATH")
|
||||
viper.BindEnv("upload.base_url", "UPLOAD_BASE_URL")
|
||||
viper.BindEnv("upload.storage_type", "UPLOAD_STORAGE_TYPE")
|
||||
|
||||
// AliSms 配置
|
||||
viper.BindEnv("ali_sms.access_key_id", "ALI_SMS_ACCESS_KEY_ID")
|
||||
viper.BindEnv("ali_sms.access_key_secret", "ALI_SMS_ACCESS_KEY_SECRET")
|
||||
viper.BindEnv("ali_sms.sign_name", "ALI_SMS_SIGN_NAME")
|
||||
viper.BindEnv("ali_sms.template_code", "ALI_SMS_TEMPLATE_CODE")
|
||||
}
|
||||
|
||||
// GetDSN 获取数据库连接字符串
|
||||
|
||||
Reference in New Issue
Block a user