From ddf28f59cf5d6ee3aa0bd809a47b5700be45cb31 Mon Sep 17 00:00:00 2001 From: unclesam <2036253946@qq.com> Date: Sat, 25 Jul 2026 15:13:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(conf):=20=E7=A7=BB=E9=99=A4=E5=86=97?= =?UTF-8?q?=E4=BD=99=E7=9A=84=20mapstructure=20=E7=BB=93=E6=9E=84=E4=BD=93?= =?UTF-8?q?=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/conf/conf.go | 19 ++++++++++++------- internal/conf/conf_db.go | 12 ++++++------ internal/conf/conf_jwt.go | 10 +++++----- internal/conf/conf_logger.go | 14 +++++++------- internal/conf/conf_redis.go | 6 +++--- internal/conf/conf_server.go | 8 ++++---- 6 files changed, 37 insertions(+), 32 deletions(-) diff --git a/internal/conf/conf.go b/internal/conf/conf.go index d50cf14..eabee82 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -4,15 +4,16 @@ import ( "fmt" "github.com/fsnotify/fsnotify" + "github.com/go-viper/mapstructure/v2" "github.com/spf13/viper" ) type Config struct { - Server ServerConfig `mapstructure:"server" yaml:"server"` - Database DatabaseConfig `mapstructure:"database" yaml:"database"` - Logger LoggerConfig `mapstructure:"logger" yaml:"logger"` - Jwt JwtConfig `mapstructure:"jwt" yaml:"jwt"` - Redis RedisConfig `mapstructure:"redis" yaml:"redis"` + Server ServerConfig `yaml:"server"` + Database DatabaseConfig `yaml:"database"` + Logger LoggerConfig `yaml:"logger"` + Jwt JwtConfig `yaml:"jwt"` + Redis RedisConfig `yaml:"redis"` } // LoadConfig 读取并解析配置文件 @@ -24,7 +25,9 @@ func LoadConfig(path string) (*Config, error) { } cfg := &Config{} - if err := v.Unmarshal(cfg); err != nil { + if err := v.Unmarshal(cfg, func(dc *mapstructure.DecoderConfig) { + dc.TagName = "yaml" + }); err != nil { return nil, fmt.Errorf("解析配置文件失败: %w", err) } @@ -32,7 +35,9 @@ func LoadConfig(path string) (*Config, error) { v.WatchConfig() v.OnConfigChange(func(e fsnotify.Event) { fmt.Printf("配置文件已更新: %s\n", e.Name) - if err := v.Unmarshal(cfg); err != nil { + if err := v.Unmarshal(cfg, func(dc *mapstructure.DecoderConfig) { + dc.TagName = "yaml" + }); err != nil { fmt.Printf("重载配置文件失败: %s\n", err) } else { fmt.Println("🚀 配置热重载成功,新配置已即时生效!") diff --git a/internal/conf/conf_db.go b/internal/conf/conf_db.go index 9c147eb..c1a430f 100644 --- a/internal/conf/conf_db.go +++ b/internal/conf/conf_db.go @@ -4,12 +4,12 @@ import "fmt" // DatabaseConfig 定义数据库连接配置结构体 type DatabaseConfig struct { - Driver string `mapstructure:"driver" yaml:"driver"` - Host string `mapstructure:"host" yaml:"host"` - Port int `mapstructure:"port" yaml:"port"` - User string `mapstructure:"user" yaml:"user"` - Password string `mapstructure:"password" yaml:"password"` - Dbname string `mapstructure:"dbname" yaml:"dbname"` + Driver string `yaml:"driver"` + Host string `yaml:"host"` + Port int `yaml:"port"` + User string `yaml:"user"` + Password string `yaml:"password"` + Dbname string `yaml:"dbname"` } // Dsn 根据 driver 动态生成 DSN 字符串 diff --git a/internal/conf/conf_jwt.go b/internal/conf/conf_jwt.go index fa965b1..ae87270 100644 --- a/internal/conf/conf_jwt.go +++ b/internal/conf/conf_jwt.go @@ -1,9 +1,9 @@ package conf type JwtConfig struct { - AccessExpire int `mapstructure:"access_expire" yaml:"access_expire"` - RefreshExpire int `mapstructure:"refresh_expire" yaml:"refresh_expire"` - AccessSecret string `mapstructure:"access_secret" yaml:"access_secret"` - RefreshSecret string `mapstructure:"refresh_secret" yaml:"refresh_secret"` - Issuer string `mapstructure:"issuer" yaml:"issuer"` + AccessExpire int `yaml:"access_expire"` + RefreshExpire int `yaml:"refresh_expire"` + AccessSecret string `yaml:"access_secret"` + RefreshSecret string `yaml:"refresh_secret"` + Issuer string `yaml:"issuer"` } diff --git a/internal/conf/conf_logger.go b/internal/conf/conf_logger.go index b4f203c..3d2fcd5 100644 --- a/internal/conf/conf_logger.go +++ b/internal/conf/conf_logger.go @@ -1,11 +1,11 @@ package conf type LoggerConfig struct { - Prefix string `mapstructure:"prefix" yaml:"prefix"` - Level string `mapstructure:"level" yaml:"level"` - Filename string `mapstructure:"filename" yaml:"filename"` - MaxSize int `mapstructure:"max_size" yaml:"max_size"` - MaxBackups int `mapstructure:"max_backups" yaml:"max_backups"` - MaxAge int `mapstructure:"max_age" yaml:"max_age"` - Compress bool `mapstructure:"compress" yaml:"compress"` + Prefix string `yaml:"prefix"` + Level string `yaml:"level"` + Filename string `yaml:"filename"` + MaxSize int `yaml:"max_size"` + MaxBackups int `yaml:"max_backups"` + MaxAge int `yaml:"max_age"` + Compress bool `yaml:"compress"` } diff --git a/internal/conf/conf_redis.go b/internal/conf/conf_redis.go index 127300c..a7d6157 100644 --- a/internal/conf/conf_redis.go +++ b/internal/conf/conf_redis.go @@ -1,7 +1,7 @@ package conf type RedisConfig struct { - Addr string `mapstructure:"addr" yaml:"addr"` - Password string `mapstructure:"password" yaml:"password"` - Db int `mapstructure:"db" yaml:"db"` + Addr string `yaml:"addr"` + Password string `yaml:"password"` + Db int `yaml:"db"` } diff --git a/internal/conf/conf_server.go b/internal/conf/conf_server.go index 954268d..7fdb005 100644 --- a/internal/conf/conf_server.go +++ b/internal/conf/conf_server.go @@ -1,8 +1,8 @@ package conf type ServerConfig struct { - Port int `mapstructure:"port" yaml:"port"` - Mode string `mapstructure:"mode" yaml:"mode"` // debug / release - AllowOrigins []string `mapstructure:"allow_origins" yaml:"allow_origins"` - Version string `mapstructure:"mode" yaml:"version"` + Port int `yaml:"port"` + Mode string `yaml:"mode"` // debug / release + AllowOrigins []string `yaml:"allow_origins"` + Version string `yaml:"version"` }