package main import ( "encoding/json" "errors" "os" "path/filepath" "strconv" ) // defaultAppPaths 收集运行时路径,支持环境变量覆盖,方便不同平台和部署方式调整。 func defaultAppPaths() appPaths { return appPaths{ ConfigFile: envOrDefault("DOUYU_CONFIG", defaultConfigFile), AccountFile: envOrDefault("DOUYU_USERS", defaultAccountFile), LogDir: envOrDefault("DOUYU_LOG_DIR", defaultLogDir), SuccessDir: envOrDefault("DOUYU_SUCCESS_DIR", defaultSuccessDir), FontFiles: collectFontCandidates(), } } func envOrDefault(key, fallback string) string { if value := os.Getenv(key); value != "" { return filepath.Clean(value) } return fallback } // loadTraceConfig 读取 trace 开关;默认关闭,避免保存敏感调试数据。 func loadTraceConfig() TraceConfig { return TraceConfig{ Enabled: isTruthyEnv("DOUYU_TRACE"), MaxBodyBytes: envInt("DOUYU_TRACE_MAX_BODY", 0), } } func isTruthyEnv(key string) bool { switch os.Getenv(key) { case "1", "true", "TRUE", "yes", "YES", "on", "ON": return true default: return false } } func envInt(key string, fallback int) int { raw := os.Getenv(key) if raw == "" { return fallback } value, err := strconv.Atoi(raw) if err != nil || value <= 0 { return fallback } return value } // loadConfig 读取配置文件;缺失时安静使用内置默认配置。 func loadConfig(path string) Config { cfg := Config{TimeAPIURL: defaultTimeAPI} if path == "" { path = defaultConfigFile } f, err := os.Open(path) if err != nil { if errors.Is(err, os.ErrNotExist) { logEvent("配置", "info", "配置加载完成: 内置默认配置") } else { logEvent("配置", "warn", "读取 %s 出错,使用内置默认配置: %v", path, err) } return cfg } defer f.Close() if err := json.NewDecoder(f).Decode(&cfg); err != nil { logEvent("配置", "warn", "解析 %s 失败,使用内置默认配置: %v", path, err) return Config{TimeAPIURL: defaultTimeAPI} } if cfg.TimeAPIURL == "" { cfg.TimeAPIURL = defaultTimeAPI } logEvent("配置", "success", "配置加载完成: %s", path) return cfg }