package config import ( "github.com/fsnotify/fsnotify" "log" "model-engine/pkg/logger" "model-engine/pkg/mysqlx" "github.com/spf13/viper" ) var LogConf = &logger.LogConf{} type esInfo struct { Ip string `mapstructure:"ip"` Port string `mapstructure:"port"` Shards string `mapstructure:"shards"` EsIndex esIndexlist `mapstructure:"index"` StorePath []string `mapstructure:"storePath"` ThresholdTime int `mapstructure:"thresholdTime"` ThresholdStayTime int `mapstructure:"thresholdStayTime"` } type esIndexlist struct { AiOcean index `mapstructure:"aiocean"` UserLog index `mapstructure:"userLog"` } type index struct { IndexName string `mapstructure:"index"` IndexType string `mapstructure:"type"` } var EsInfo = &esInfo{} var MysqlConf = &mysqlx.Conf{} // Init is an exported method that takes the environment starts the viper // (external lib) and returns the configuration struct. func Init() { var err error v := viper.New() v.SetConfigType("yaml") v.SetConfigName("model-engine") v.AddConfigPath("../config/") v.AddConfigPath("./config/") err = v.ReadInConfig() if err != nil { log.Fatal("error on parsing configuration file") } read2Conf(v) v.WatchConfig() v.OnConfigChange(func(in fsnotify.Event) { read2Conf(v) }) showConfig() } func read2Conf(v *viper.Viper) { v.UnmarshalKey("log", LogConf) v.UnmarshalKey("mysqlConf", MysqlConf) v.UnmarshalKey("es", EsInfo) } func showConfig() { log.Println("......................................................") log.Printf(" log: %+v", LogConf) log.Printf(" EsInfo: %+v", EsInfo) log.Printf(" mysqlConf: %+v", MysqlConf) }