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.

30 lines
728 B

  1. package core
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "github.com/fsnotify/fsnotify"
  6. "github.com/spf13/viper"
  7. )
  8. func init() {
  9. v := viper.New()
  10. v.SetConfigName("config") // 设置配置文件名 (不带后缀)
  11. v.AddConfigPath("./") // 第一个搜索路径
  12. v.SetConfigType("json")
  13. err := v.ReadInConfig() // 搜索路径,并读取配置数据
  14. if err != nil {
  15. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  16. }
  17. v.WatchConfig()
  18. v.OnConfigChange(func(e fsnotify.Event) {
  19. fmt.Println("config file changed:", e.Name)
  20. if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
  21. fmt.Println(err)
  22. }
  23. })
  24. if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
  25. fmt.Println(err)
  26. }
  27. global.GVA_VP = v
  28. }