支持从目录批量读取配置文件

This commit is contained in:
白茶清欢 2023-07-31 18:00:43 +08:00
parent 0f28427811
commit c3638cb36b
5 changed files with 114 additions and 7 deletions

View File

@ -27,6 +27,7 @@ type MysqlConfig struct {
//
// Date : 10:54 2023/7/31
type LoggerConfig struct {
Ignore bool `json:"ignore" gorm:"ignore" ini:"ignore" xml:"ignore" yaml:"ignore"` // 会略日志记录
LogPath string `json:"log_path" gorm:"log_path" ini:"log_path" xml:"log_path" yaml:"log_path"` // 日志路径, 不包含文件名, 相对路径, 则日志存储与项目路径下
Level int `json:"level" gorm:"level" ini:"level" xml:"level" yaml:"level"` // 日志等级
Console bool `json:"console" gorm:"console" ini:"console" xml:"console" yaml:"console"` // 控制台输出

View File

@ -18,6 +18,7 @@ type Database struct {
Description string `json:"description" gorm:"description" ini:"description" xml:"description" yaml:"description"` // 数据库描述
Logger *LoggerConfig `json:"logger" gorm:"logger" ini:"logger" xml:"logger" yaml:"logger"` // 日志配置
Config map[string]interface{} `json:"config" gorm:"config" ini:"config" xml:"config" yaml:"config"` // 数据库的配置
TableList []*Table `json:"table_list" gorm:"table_list" ini:"table_list" xml:"table_list" yaml:"table_list"` // 数据表列表
}
// Table 数据表结构
@ -26,12 +27,12 @@ type Database struct {
//
// Date : 16:43 2023/7/30
type Table struct {
Name string `json:"name"` // 数据表名称
Split bool `json:"split"` // 是否分表
SplitCount int64 `json:"split_count"` // 分表数量
SplitField string `json:"split_field"` // 分表字段
PrimaryField string `json:"primary_field"` // 主键字段
FieldList []*TableField `json:"field_list"` // 字段列表
Name string `json:"name" gorm:"name" ini:"name" xml:"name" yaml:"name"` // 数据表名称
Split bool `json:"split" gorm:"split" ini:"split" xml:"split" yaml:"split"` // 是否分表
SplitCount int64 `json:"split_count" gorm:"split_count" ini:"split_count" xml:"split_count" yaml:"split_count"` // 分表数量
SplitField string `json:"split_field" gorm:"split_field" ini:"split_field" xml:"split_field" yaml:"split_field"` // 分表字段
PrimaryField string `json:"primary_field" gorm:"primary_field" ini:"primary_field" xml:"primary_field" yaml:"primary_field"` // 主键字段
FieldList []*TableField `json:"field_list" gorm:"field_list" ini:"field_list" xml:"field_list" yaml:"field_list"` // 字段列表
}
// TableField 数据表字段配置

View File

@ -0,0 +1,91 @@
// Package api2sql ...
//
// Description : api2sql ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-07-31 14:21
package api2sql
import (
"errors"
"git.zhangdeman.cn/zhangdeman/database/api2sql/define"
"git.zhangdeman.cn/zhangdeman/easymap"
"git.zhangdeman.cn/zhangdeman/util"
utilDefine "git.zhangdeman.cn/zhangdeman/util/define"
"git.zhangdeman.cn/zhangdeman/wrapper"
)
var (
// InstanceManager 实例管理
InstanceManager *instanceManager
)
func init() {
InstanceManager = &instanceManager{
instanceTable: easymap.NewNormal(true),
supportConfigFileFormat: []string{
"json",
"yml",
"yaml",
"ini",
"toml",
},
}
}
// instanceManager 实例管理
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:21 2023/7/31
type instanceManager struct {
instanceTable easymap.EasyMap // 实例存储
supportConfigFileFormat []string // 支持的配置文件格式
}
// RegisterWithConfig 基于配置进行注册
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:26 2023/7/31
func (im *instanceManager) RegisterWithConfig(instanceConfig *define.Database) error {
return nil
}
// RegisterWithConfigFile 基于配置文件进行注册
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:32 2023/7/31
func (im *instanceManager) RegisterWithConfigFile(configFileList []string) error {
return nil
}
// RegisterWithConfigDir 配置的目录
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:33 2023/7/31
func (im *instanceManager) RegisterWithConfigDir(configDir string) error {
var (
fileList []*utilDefine.FileInfo
err error
)
if fileList, err = util.File.ReadDirFileList(configDir, true, false); nil != err {
return errors.New("config file list read fail : " + err.Error())
}
cfgFileList := make([]string, 0)
for _, item := range fileList {
if item.IsDir {
continue
}
if wrapper.Array(im.supportConfigFileFormat).In(item) >= 0 {
// 支持此格式
cfgFileList = append(cfgFileList, item.AbsolutePath)
}
}
return im.RegisterWithConfigFile(cfgFileList)
}

5
go.mod
View File

@ -5,7 +5,7 @@ go 1.20
require (
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20230530101401-4326fad68c6f
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20230629110904-e1fd92d004ca
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230730112409-1d8e8d546b8f
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230731074659-d9fac952a807
github.com/gin-gonic/gin v1.9.1
github.com/pkg/errors v0.9.1
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
@ -16,6 +16,9 @@ require (
)
require (
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 // indirect
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230731092612-eb97a0b352af // indirect
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/bytedance/sonic v1.10.0-rc3 // indirect

11
go.sum
View File

@ -1,9 +1,19 @@
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI=
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10 h1:+Lg4vXFEiWVKjhUJdXuoP0AgjGT49oqJ3301STnZErk=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20230307094841-e437ba87af10/go.mod h1:+Lc0zYF8sylRi75A7NGmObrLxugwAZa8WVpWh2eh5X0=
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20230530101401-4326fad68c6f h1:IILGYXUlNGtr61Q6VaHneyjT0IFcHekSeFsQEVjUCdc=
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20230530101401-4326fad68c6f/go.mod h1:RYrfkfiMNRR+pApm+xh8B/skCXgEVjJS/1AyXYAaYno=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20230629110904-e1fd92d004ca h1:H/rhiFPv+euwpob7Hg92VLQoIVOUQz9U3ZkY8s7V/xk=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20230629110904-e1fd92d004ca/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230730112409-1d8e8d546b8f h1:6ckH+oNMxiO3V4EMRGmTQcsTbdnBYdYv1mGoYmj3ibQ=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230730112409-1d8e8d546b8f/go.mod h1:94FAWQr3vB1ogydIZ+0aNhd+ai+CKgDVGK2q+yGmOzM=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230731073902-223d5ab37410 h1:5IDfSY1yEki84fKOyTumdjg4AvPuVQ3ESAVekhlQKFQ=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230731073902-223d5ab37410/go.mod h1:94FAWQr3vB1ogydIZ+0aNhd+ai+CKgDVGK2q+yGmOzM=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230731074659-d9fac952a807 h1:BiYAlw9iZN5fhkrEoR5EpgxImBmV56rljO2o5v4G6vo=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230731074659-d9fac952a807/go.mod h1:94FAWQr3vB1ogydIZ+0aNhd+ai+CKgDVGK2q+yGmOzM=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230731092612-eb97a0b352af h1:Bm8YXIxodaSTBYwG/vRXswJ/zb9cKlMdxq5ElMjs9w4=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230731092612-eb97a0b352af/go.mod h1:Zl2pqhTvNpr5SrKPfZ5JDt+FkyimxnYxLfpvTgaLG+c=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
@ -22,6 +32,7 @@ github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLI
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=