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.

38 lines
785 B

  1. package config
  2. import (
  3. "fmt"
  4. "github.com/fsnotify/fsnotify"
  5. "github.com/spf13/viper"
  6. )
  7. type Config struct {
  8. Admin Admin
  9. }
  10. type Admin struct {
  11. Username string
  12. Password string
  13. Path string
  14. Dbname string
  15. Config string
  16. }
  17. var Dbconfig Config
  18. func init() {
  19. v := viper.New()
  20. v.SetConfigName("config") // 设置配置文件名 (不带后缀)
  21. v.AddConfigPath("./config/dbconfig/") // 第一个搜索路径
  22. v.SetConfigType("json")
  23. err := v.ReadInConfig() // 搜索路径,并读取配置数据
  24. if err != nil {
  25. panic(fmt.Errorf("Fatal error config file: %s \n", err))
  26. }
  27. v.WatchConfig()
  28. v.OnConfigChange(func(e fsnotify.Event) {
  29. fmt.Println("Config file changed:", e.Name)
  30. })
  31. if err := v.Unmarshal(&Dbconfig); err != nil {
  32. fmt.Println(err)
  33. }
  34. }