支持单点与批量载入配置文件
This commit is contained in:
parent
efdbb35412
commit
5497849900
31
client.go
31
client.go
@ -115,7 +115,7 @@ type OwnClient struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:31 2022/6/15
|
||||
func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) {
|
||||
func (c *OwnClient) loadConfig(cfgPath string) error {
|
||||
var (
|
||||
err error
|
||||
cfg FullConfig
|
||||
@ -123,12 +123,12 @@ func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) {
|
||||
|
||||
filePathArr := strings.Split(cfgPath, string(filepath.Separator))
|
||||
if len(filePathArr) == 0 {
|
||||
return nil, CfgFilePathError()
|
||||
return CfgFilePathError()
|
||||
}
|
||||
fileName := strings.ToLower(filePathArr[len(filePathArr)-1])
|
||||
fileArr := strings.Split(fileName, ".")
|
||||
if len(filePathArr) < 2 {
|
||||
return nil, CfgFileFormatErr("未知")
|
||||
return CfgFileFormatErr("未知")
|
||||
}
|
||||
flag := strings.Trim(
|
||||
strings.Trim(
|
||||
@ -137,22 +137,39 @@ func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) {
|
||||
switch strings.ToLower(fileArr[len(fileArr)-1]) {
|
||||
case "json":
|
||||
if err = util.File.ReadJSONContent(cfgPath, &cfg); nil != err {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
flag = strings.Join(fileArr[0:len(fileArr)-1], ".")
|
||||
case "yml":
|
||||
fallthrough
|
||||
case "yaml":
|
||||
if err = util.File.ReadYmlContent(cfgPath, &cfg); nil != err {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
flag = strings.Join(fileArr[0:len(fileArr)-1], ".")
|
||||
default:
|
||||
return nil, CfgFileFormatErr(fileArr[len(fileArr)-1])
|
||||
return CfgFileFormatErr(fileArr[len(fileArr)-1])
|
||||
}
|
||||
|
||||
c.lock.Lock()
|
||||
c.confTable[flag] = &cfg
|
||||
return &cfg, nil
|
||||
c.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// batchLoadConfig 批量载入配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:44 2022/6/15
|
||||
func (c *OwnClient) batchLoadConfig(cfgDir string) error {
|
||||
filepathNames, _ := filepath.Glob(filepath.Join(cfgDir, "*"))
|
||||
for i := range filepathNames {
|
||||
if err := c.loadConfig(filepathNames[i]); nil != err {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// init 初始化redis连接
|
||||
|
Loading…
Reference in New Issue
Block a user