refactor(conf): 移除冗余的 mapstructure 结构体标签

This commit is contained in:
unclesam committed 2026-07-25 15:13:42 +08:00
1 parent 7f68cecf21
commit ddf28f59cf
6 files changed
+37 -32

No files matched your search

+12 -7
View File
@@ -4,15 +4,16 @@ import (
"fmt" "fmt"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
type Config struct { type Config struct {
Server ServerConfig `mapstructure:"server" yaml:"server"` Server ServerConfig `yaml:"server"`
Database DatabaseConfig `mapstructure:"database" yaml:"database"` Database DatabaseConfig `yaml:"database"`
Logger LoggerConfig `mapstructure:"logger" yaml:"logger"` Logger LoggerConfig `yaml:"logger"`
Jwt JwtConfig `mapstructure:"jwt" yaml:"jwt"` Jwt JwtConfig `yaml:"jwt"`
Redis RedisConfig `mapstructure:"redis" yaml:"redis"` Redis RedisConfig `yaml:"redis"`
} }
// LoadConfig 读取并解析配置文件 // LoadConfig 读取并解析配置文件
@@ -24,7 +25,9 @@ func LoadConfig(path string) (*Config, error) {
} }
cfg := &Config{} 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) return nil, fmt.Errorf("解析配置文件失败: %w", err)
} }
@@ -32,7 +35,9 @@ func LoadConfig(path string) (*Config, error) {
v.WatchConfig() v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) { v.OnConfigChange(func(e fsnotify.Event) {
fmt.Printf("配置文件已更新: %s\n", e.Name) 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) fmt.Printf("重载配置文件失败: %s\n", err)
} else { } else {
fmt.Println("🚀 配置热重载成功,新配置已即时生效!") fmt.Println("🚀 配置热重载成功,新配置已即时生效!")
+6 -6
View File
@@ -4,12 +4,12 @@ import "fmt"
// DatabaseConfig 定义数据库连接配置结构体 // DatabaseConfig 定义数据库连接配置结构体
type DatabaseConfig struct { type DatabaseConfig struct {
Driver string `mapstructure:"driver" yaml:"driver"` Driver string `yaml:"driver"`
Host string `mapstructure:"host" yaml:"host"` Host string `yaml:"host"`
Port int `mapstructure:"port" yaml:"port"` Port int `yaml:"port"`
User string `mapstructure:"user" yaml:"user"` User string `yaml:"user"`
Password string `mapstructure:"password" yaml:"password"` Password string `yaml:"password"`
Dbname string `mapstructure:"dbname" yaml:"dbname"` Dbname string `yaml:"dbname"`
} }
// Dsn 根据 driver 动态生成 DSN 字符串 // Dsn 根据 driver 动态生成 DSN 字符串
+5 -5
View File
@@ -1,9 +1,9 @@
package conf package conf
type JwtConfig struct { type JwtConfig struct {
AccessExpire int `mapstructure:"access_expire" yaml:"access_expire"` AccessExpire int `yaml:"access_expire"`
RefreshExpire int `mapstructure:"refresh_expire" yaml:"refresh_expire"` RefreshExpire int `yaml:"refresh_expire"`
AccessSecret string `mapstructure:"access_secret" yaml:"access_secret"` AccessSecret string `yaml:"access_secret"`
RefreshSecret string `mapstructure:"refresh_secret" yaml:"refresh_secret"` RefreshSecret string `yaml:"refresh_secret"`
Issuer string `mapstructure:"issuer" yaml:"issuer"` Issuer string `yaml:"issuer"`
} }
+7 -7
View File
@@ -1,11 +1,11 @@
package conf package conf
type LoggerConfig struct { type LoggerConfig struct {
Prefix string `mapstructure:"prefix" yaml:"prefix"` Prefix string `yaml:"prefix"`
Level string `mapstructure:"level" yaml:"level"` Level string `yaml:"level"`
Filename string `mapstructure:"filename" yaml:"filename"` Filename string `yaml:"filename"`
MaxSize int `mapstructure:"max_size" yaml:"max_size"` MaxSize int `yaml:"max_size"`
MaxBackups int `mapstructure:"max_backups" yaml:"max_backups"` MaxBackups int `yaml:"max_backups"`
MaxAge int `mapstructure:"max_age" yaml:"max_age"` MaxAge int `yaml:"max_age"`
Compress bool `mapstructure:"compress" yaml:"compress"` Compress bool `yaml:"compress"`
} }
+3 -3
View File
@@ -1,7 +1,7 @@
package conf package conf
type RedisConfig struct { type RedisConfig struct {
Addr string `mapstructure:"addr" yaml:"addr"` Addr string `yaml:"addr"`
Password string `mapstructure:"password" yaml:"password"` Password string `yaml:"password"`
Db int `mapstructure:"db" yaml:"db"` Db int `yaml:"db"`
} }
+4 -4
View File
@@ -1,8 +1,8 @@
package conf package conf
type ServerConfig struct { type ServerConfig struct {
Port int `mapstructure:"port" yaml:"port"` Port int `yaml:"port"`
Mode string `mapstructure:"mode" yaml:"mode"` // debug / release Mode string `yaml:"mode"` // debug / release
AllowOrigins []string `mapstructure:"allow_origins" yaml:"allow_origins"` AllowOrigins []string `yaml:"allow_origins"`
Version string `mapstructure:"mode" yaml:"version"` Version string `yaml:"version"`
} }