zhangqian
2024-12-12 66f3c0e80750f288d13f23360b550b8aff1dbc04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)
}