zhangzengfei
2023-05-22 acb15a431beb73159921477415c5d0f7d06c6768
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
71
72
73
74
75
76
77
78
79
80
81
82
83
package config
 
import (
    "fmt"
 
    "serfNode/logger"
 
    "github.com/fsnotify/fsnotify"
    "github.com/spf13/viper"
)
 
type WebConfig struct {
    Port string `mapstructure:"port"`
}
 
type SerfConfig struct {
    ClusterName string `mapstructure:"clusterName"`
    NodeName    string `mapstructure:"nodeName"`
    BindAddr    string `mapstructure:"bindAddr"`
    RPCAddr     string `mapstructure:"rpcAddr"`
    RPCPort     int    `mapstructure:"rpcPort"`
    AuthKey     string `mapstructure:"authKey"`
}
 
type LogConfig struct {
    Path       string `mapstructure:"path"`       //日志存储路径
    Level      int    `mapstructure:"level"`      //日志等级
    MaxSize    int    `mapstructure:"maxSize"`    //日志文件大小上限
    MaxBackups int    `mapstructure:"maxBackups"` //日志压缩包个数
    MaxAge     int    `mapstructure:"maxAge"`     //保留压缩包天数
}
 
type VRRPConfig struct {
    NetworkAdapter string `mapstructure:"networkAdapter"` // 网口
    VirtualIp      string `mapstructure:"virtualIp"`      // 虚拟ip
    VirtualId      byte   `mapstructure:"virtualId"`      // vrrp id
}
 
type NsqServerConfig struct {
    ServerAddr string `mapstructure:"serverAddr"`
}
 
var SerfConf = &SerfConfig{}
var LogConf = &LogConfig{}
var WebConf = &WebConfig{}
var VRRPConf = &VRRPConfig{}
var NsqConf = &NsqServerConfig{}
 
// 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("config")
    v.AddConfigPath("./config/")
 
    err = v.ReadInConfig()
    if err != nil {
        fmt.Println("error on parsing configuration file", err)
        panic(err)
    }
 
    read2Conf(v)
 
    v.WatchConfig()
    v.OnConfigChange(func(e fsnotify.Event) {
        read2Conf(v)
    })
}
 
func read2Conf(v *viper.Viper) {
 
    // 暂时注释掉, 通过node-red获取serf配置
    //v.UnmarshalKey("serf", SerfConf)
 
    v.UnmarshalKey("log", LogConf)
    v.UnmarshalKey("web", WebConf)
    v.UnmarshalKey("vrrp", VRRPConf)
    v.UnmarshalKey("nsq", NsqConf)
 
    logger.SetLevel(LogConf.Level)
}