package config import ( "log" "github.com/spf13/viper" ) type Config struct { DB DBConfig Server ServerConfig Redis RedisConfig JWT JWTConfig Log LogConfig // 新增日志配置 } type DBConfig struct { Host string Port string User string Password string Name string } type ServerConfig struct { Port string } type RedisConfig struct { Host string Port string Password string DB int } type JWTConfig struct { Secret string Expire int // 小时 } // 新增日志配置结构 type LogConfig struct { Level string // 日志级别: debug, info, warn, error Path string // 日志文件路径 } func LoadConfig() *Config { viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath(".") viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { log.Fatalf("Error reading config file: %v", err) } var cfg Config if err := viper.Unmarshal(&cfg); err != nil { log.Fatalf("Unable to decode into struct: %v", err) } return &cfg }