settings.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/fsnotify/fsnotify"
  5. "github.com/spf13/viper"
  6. )
  7. var Conf = new(AppConfig)
  8. type AppConfig struct {
  9. Mode string `mapstructure:"mode"`
  10. Port int `mapstructure:"port"`
  11. CacheDir string `mapstructure:"cache_dir"`
  12. Upload *UploadConfig `mapstructure:"upload"`
  13. ReplicasForUser *MySQLConfig `mapstructure:"replicas[user]""`
  14. ReplicasForOrder *MySQLConfig `mapstructure:"replicas[order]""`
  15. ReplicasForOther *MySQLConfig `mapstructure:"replicas[other]""`
  16. *EncryptConfig `mapstructure:"encrypt"`
  17. *JWTConfig `mapstructure:"jwt"`
  18. *LogConfig `mapstructure:"log"`
  19. *MySQLConfig `mapstructure:"mysql"`
  20. *RedisConfig `mapstructure:"redis"`
  21. }
  22. type EncryptConfig struct {
  23. Key string `mapstructure:"key"`
  24. ExpireAt int64 `mapstructure:"expire_at"`
  25. }
  26. type UploadConfig struct {
  27. Dir string `mapstructure:"dir"`
  28. Fonts string `mapstructure:"fonts"`
  29. Material string `mapstructure:"material"`
  30. MaxFileSize int64 `mapstructure:"max_file_size"`
  31. MaxSize string `mapstructure:"max_size"`
  32. }
  33. type JWTConfig struct {
  34. Prefix string `mapstructure:"prefix"`
  35. Secret string `mapstructure:"secret"`
  36. Project string `mapstructure:"project"`
  37. ExpireAt int64 `mapstructure:"expire_at"`
  38. }
  39. type LogConfig struct {
  40. Level string `mapstructure:"level"`
  41. WebLogName string `mapstructure:"web_log_name"`
  42. LogFilePath string `mapstructure:"log_file_path"`
  43. }
  44. type MySQLConfig struct {
  45. Host string `mapstructure:"host"`
  46. User string `mapstructure:"user"`
  47. Password string `mapstructure:"password"`
  48. DB string `mapstructure:"dbname"`
  49. Port int `mapstructure:"port"`
  50. MaxOpenConns int `mapstructure:"max_open_conns"`
  51. MaxIdleConns int `mapstructure:"max_idle_conns"`
  52. }
  53. type RedisConfig struct {
  54. Host string `mapstructure:"host"`
  55. Password string `mapstructure:"password"`
  56. Port int `mapstructure:"port"`
  57. DB int `mapstructure:"db"`
  58. PoolSize int `mapstructure:"pool_size"`
  59. MinIdleConns int `mapstructure:"min_idle_conns"`
  60. }
  61. var (
  62. devFilePath = "./config/config.dev.yaml"
  63. releaseFilePath = "./config/config.online.yaml"
  64. localFilePath = "./config/config.local.yaml"
  65. testFilePath = "../config/config.local.yaml"
  66. )
  67. func Init(mode string) {
  68. var filePath string
  69. if mode == "dev" {
  70. filePath = devFilePath
  71. } else if mode == "release" {
  72. filePath = releaseFilePath
  73. } else if mode == "test" { //for test only
  74. filePath = testFilePath
  75. } else { // local
  76. filePath = localFilePath
  77. }
  78. viper.SetConfigFile(filePath)
  79. err := viper.ReadInConfig() // 读取配置信息
  80. if err != nil {
  81. // 读取配置信息失败
  82. panic(fmt.Sprintf("viper.ReadInConfig failed, err:%v\n", err))
  83. }
  84. // 把读取到的配置信息反序列化到 Conf 变量中
  85. if err := viper.Unmarshal(Conf); err != nil {
  86. fmt.Printf("viper.Unmarshal failed, err:%v\n", err)
  87. }
  88. viper.WatchConfig()
  89. viper.OnConfigChange(func(in fsnotify.Event) {
  90. fmt.Println("配置文件修改了...")
  91. if err := viper.Unmarshal(Conf); err != nil {
  92. panic(fmt.Sprintf("viper.Unmarshal failed, err:%v\n", err))
  93. }
  94. })
  95. }