config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package init
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "github.com/fsnotify/fsnotify"
  6. "github.com/spf13/viper"
  7. )
  8. type Config struct {
  9. MysqlAdmin MysqlAdmin `json:"mysqlAdmin"`
  10. Qiniu Qiniu `json:"qiniu"`
  11. CasbinConfig CasbinConfig `json:"casbinConfig"`
  12. RedisAdmin RedisAdmin `json:"redisAdmin"`
  13. System System `json:"system"`
  14. JWT JWT `json:"jwt"`
  15. Captcha Captcha `json:"captcha"`
  16. Log Log `json:"log"`
  17. }
  18. type System struct { // 系统配置
  19. UseMultipoint bool `json:"useMultipoint"`
  20. Env string `json:"env"`
  21. Addr int `json:"addr"`
  22. }
  23. type JWT struct { // jwt签名
  24. SigningKey string `json:"signingKey"`
  25. }
  26. type CasbinConfig struct { //casbin配置
  27. ModelPath string `json:"modelPath"` // casbin model地址配置
  28. }
  29. type MysqlAdmin struct { // mysql admin 数据库配置
  30. Username string `json:"username"`
  31. Password string `json:"password"`
  32. Path string `json:"path"`
  33. Dbname string `json:"dbname"`
  34. Config string `json:"config"`
  35. MaxIdleConns int `json:"maxIdleConns"`
  36. MaxOpenConns int `json:"maxOpenConns"`
  37. LogMode bool `json:"maxOpenConns"`
  38. }
  39. type RedisAdmin struct { // Redis admin 数据库配置
  40. Addr string `json:"addr"`
  41. Password string `json:"password"`
  42. DB int `json:"db"`
  43. }
  44. type Qiniu struct { // 七牛 密钥配置
  45. AccessKey string `json:"accessKey"`
  46. SecretKey string `json:"secretKey"`
  47. }
  48. type Captcha struct { // 验证码配置
  49. KeyLong int `json:"keyLong"`
  50. ImgWidth int `json:"imgWidth"`
  51. ImgHeight int `json:"imgHeight"`
  52. }
  53. /**
  54. Log Config
  55. "CRITICAL"
  56. "ERROR"
  57. "WARNING"
  58. "NOTICE"
  59. "INFO"
  60. "DEBUG"
  61. */
  62. type Log struct {
  63. // log 打印的前缀
  64. Prefix string `json:"prefix"`
  65. // 是否显示打印log的文件具体路径
  66. LogFile bool `json:"logFile"`
  67. // 在控制台打印log的级别, []默认不打印
  68. Stdout []string `json:"stdout"`
  69. // 在文件中打印log的级别 []默认不打印
  70. File []string `json:"file"`
  71. }
  72. func init() {
  73. v := viper.New()
  74. v.SetConfigName("config") // 设置配置文件名 (不带后缀)
  75. v.AddConfigPath("/") // 第一个搜索路径
  76. v.SetConfigType("json")
  77. err := v.ReadInConfig() // 搜索路径,并读取配置数据
  78. if err != nil {
  79. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  80. }
  81. v.WatchConfig()
  82. v.OnConfigChange(func(e fsnotify.Event) {
  83. fmt.Println("config file changed:", e.Name)
  84. if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
  85. fmt.Println(err)
  86. }
  87. })
  88. if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
  89. fmt.Println(err)
  90. }
  91. global.GVA_VP = v
  92. }