package config import ( "log" "github.com/spf13/viper" ) // Config 应用配置结构 type Config struct { Server ServerConfig `mapstructure:"server"` Database DatabaseConfig `mapstructure:"database"` Redis RedisConfig `mapstructure:"redis"` JWT JWTConfig `mapstructure:"jwt"` Upload UploadConfig `mapstructure:"upload"` Log LogConfig `mapstructure:"log"` } // ServerConfig 服务器配置 type ServerConfig struct { Port string `mapstructure:"port"` Mode string `mapstructure:"mode"` ReadTimeout int `mapstructure:"read_timeout"` WriteTimeout int `mapstructure:"write_timeout"` } // DatabaseConfig 数据库配置 type DatabaseConfig struct { Host string `mapstructure:"host"` Port string `mapstructure:"port"` User string `mapstructure:"user"` Password string `mapstructure:"password"` DBName string `mapstructure:"dbname"` MaxOpenConns int `mapstructure:"max_open_conns"` MaxIdleConns int `mapstructure:"max_idle_conns"` ConnMaxLifetime int `mapstructure:"conn_max_lifetime"` } // RedisConfig Redis配置 type RedisConfig struct { Host string `mapstructure:"host"` Port string `mapstructure:"port"` Password string `mapstructure:"password"` DB int `mapstructure:"db"` PoolSize int `mapstructure:"pool_size"` } // JWTConfig JWT配置 type JWTConfig struct { Secret string `mapstructure:"secret"` Expire int `mapstructure:"expire"` } // UploadConfig 文件上传配置 type UploadConfig struct { Path string `mapstructure:"path"` MaxSize int `mapstructure:"max_size"` AllowedTypes []string `mapstructure:"allowed_types"` } // LogConfig 日志配置 type LogConfig struct { Level string `mapstructure:"level"` Output string `mapstructure:"output"` MaxSize int `mapstructure:"max_size"` MaxBackups int `mapstructure:"max_backups"` MaxAge int `mapstructure:"max_age"` } // LoadConfig 加载配置文件 func LoadConfig(configPath string) (*Config, error) { viper.SetConfigFile(configPath) viper.SetConfigType("yaml") // 设置默认值 setDefaults() // 环境变量覆盖 viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { log.Printf("Error reading config file: %v", err) return nil, err } var config Config if err := viper.Unmarshal(&config); err != nil { log.Printf("Error unmarshaling config: %v", err) return nil, err } return &config, nil } // setDefaults 设置默认配置值 func setDefaults() { // 服务器默认配置 viper.SetDefault("server.port", "8080") viper.SetDefault("server.mode", "debug") viper.SetDefault("server.read_timeout", 60) viper.SetDefault("server.write_timeout", 60) // 数据库默认配置 viper.SetDefault("database.host", "localhost") viper.SetDefault("database.port", "3306") viper.SetDefault("database.user", "root") viper.SetDefault("database.password", "password") viper.SetDefault("database.dbname", "web_training") viper.SetDefault("database.max_open_conns", 100) viper.SetDefault("database.max_idle_conns", 10) viper.SetDefault("database.conn_max_lifetime", 3600) // Redis默认配置 viper.SetDefault("redis.host", "localhost") viper.SetDefault("redis.port", "6379") viper.SetDefault("redis.password", "") viper.SetDefault("redis.db", 0) viper.SetDefault("redis.pool_size", 10) // JWT默认配置 viper.SetDefault("jwt.secret", "your-secret-key") viper.SetDefault("jwt.expire", 86400) // 上传默认配置 viper.SetDefault("upload.path", "./uploads") viper.SetDefault("upload.max_size", 10) viper.SetDefault("upload.allowed_types", []string{"jpg", "jpeg", "png", "gif"}) // 日志默认配置 viper.SetDefault("log.level", "info") viper.SetDefault("log.output", "logs/app.log") viper.SetDefault("log.max_size", 100) viper.SetDefault("log.max_backups", 3) viper.SetDefault("log.max_age", 28) }