package config import ( "fmt" "os" ) // Config 保存进程运行所需的全部配置。 type Config struct { Addr string // HTTP 监听地址 DBPath string // SQLite 文件路径 DataKey string // 敏感字段加密主密钥 JWTSecret string // JWT 签名密钥 AdminUsername string // 初始管理员用户名 AdminPassword string // 初始管理员密码,仅在该用户不存在时用于创建 PublicURL string // 面板公网基址(如 https://demo.example.com),日志回传引导创建用 } // Load 从环境变量读取配置;DATA_KEY、JWT_SECRET 无默认值,缺失即失败。 func Load() (*Config, error) { dataKey := os.Getenv("DATA_KEY") if dataKey == "" { return nil, fmt.Errorf("load config: DATA_KEY is required") } jwtSecret := os.Getenv("JWT_SECRET") if jwtSecret == "" { return nil, fmt.Errorf("load config: JWT_SECRET is required") } return &Config{ Addr: envOr("ADDR", ":8080"), DBPath: envOr("DB_PATH", "oci-portal.db"), DataKey: dataKey, JWTSecret: jwtSecret, AdminUsername: envOr("ADMIN_USERNAME", "admin"), AdminPassword: os.Getenv("ADMIN_PASSWORD"), PublicURL: os.Getenv("PUBLIC_URL"), }, nil } func envOr(name, def string) string { if v := os.Getenv(name); v != "" { return v } return def }