package config import ( "os" "strconv" ) // Config 全局配置,优先从环境变量读取,提供默认值以便开箱即用。 type Config struct { Port string JWTSecret string JWTExpireHrs int UploadDir string // MySQL DBHost string DBPort string DBUser string DBPassword string DBName string DBCharset string // 平台管理员账号(首次启动自动创建) AdminUsername string AdminPassword string // 班级审核补齐期限(天),逾期自动删除 AuditGraceDays int // 极验行为验证(参数后期补,为空则跳过极验校验) GeeTestID string GeeTestKey string // 短信验证码(参数后期补,为空则短信接口返回配置缺失) SMSProvider string // aliyun / tencent SMSAccessKey string SMSSecretKey string SMSSignName string SMSTemplateCode string // 微信扫码登录(参数后期补,为空则微信接口返回配置缺失) WechatAppID string WechatAppSecret string WechatRedirectURI string // Redis(缓存/验证码/限流,连接失败自动降级内存) RedisHost string RedisPort string RedisPassword string RedisDB int } var C *Config func Load() *Config { C = &Config{ Port: getEnv("PORT", "8010"), JWTSecret: getEnv("JWT_SECRET", "photowall-dev-secret-change-in-prod"), JWTExpireHrs: getEnvInt("JWT_EXPIRE_HOURS", 3), UploadDir: getEnv("UPLOAD_DIR", "uploads"), DBHost: getEnv("DB_HOST", "10.31.100.3"), DBPort: getEnv("DB_PORT", "3306"), DBUser: getEnv("DB_USER", "photowall"), DBPassword: getEnv("DB_PASSWORD", "Dfn47yeKpyJfwz8n"), DBName: getEnv("DB_NAME", "photowall"), DBCharset: getEnv("DB_CHARSET", "utf8mb4"), AdminUsername: getEnv("ADMIN_USERNAME", "hero920103"), AdminPassword: getEnv("ADMIN_PASSWORD", "920103"), AuditGraceDays: getEnvInt("AUDIT_GRACE_DAYS", 30), // 极验(后期补参数) GeeTestID: getEnv("GEETEST_ID", ""), GeeTestKey: getEnv("GEETEST_KEY", ""), // 短信(后期补参数) SMSProvider: getEnv("SMS_PROVIDER", "aliyun"), SMSAccessKey: getEnv("SMS_ACCESS_KEY", ""), SMSSecretKey: getEnv("SMS_SECRET_KEY", ""), SMSSignName: getEnv("SMS_SIGN_NAME", ""), SMSTemplateCode: getEnv("SMS_TEMPLATE_CODE", ""), // 微信扫码登录(后期补参数) WechatAppID: getEnv("WECHAT_APP_ID", ""), WechatAppSecret: getEnv("WECHAT_APP_SECRET", ""), WechatRedirectURI: getEnv("WECHAT_REDIRECT_URI", ""), // Redis RedisHost: getEnv("REDIS_HOST", "localhost"), RedisPort: getEnv("REDIS_PORT", "6379"), RedisPassword: getEnv("REDIS_PASSWORD", ""), RedisDB: getEnvInt("REDIS_DB", 0), } return C } // DSN 生成 MySQL 连接串 func (c *Config) DSN() string { return c.DBUser + ":" + c.DBPassword + "@tcp(" + c.DBHost + ":" + c.DBPort + ")/" + c.DBName + "?charset=" + c.DBCharset + "&parseTime=True&loc=Local" } func getEnv(key, def string) string { if v := os.Getenv(key); v != "" { return v } return def } func getEnvInt(key string, def int) int { if v := os.Getenv(key); v != "" { if n, err := strconv.Atoi(v); err == nil { return n } } return def }