feat 添加默认初始化方法

This commit is contained in:
timerzz 2024-09-02 20:16:06 +08:00
parent 85e6bb8a8e
commit a19977d88f
2 changed files with 42 additions and 0 deletions

View File

@ -1,11 +1,30 @@
package dw_sdk
import (
"fmt"
"os"
)
type Client struct {
articleServiceClient ArticleServiceClient
consignBidClient BidClient
preSaleBidClient BidClient
}
func InitDefaultDWClient() (*Client, error) {
path := os.Getenv(ConfigPathEnvKey)
if path == "" {
path = DefaultConfigPath
}
cfg, err := LoadDWConfig(path)
if err != nil {
return nil, fmt.Errorf("获取得物配置失败:%v", err)
}
cli := NewClient(*cfg)
return cli, nil
}
func NewClient(cfg Config) *Client {
return &Client{
articleServiceClient: NewArticleServiceClient(cfg),

View File

@ -1,5 +1,11 @@
package dw_sdk
import (
"os"
"gopkg.in/yaml.v3"
)
type Config struct {
Public PublicConfig `json:"public" yaml:"public"`
Proxy *ProxyConfig `json:"proxy,omitempty" yaml:"proxy,omitempty"`
@ -17,3 +23,20 @@ type ProxyConfig struct {
User string `json:"user" yaml:"user"`
Pass string `json:"pass" yaml:"pass"`
}
const (
// 默认的数据库配置文件路径
DefaultConfigPath = "/cfg/dw.yaml"
// 数据库配置文件路径环境变量
ConfigPathEnvKey = "DW_CONFIG_PATH"
)
func LoadDWConfig(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var opt Config
return &opt, yaml.NewDecoder(f).Decode(&opt)
}