zhangzengfei
2024-02-23 b4f52495ea8c990bba30efcb3f36a6167cc85bab
config/config.go
@@ -1,25 +1,34 @@
package config
import (
   "github.com/fsnotify/fsnotify"
   "github.com/spf13/viper"
   "log"
)
type servUrls struct {
type common struct {
   EsUrl     string `mapstructure: "esUrl"`
   ServerUrl string `mapstructure: "serverUrl"`
   OrgName   string `mapstructure: "orgName"`
}
var ServUrls = &servUrls{}
var Options = &common{}
func Init(env string) {
func Init() {
   var err error
   viper.SetConfigType("yaml")
   viper.SetConfigName(env)
   viper.SetConfigName("esSync")
   viper.AddConfigPath("../config")
   viper.AddConfigPath("config")
   viper.AddConfigPath("")
   err = viper.ReadInConfig()
   if err != nil {
      log.Fatal("error on parsing configuration file", err)
   }
   viper.UnmarshalKey("servUrls", ServUrls)
   viper.UnmarshalKey("common", Options)
   viper.WatchConfig()
   viper.OnConfigChange(func(in fsnotify.Event) {
      viper.UnmarshalKey("common", Options)
   })
}