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.

80 lines
2.0 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. }
  34. type RedisAdmin struct { // Redis admin 数据库配置
  35. Addr string `json:"addr"`
  36. Password string `json:"password"`
  37. DB int `json:"db"`
  38. }
  39. type Qiniu struct { // 七牛 密钥配置
  40. AccessKey string `json:"accessKey"`
  41. SecretKey string `json:"secretKey"`
  42. }
  43. type Captcha struct { // 验证码配置
  44. KeyLong int `json:"keyLong"`
  45. ImgWidth int `json:"imgWidth"`
  46. ImgHeight int `json:"imgHeight"`
  47. }
  48. var GinVueAdminconfig Config
  49. var VTool *viper.Viper
  50. func init() {
  51. v := viper.New()
  52. v.SetConfigName("config") // 设置配置文件名 (不带后缀)
  53. v.AddConfigPath("./static/config/") // 第一个搜索路径
  54. v.SetConfigType("json")
  55. err := v.ReadInConfig() // 搜索路径,并读取配置数据
  56. if err != nil {
  57. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  58. }
  59. v.WatchConfig()
  60. v.OnConfigChange(func(e fsnotify.Event) {
  61. fmt.Println("Config file changed:", e.Name)
  62. if err := v.Unmarshal(&GinVueAdminconfig); err != nil {
  63. fmt.Println(err)
  64. }
  65. })
  66. if err := v.Unmarshal(&GinVueAdminconfig); err != nil {
  67. fmt.Println(err)
  68. }
  69. VTool = v
  70. }