Files
2026-09-03 17:55:42 +08:00

94 lines
2.4 KiB
Go

package config
import (
"os"
"strconv"
)
// Config 全局基础配置(仅保留服务启动及基础设施参数)
// 业务参数(极验、短信、微信、审核时限等)已统一移入 MySQL 数据库 yz_pw_system_configs 表管理
type Config struct {
Port string
JWTSecret string
JWTExpireHrs int
UploadDir string
// MySQL
DBHost string
DBPort string
DBUser string
DBPassword string
DBName string
DBCharset string
DBAutoMigrate bool
// 平台管理员账号(首次启动自动创建)
AdminUsername string
AdminPassword 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", "212.64.112.158"),
DBPort: getEnv("DB_PORT", "3388"),
// 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"),
DBAutoMigrate: getEnvBool("DB_AUTO_MIGRATE", false),
AdminUsername: getEnv("ADMIN_USERNAME", "hero920103"),
AdminPassword: getEnv("ADMIN_PASSWORD", "920103"),
// 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
}
func getEnvBool(key string, def bool) bool {
if v := os.Getenv(key); v != "" {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return def
}