20260719215738

This commit is contained in:
oneao committed 2026-07-19 21:57:40 +08:00
1 parent 35aa108a34
commit cda7f16d3a
116 files changed
+11826 -3669

No files matched your search

+1 -7
View File
@@ -14,10 +14,8 @@ func Load() (*Config, error) {
v := viper.New()
v.SetConfigType("yaml")
// ✅ 默认值
setDefaults(v)
// ✅ 支持环境变量(如 APP_PORT → app.port)
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
@@ -34,7 +32,6 @@ func Load() (*Config, error) {
v.SetConfigName(envConfigName)
if err := v.MergeInConfig(); err != nil {
// dev 可以没有,prod 必须有
if env == "prod" {
return nil, fmt.Errorf("读取环境配置失败(%s): %w", env, err)
}
@@ -48,13 +45,10 @@ func Load() (*Config, error) {
return &cfg, nil
}
// ✅ 默认值统一在这里管理
func setDefaults(v *viper.Viper) {
v.SetDefault("app.name", "allapp-go")
v.SetDefault("app.name", "app")
v.SetDefault("app.port", 8080)
v.SetDefault("app.base_url", "app")
// 可以继续扩展
v.SetDefault("unique_id.datacenter_id", 1)
v.SetDefault("unique_id.worker_id", 1)
}