refactor(conf): 移除冗余的 mapstructure 结构体标签
This commit is contained in:
1 parent
7f68cecf21
commit
ddf28f59cf
6 files changed
+37
-32
No files matched your search
+12
-7
@@ -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("🚀 配置热重载成功,新配置已即时生效!")
|
||||
|
||||
@@ -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 字符串
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
Reference in new issue
Block a user