config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package config
  2. import (
  3. "log"
  4. "github.com/spf13/viper"
  5. )
  6. // Config 应用配置结构
  7. type Config struct {
  8. Server ServerConfig `mapstructure:"server"`
  9. Database DatabaseConfig `mapstructure:"database"`
  10. Redis RedisConfig `mapstructure:"redis"`
  11. JWT JWTConfig `mapstructure:"jwt"`
  12. Upload UploadConfig `mapstructure:"upload"`
  13. Log LogConfig `mapstructure:"log"`
  14. }
  15. // ServerConfig 服务器配置
  16. type ServerConfig struct {
  17. Port string `mapstructure:"port"`
  18. Mode string `mapstructure:"mode"`
  19. ReadTimeout int `mapstructure:"read_timeout"`
  20. WriteTimeout int `mapstructure:"write_timeout"`
  21. }
  22. // DatabaseConfig 数据库配置
  23. type DatabaseConfig struct {
  24. Host string `mapstructure:"host"`
  25. Port string `mapstructure:"port"`
  26. User string `mapstructure:"user"`
  27. Password string `mapstructure:"password"`
  28. DBName string `mapstructure:"dbname"`
  29. MaxOpenConns int `mapstructure:"max_open_conns"`
  30. MaxIdleConns int `mapstructure:"max_idle_conns"`
  31. ConnMaxLifetime int `mapstructure:"conn_max_lifetime"`
  32. }
  33. // RedisConfig Redis配置
  34. type RedisConfig struct {
  35. Host string `mapstructure:"host"`
  36. Port string `mapstructure:"port"`
  37. Password string `mapstructure:"password"`
  38. DB int `mapstructure:"db"`
  39. PoolSize int `mapstructure:"pool_size"`
  40. }
  41. // JWTConfig JWT配置
  42. type JWTConfig struct {
  43. Secret string `mapstructure:"secret"`
  44. Expire int `mapstructure:"expire"`
  45. }
  46. // UploadConfig 文件上传配置
  47. type UploadConfig struct {
  48. Path string `mapstructure:"path"`
  49. MaxSize int `mapstructure:"max_size"`
  50. AllowedTypes []string `mapstructure:"allowed_types"`
  51. }
  52. // LogConfig 日志配置
  53. type LogConfig struct {
  54. Level string `mapstructure:"level"`
  55. Output string `mapstructure:"output"`
  56. MaxSize int `mapstructure:"max_size"`
  57. MaxBackups int `mapstructure:"max_backups"`
  58. MaxAge int `mapstructure:"max_age"`
  59. }
  60. // LoadConfig 加载配置文件
  61. func LoadConfig(configPath string) (*Config, error) {
  62. viper.SetConfigFile(configPath)
  63. viper.SetConfigType("yaml")
  64. // 设置默认值
  65. setDefaults()
  66. // 环境变量覆盖
  67. viper.AutomaticEnv()
  68. if err := viper.ReadInConfig(); err != nil {
  69. log.Printf("Error reading config file: %v", err)
  70. return nil, err
  71. }
  72. var config Config
  73. if err := viper.Unmarshal(&config); err != nil {
  74. log.Printf("Error unmarshaling config: %v", err)
  75. return nil, err
  76. }
  77. return &config, nil
  78. }
  79. // setDefaults 设置默认配置值
  80. func setDefaults() {
  81. // 服务器默认配置
  82. viper.SetDefault("server.port", "8080")
  83. viper.SetDefault("server.mode", "debug")
  84. viper.SetDefault("server.read_timeout", 60)
  85. viper.SetDefault("server.write_timeout", 60)
  86. // 数据库默认配置
  87. viper.SetDefault("database.host", "localhost")
  88. viper.SetDefault("database.port", "3306")
  89. viper.SetDefault("database.user", "root")
  90. viper.SetDefault("database.password", "password")
  91. viper.SetDefault("database.dbname", "web_training")
  92. viper.SetDefault("database.max_open_conns", 100)
  93. viper.SetDefault("database.max_idle_conns", 10)
  94. viper.SetDefault("database.conn_max_lifetime", 3600)
  95. // Redis默认配置
  96. viper.SetDefault("redis.host", "localhost")
  97. viper.SetDefault("redis.port", "6379")
  98. viper.SetDefault("redis.password", "")
  99. viper.SetDefault("redis.db", 0)
  100. viper.SetDefault("redis.pool_size", 10)
  101. // JWT默认配置
  102. viper.SetDefault("jwt.secret", "your-secret-key")
  103. viper.SetDefault("jwt.expire", 86400)
  104. // 上传默认配置
  105. viper.SetDefault("upload.path", "./uploads")
  106. viper.SetDefault("upload.max_size", 10)
  107. viper.SetDefault("upload.allowed_types", []string{"jpg", "jpeg", "png", "gif"})
  108. // 日志默认配置
  109. viper.SetDefault("log.level", "info")
  110. viper.SetDefault("log.output", "logs/app.log")
  111. viper.SetDefault("log.max_size", 100)
  112. viper.SetDefault("log.max_backups", 3)
  113. viper.SetDefault("log.max_age", 28)
  114. }