69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package config
|
|||
|
|
|
||
|
|
import "github.com/spf13/viper"
|
||
|
|
|
||
|
|
type Config struct {
|
||
|
|
OSI OSIConfig `mapstructure:"osi"`
|
||
|
|
PHIS PHISConfig `mapstructure:"phis"`
|
||
|
|
Redis RedisConfig `mapstructure:"redis"`
|
||
|
|
ReportLogFileDir string `mapstructure:"report_log_file_dir"`
|
||
|
|
RetryMax int `mapstructure:"retry_max"`
|
||
|
|
CircuitFailThreshold int `mapstructure:"circuit_fail_threshold"`
|
||
|
|
CircuitSleepSec int `mapstructure:"circuit_sleep_sec"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type OSIConfig struct {
|
||
|
|
BaseURL string `mapstructure:"base_url"`
|
||
|
|
OrgCode string `mapstructure:"org_code"`
|
||
|
|
UserName string `mapstructure:"user_name"`
|
||
|
|
Ask string `mapstructure:"ask"`
|
||
|
|
DeviceSN string `mapstructure:"device_sn"`
|
||
|
|
OperateUser string `mapstructure:"operate_user"`
|
||
|
|
TimeoutSec int `mapstructure:"timeout_sec"`
|
||
|
|
Socks5Proxy string `mapstructure:"socks5_proxy"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type PHISConfig struct {
|
||
|
|
BaseURL string `mapstructure:"base_url"`
|
||
|
|
Token string `mapstructure:"token"`
|
||
|
|
RegionCode string `mapstructure:"region_code"`
|
||
|
|
PollIntervalSec int `mapstructure:"poll_interval_sec"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type RedisConfig struct {
|
||
|
|
Addr string `mapstructure:"addr"`
|
||
|
|
DB int `mapstructure:"db"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func Load(path string) (Config, error) {
|
||
|
|
v := viper.New()
|
||
|
|
setDefaults(v)
|
||
|
|
|
||
|
|
if path != "" {
|
||
|
|
v.SetConfigFile(path)
|
||
|
|
v.SetConfigType("yaml")
|
||
|
|
if err := v.ReadInConfig(); err != nil {
|
||
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
||
|
|
return Config{}, err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var cfg Config
|
||
|
|
if err := v.Unmarshal(&cfg); err != nil {
|
||
|
|
return Config{}, err
|
||
|
|
}
|
||
|
|
return cfg, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func setDefaults(v *viper.Viper) {
|
||
|
|
v.SetDefault("osi.base_url", "http://<osi-host>")
|
||
|
|
v.SetDefault("osi.timeout_sec", 20)
|
||
|
|
v.SetDefault("phis.poll_interval_sec", 5)
|
||
|
|
v.SetDefault("redis.db", 0)
|
||
|
|
v.SetDefault("report_log_file_dir", "logs")
|
||
|
|
v.SetDefault("retry_max", 3)
|
||
|
|
v.SetDefault("circuit_fail_threshold", 5)
|
||
|
|
v.SetDefault("circuit_sleep_sec", 60)
|
||
|
|
}
|