2024-05-14 15:27:40 +08:00
|
|
|
package options
|
|
|
|
|
|
|
|
import (
|
2024-09-13 22:47:19 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
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-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-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"))
|
|
|
|
|
|
|
|
if opt.WatchInterval == 0 {
|
|
|
|
opt.WatchInterval = 5 * time.Minute
|
|
|
|
}
|
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
|
|
|
}
|