configs.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package configs
  2. import (
  3. "bytes"
  4. _ "embed"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "time"
  9. "github.com/xinliangnote/go-gin-api/pkg/env"
  10. "github.com/xinliangnote/go-gin-api/pkg/file"
  11. "github.com/fsnotify/fsnotify"
  12. "github.com/spf13/viper"
  13. )
  14. var config = new(Config)
  15. type Config struct {
  16. MySQL struct {
  17. Read struct {
  18. Addr string `toml:"addr"`
  19. User string `toml:"user"`
  20. Pass string `toml:"pass"`
  21. Name string `toml:"name"`
  22. } `toml:"read"`
  23. Write struct {
  24. Addr string `toml:"addr"`
  25. User string `toml:"user"`
  26. Pass string `toml:"pass"`
  27. Name string `toml:"name"`
  28. } `toml:"write"`
  29. Base struct {
  30. MaxOpenConn int `toml:"maxOpenConn"`
  31. MaxIdleConn int `toml:"maxIdleConn"`
  32. ConnMaxLifeTime time.Duration `toml:"connMaxLifeTime"`
  33. } `toml:"base"`
  34. } `toml:"mysql"`
  35. Redis struct {
  36. Addr string `toml:"addr"`
  37. Pass string `toml:"pass"`
  38. Db int `toml:"db"`
  39. MaxRetries int `toml:"maxRetries"`
  40. PoolSize int `toml:"poolSize"`
  41. MinIdleConns int `toml:"minIdleConns"`
  42. } `toml:"redis"`
  43. Mail struct {
  44. Host string `toml:"host"`
  45. Port int `toml:"port"`
  46. User string `toml:"user"`
  47. Pass string `toml:"pass"`
  48. To string `toml:"to"`
  49. } `toml:"mail"`
  50. HashIds struct {
  51. Secret string `toml:"secret"`
  52. Length int `toml:"length"`
  53. } `toml:"hashids"`
  54. Language struct {
  55. Local string `toml:"local"`
  56. } `toml:"language"`
  57. }
  58. var (
  59. //go:embed dev_configs.toml
  60. devConfigs []byte
  61. //go:embed fat_configs.toml
  62. fatConfigs []byte
  63. //go:embed uat_configs.toml
  64. uatConfigs []byte
  65. //go:embed pro_configs.toml
  66. proConfigs []byte
  67. )
  68. func init() {
  69. var r io.Reader
  70. switch env.Active().Value() {
  71. case "dev":
  72. r = bytes.NewReader(devConfigs)
  73. case "fat":
  74. r = bytes.NewReader(fatConfigs)
  75. case "uat":
  76. r = bytes.NewReader(uatConfigs)
  77. case "pro":
  78. r = bytes.NewReader(proConfigs)
  79. default:
  80. r = bytes.NewReader(fatConfigs)
  81. }
  82. viper.SetConfigType("toml")
  83. if err := viper.ReadConfig(r); err != nil {
  84. panic(err)
  85. }
  86. if err := viper.Unmarshal(config); err != nil {
  87. panic(err)
  88. }
  89. viper.SetConfigName(env.Active().Value() + "_configs")
  90. viper.AddConfigPath("./configs")
  91. configFile := "./configs/" + env.Active().Value() + "_configs.toml"
  92. _, ok := file.IsExists(configFile)
  93. if !ok {
  94. if err := os.MkdirAll(filepath.Dir(configFile), 0766); err != nil {
  95. panic(err)
  96. }
  97. f, err := os.Create(configFile)
  98. if err != nil {
  99. panic(err)
  100. }
  101. defer f.Close()
  102. if err := viper.WriteConfig(); err != nil {
  103. panic(err)
  104. }
  105. }
  106. viper.WatchConfig()
  107. viper.OnConfigChange(func(e fsnotify.Event) {
  108. if err := viper.Unmarshal(config); err != nil {
  109. panic(err)
  110. }
  111. })
  112. }
  113. func Get() Config {
  114. return *config
  115. }