41 lines
882 B
Go
41 lines
882 B
Go
package options
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"gitea.timerzz.com/kedaya_haitao/common/pkg/proxy"
|
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
|
"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"`
|
|
}
|
|
|
|
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)
|
|
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)
|
|
}
|