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)
|
}
|