us-coach-spider/pkg/options/init.go

62 lines
1.6 KiB
Go

package options
import (
"fmt"
"os"
"strconv"
"time"
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"github.com/golang/glog"
"gopkg.in/yaml.v3"
)
const (
ProxyConfigEnv = "PROXY_CONFIG_PATH"
DefaultProxyConfigPath = "/cfg/proxy.yaml"
)
type Config struct {
Proxy proxy.Option `yaml:"proxy"`
ProviderId v2.ProviderId `yaml:"provider_id"`
WatchInterval time.Duration
AtsInterval time.Duration
AtsThreshold int // 库存一定时间内减少多少个通知
}
func LoadConfigs() (opt *Config, err error) {
opt = &Config{}
// 从环境变量中读取ProviderId
opt.ProviderId = v2.ProviderId(os.Getenv("PROVIDER_ID"))
if opt.ProviderId == "" {
return nil, fmt.Errorf("环境变量未配置供应商id")
}
opt.WatchInterval, _ = time.ParseDuration(os.Getenv("WATCH_INTERVAL"))
opt.AtsInterval, _ = time.ParseDuration(os.Getenv("ATS_INTERVAL"))
if opt.WatchInterval == 0 {
opt.WatchInterval = 5 * time.Minute
}
if opt.AtsInterval == 0 {
opt.AtsInterval = 15 * time.Minute
}
opt.AtsThreshold, _ = strconv.Atoi(os.Getenv("ATS_THRESHOLD"))
if opt.AtsThreshold == 0 {
opt.AtsThreshold = 40
}
glog.Infof("加载watch interval %s\nats interval %s\nats threshold %d", opt.WatchInterval, opt.AtsInterval, opt.AtsThreshold)
// 加载代理配置
cfgPath := os.Getenv(ProxyConfigEnv)
if cfgPath == "" {
cfgPath = DefaultProxyConfigPath
}
f, err := os.Open(cfgPath)
if err != nil {
return nil, err
}
defer f.Close()
return opt, yaml.NewDecoder(f).Decode(&opt.Proxy)
}