完成配量读取配置文件

This commit is contained in:
白茶清欢 2022-06-11 18:00:47 +08:00
parent e610e02d45
commit 879c9df7cc
3 changed files with 19 additions and 26 deletions

View File

@ -77,20 +77,27 @@ func (c *client) getMysqlCfgFileList(cfgDir string) ([]cfgFile, error) {
continue
}
fileType := strings.ToLower(fileArr[len(fileArr)-1])
var (
err error
)
cfgInfo := cfgFile{
Path: filepathNames[i],
Type: FileTypeYaml,
Config: Mysql{},
}
switch fileType {
case FileTypeYaml:
fallthrough
case FileTypeYml:
cfgFileList = append(cfgFileList, cfgFile{
Path: filepathNames[i],
Type: FileTypeYaml,
})
util.File.ReadYmlConfig()
cfgInfo.Type = FileTypeYaml
if err = util.File.ReadYmlContent(filepathNames[i], &cfgInfo); nil != err {
return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgInfo.Path, err.Error())
}
case FileTypeJson:
cfgFileList = append(cfgFileList, cfgFile{
Path: filepathNames[i],
Type: FileTypeJson,
})
cfgInfo.Type = FileTypeJson
if err = util.File.ReadJSONContent(filepathNames[i], &cfgInfo); nil != err {
return nil, fmt.Errorf("%s 配置文件解析失败, 原因 : %s", cfgInfo.Path, err.Error())
}
default:
// 不是JSON , 也不是YML, 跳过
continue
@ -104,7 +111,7 @@ func (c *client) getMysqlCfgFileList(cfgDir string) ([]cfgFile, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:32 2022/6/5
func (c *client) GetDBClient(ctx *gin.Context, dbFlag string) (*DBClient, error) {
func (c *client) GetDBClient(dbFlag string) (*DBClient, error) {
c.lock.RLock()
defer c.lock.RUnlock()
var (
@ -127,7 +134,7 @@ func (c *client) GetMasterClient(ctx *gin.Context, dbFlag string) (*gorm.DB, err
err error
dbClient *DBClient
)
if dbClient, err = c.GetDBClient(ctx, dbFlag); nil != err {
if dbClient, err = c.GetDBClient(dbFlag); nil != err {
return nil, err
}
@ -146,7 +153,7 @@ func (c *client) GetSlaveClient(ctx *gin.Context, dbFlag string) (*gorm.DB, erro
err error
dbClient *DBClient
)
if dbClient, err = c.GetDBClient(ctx, dbFlag); nil != err {
if dbClient, err = c.GetDBClient(dbFlag); nil != err {
return nil, err
}
@ -190,15 +197,6 @@ func (c *client) getGormClient() (*gorm.DB, error) {
return nil, nil
}
// BatchNewDBClient 批量设置数据库实例, 读取指定目录下的yaml文件, 文件名作为标识, 内容是数据库具体配置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:43 2022/6/5
func BatchNewDBClient(dbConfigDir string) {
}
// NewDBClient ...
//
// Author : go_developer@163.com<白茶清欢>

1
go.mod
View File

@ -14,7 +14,6 @@ require (
)
require (
git.zhangdeman.cn/zhangdeman/command v0.0.0-20220522141301-bced9bb50647 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect

4
go.sum
View File

@ -1,9 +1,5 @@
git.zhangdeman.cn/zhangdeman/command v0.0.0-20220522141301-bced9bb50647 h1:TQhXffwAtgJIyu7FuPdmgX6F7OoCGIujW8aN3EFODpw=
git.zhangdeman.cn/zhangdeman/command v0.0.0-20220522141301-bced9bb50647/go.mod h1:oCRPD8pwzLidtrqKQXcHMMJocYs5WDXnh20SCu34cBo=
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3 h1:T41tE9F2Gy8eKVSKtTPhf7RaRT12qHEXTfx4IJuoIS4=
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20220514052229-cf395d3dc4c3/go.mod h1:0A5BV9pE31nuFE60TLbP7BIhhV/fcWoi+fHrcV2clJw=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220514082633-1be4d9eab11f h1:1amgaCqOPn7gvcUEEX614cO8lkIz+G8W/YtGXLIdW1w=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220514082633-1be4d9eab11f/go.mod h1:YI/XeTmrr9+8dxa4ThPkmNcEE8WHG5pZkKujpSWwIxM=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220609072516-022a755fdf2f h1:yAxxukVUdSM5wn264el+QiAEB0OBN/5H7Xw9Z6rLzUY=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220609072516-022a755fdf2f/go.mod h1:YI/XeTmrr9+8dxa4ThPkmNcEE8WHG5pZkKujpSWwIxM=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=