2024-05-14 15:27:40 +08:00
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
2024-09-13 22:47:19 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-12-02 19:07:38 +08:00
|
|
|
"strconv"
|
2024-11-30 20:26:37 +08:00
|
|
|
"time"
|
2024-09-13 22:47:19 +08:00
|
|
|
|
2024-05-14 15:27:40 +08:00
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
|
2024-09-13 22:47:19 +08:00
|
|
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
2024-12-02 19:07:38 +08:00
|
|
|
"github.com/golang/glog"
|
2024-05-14 15:27:40 +08:00
|
|
|
"gopkg.in/yaml.v3"
|
2024-09-13 22:47:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ProxyConfigEnv = "PROXY_CONFIG_PATH"
|
|
|
|
DefaultProxyConfigPath = "/cfg/proxy.yaml"
|
2024-05-14 15:27:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2024-11-30 20:26:37 +08:00
|
|
|
Proxy proxy.Option `yaml:"proxy"`
|
|
|
|
ProviderId v2.ProviderId `yaml:"provider_id"`
|
|
|
|
WatchInterval time.Duration
|
2024-12-02 19:07:38 +08:00
|
|
|
AtsInterval time.Duration
|
|
|
|
AtsThreshold int // 库存一定时间内减少多少个通知
|
2024-05-14 15:27:40 +08:00
|
|
|
}
|
|
|
|
|
2024-11-30 20:26:37 +08:00
|
|
|
func LoadConfigs() (opt *Config, err error) {
|
|
|
|
opt = &Config{}
|
2024-09-13 22:47:19 +08:00
|
|
|
// 从环境变量中读取ProviderId
|
|
|
|
opt.ProviderId = v2.ProviderId(os.Getenv("PROVIDER_ID"))
|
|
|
|
if opt.ProviderId == "" {
|
|
|
|
return nil, fmt.Errorf("环境变量未配置供应商id")
|
|
|
|
}
|
2024-11-30 20:26:37 +08:00
|
|
|
|
|
|
|
opt.WatchInterval, _ = time.ParseDuration(os.Getenv("WATCH_INTERVAL"))
|
2024-12-02 19:07:38 +08:00
|
|
|
opt.AtsInterval, _ = time.ParseDuration(os.Getenv("ATS_INTERVAL"))
|
2024-11-30 20:26:37 +08:00
|
|
|
if opt.WatchInterval == 0 {
|
|
|
|
opt.WatchInterval = 5 * time.Minute
|
|
|
|
}
|
2024-12-02 19:07:38 +08:00
|
|
|
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)
|
2024-09-13 22:47:19 +08:00
|
|
|
// 加载代理配置
|
|
|
|
cfgPath := os.Getenv(ProxyConfigEnv)
|
2024-05-14 15:27:40 +08:00
|
|
|
if cfgPath == "" {
|
2024-09-13 22:47:19 +08:00
|
|
|
cfgPath = DefaultProxyConfigPath
|
2024-05-14 15:27:40 +08:00
|
|
|
}
|
|
|
|
f, err := os.Open(cfgPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2024-11-30 20:26:37 +08:00
|
|
|
return opt, yaml.NewDecoder(f).Decode(&opt.Proxy)
|
2024-05-14 15:27:40 +08:00
|
|
|
}
|