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

41 lines
882 B
Go
Raw Normal View History

2024-05-14 15:27:40 +08:00
package options
import (
2024-09-13 22:47:19 +08:00
"fmt"
"os"
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-09-13 22:47:19 +08:00
Proxy proxy.Option `yaml:"proxy"`
ProviderId v2.ProviderId `yaml:"provider_id"`
2024-05-14 15:27:40 +08:00
}
2024-09-13 22:47:19 +08:00
func LoadConfigs() (*Config, error) {
var opt Config
// 从环境变量中读取ProviderId
opt.ProviderId = v2.ProviderId(os.Getenv("PROVIDER_ID"))
if opt.ProviderId == "" {
return nil, fmt.Errorf("环境变量未配置供应商id")
}
// 加载代理配置
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-09-13 22:47:19 +08:00
return &opt, yaml.NewDecoder(f).Decode(&opt.Proxy)
2024-05-14 15:27:40 +08:00
}