You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.2 KiB

  1. package config
  2. import (
  3. "fmt"
  4. "github.com/fsnotify/fsnotify"
  5. "github.com/spf13/viper"
  6. )
  7. type Config struct {
  8. MysqlAdmin MysqlAdmin `json:"mysqlAdmin"`
  9. Qiniu Qiniu `json:"qiniu"`
  10. CasbinConfig CasbinConfig `json:"casbinConfig"`
  11. RedisAdmin RedisAdmin `json:"redisAdmin"`
  12. System System `json:"system"`
  13. JWT JWT `json:"jwt"`
  14. Captcha Captcha `json:"captcha"`
  15. }
  16. type System struct { // 系统配置
  17. UseMultipoint bool `json:"useMultipoint"`
  18. Env string `json:"env"`
  19. Addr int `json:"addr"`
  20. }
  21. type JWT struct { // jwt签名
  22. SigningKey string `json:"signingKey"`
  23. }
  24. type CasbinConfig struct { //casbin配置
  25. ModelPath string `json:"modelPath"` // casbin model地址配置
  26. }
  27. type MysqlAdmin struct { // mysql admin 数据库配置
  28. Username string `json:"username"`
  29. Password string `json:"password"`
  30. Path string `json:"path"`
  31. Dbname string `json:"dbname"`
  32. Config string `json:"config"`
  33. MaxIdleConns int `json:"maxIdleConns"`
  34. MaxOpenConns int `json:"maxOpenConns"`
  35. LogMode bool `json:"maxOpenConns"`
  36. }
  37. type RedisAdmin struct { // Redis admin 数据库配置
  38. Addr string `json:"addr"`
  39. Password string `json:"password"`
  40. DB int `json:"db"`
  41. }
  42. type Qiniu struct { // 七牛 密钥配置
  43. AccessKey string `json:"accessKey"`
  44. SecretKey string `json:"secretKey"`
  45. }
  46. type Captcha struct { // 验证码配置
  47. KeyLong int `json:"keyLong"`
  48. ImgWidth int `json:"imgWidth"`
  49. ImgHeight int `json:"imgHeight"`
  50. }
  51. var GinVueAdminconfig Config
  52. var VTool *viper.Viper
  53. func init() {
  54. v := viper.New()
  55. v.SetConfigName("config") // 设置配置文件名 (不带后缀)
  56. v.AddConfigPath("./static/config/") // 第一个搜索路径
  57. v.SetConfigType("json")
  58. err := v.ReadInConfig() // 搜索路径,并读取配置数据
  59. if err != nil {
  60. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  61. }
  62. v.WatchConfig()
  63. v.OnConfigChange(func(e fsnotify.Event) {
  64. fmt.Println("Config file changed:", e.Name)
  65. if err := v.Unmarshal(&GinVueAdminconfig); err != nil {
  66. fmt.Println(err)
  67. }
  68. })
  69. if err := v.Unmarshal(&GinVueAdminconfig); err != nil {
  70. fmt.Println(err)
  71. }
  72. VTool = v
  73. }