zhangzengfei
2024-03-30 e9e0c1f3abf1bbf83ebdb933d318caa2a45c15b0
config/config.go
@@ -1,33 +1,14 @@
package config
import (
   "fmt"
   "basic.com/valib/logger.git"
   "github.com/fsnotify/fsnotify"
   "github.com/spf13/viper"
   "log"
)
type server struct {
   AnalyServerId  string `mapstructure:"analyServerId"`
   NetworkAdapter string `mapstructure:"networkAdapter"`
}
var Server = &server{}
type esinfo struct {
   EsIndex esindexlist `mapstructure:"index"`
}
type esindexlist struct {
   AiOcean index `mapstructure:"aiOcean"`
}
type index struct {
   IndexName string `mapstructure:"index"`
   IndexType string `mapstructure:"type"`
}
type dbpersoncompare struct {
type database struct {
   MysqlAddr   string `mapstructure:"mysqlAddr"`
   Username    string `mapstructure:"username"`
   Password    string `mapstructure:"password"`
@@ -36,17 +17,7 @@
   ServePort   int    `mapstructure:"servePort"`
}
type espersoncompare struct {
   ServePort int    `mapstructure:"servePort"`
   ESIP      string `mapstructure:"esip"`
   ESPort    string `mapstructure:"esPort"`
}
var DbPersonCompInfo = &dbpersoncompare{}
var EsCompServerInfo = &espersoncompare{}
var EsInfo = &esinfo{}
var DbPersonCompInfo = &database{}
type LogConfig struct {
   Path       string `mapstructure:"path"`       //日志存储路径
@@ -58,29 +29,31 @@
var LogConf = &LogConfig{}
func Init(env string) {
func Init() error {
   var err error
   v := viper.New()
   v.SetConfigType("yaml")
   v.SetConfigName(env)
   v.SetConfigName("compare")
   v.AddConfigPath("./")
   v.AddConfigPath("./config/")
   err = v.ReadInConfig()
   if err != nil {
      log.Fatal("error on parsing configuration file")
      fmt.Printf("error on parsing configuration file, %s, config file compare.yaml\n", err.Error())
      return err
   }
   read2Conf(v)
   v.WatchConfig()
   v.OnConfigChange(func(in fsnotify.Event) {
      read2Conf(v)
   })
   return nil
}
func read2Conf(v *viper.Viper) {
   v.UnmarshalKey("es", EsInfo)
   v.UnmarshalKey("server", Server)
   v.UnmarshalKey("dbpersoncompare", DbPersonCompInfo)
   v.UnmarshalKey("espersoncompare", EsCompServerInfo)
   v.UnmarshalKey("database", DbPersonCompInfo)
   v.UnmarshalKey("log", LogConf)
   logger.SetLevel(LogConf.Level)
}