feat: 新增简易版ioc

This commit is contained in:
meilin.huang
2024-01-21 22:52:20 +08:00
parent f4a64b96a9
commit f27d3d200f
106 changed files with 815 additions and 707 deletions

View File

@@ -32,11 +32,11 @@ import (
)
type Db struct {
InstanceApp application.Instance
DbApp application.Db
DbSqlExecApp application.DbSqlExec
MsgApp msgapp.Msg
TagApp tagapp.TagTree
InstanceApp application.Instance `inject:"DbInstanceApp"`
DbApp application.Db `inject:""`
DbSqlExecApp application.DbSqlExec `inject:""`
MsgApp msgapp.Msg `inject:""`
TagApp tagapp.TagTree `inject:"TagTreeApp"`
}
// @router /api/dbs [get]

View File

@@ -19,7 +19,7 @@ import (
)
type DataSyncTask struct {
DataSyncTaskApp application.DataSyncTask
DataSyncTaskApp application.DataSyncTask `inject:"DbDataSyncTaskApp"`
}
func (d *DataSyncTask) Tasks(rc *req.Ctx) {

View File

@@ -10,7 +10,7 @@ import (
)
type DbSql struct {
DbSqlApp application.DbSql
DbSqlApp application.DbSql `inject:""`
}
// @router /api/db/:dbId/sql [post]

View File

@@ -9,7 +9,7 @@ import (
)
type DbSqlExec struct {
DbSqlExecApp application.DbSqlExec
DbSqlExecApp application.DbSqlExec `inject:""`
}
func (d *DbSqlExec) DbSqlExecs(rc *req.Ctx) {

View File

@@ -16,8 +16,8 @@ import (
)
type Instance struct {
InstanceApp application.Instance
DbApp application.Db
InstanceApp application.Instance `inject:"DbInstanceApp"`
DbApp application.Db `inject:""`
}
// Instances 获取数据库实例信息

View File

@@ -4,21 +4,26 @@ import (
"fmt"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/internal/db/infrastructure/persistence"
tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/ioc"
"sync"
)
var (
instanceApp Instance
dbApp Db
dbSqlExecApp DbSqlExec
dbSqlApp DbSql
dbBackupApp *DbBackupApp
dbRestoreApp *DbRestoreApp
dbBinlogApp *DbBinlogApp
dataSyncApp DataSyncTask
)
func init() {
persistence.Init()
ioc.Register(new(instanceAppImpl), ioc.WithComponentName("DbInstanceApp"))
ioc.Register(new(dbAppImpl), ioc.WithComponentName("DbApp"))
ioc.Register(new(dbSqlExecAppImpl), ioc.WithComponentName("DbSqlExecApp"))
ioc.Register(new(dbSqlAppImpl), ioc.WithComponentName("DbSqlApp"))
ioc.Register(new(dataSyncAppImpl), ioc.WithComponentName("DbDataSyncTaskApp"))
}
func Init() {
sync.OnceFunc(func() {
repositories := &repository.Repositories{
@@ -31,13 +36,7 @@ func Init() {
BinlogHistory: persistence.NewDbBinlogHistoryRepo(),
}
var err error
instanceRepo := persistence.GetInstanceRepo()
instanceApp = newInstanceApp(instanceRepo)
dbApp = newDbApp(persistence.GetDbRepo(), persistence.GetDbSqlRepo(), instanceApp, tagapp.GetTagTreeApp())
dbSqlExecApp = newDbSqlExecApp(persistence.GetDbSqlExecRepo())
dbSqlApp = newDbSqlApp(persistence.GetDbSqlRepo())
dataSyncApp = newDataSyncApp(persistence.GetDataSyncTaskRepo(), persistence.GetDataSyncLogRepo())
dbApp := GetDbApp()
scheduler, err := newDbScheduler(repositories)
if err != nil {
panic(fmt.Sprintf("初始化 dbScheduler 失败: %v", err))
@@ -55,24 +54,24 @@ func Init() {
panic(fmt.Sprintf("初始化 dbBinlogApp 失败: %v", err))
}
dataSyncApp.InitCronJob()
GetDataSyncTaskApp().InitCronJob()
})()
}
func GetInstanceApp() Instance {
return instanceApp
return ioc.Get[Instance]("DbInstance")
}
func GetDbApp() Db {
return dbApp
return ioc.Get[Db]("DbApp")
}
func GetDbSqlApp() DbSql {
return dbSqlApp
return ioc.Get[DbSql]("DbSqlApp")
}
func GetDbSqlExecApp() DbSqlExec {
return dbSqlExecApp
return ioc.Get[DbSqlExec]("DbSqlExecApp")
}
func GetDbBackupApp() *DbBackupApp {
@@ -88,5 +87,5 @@ func GetDbBinlogApp() *DbBinlogApp {
}
func GetDataSyncTaskApp() DataSyncTask {
return dataSyncApp
return ioc.Get[DataSyncTask]("DbDataSyncTaskApp")
}

View File

@@ -40,22 +40,17 @@ type Db interface {
GetDbConnByInstanceId(instanceId uint64) (*dbi.DbConn, error)
}
func newDbApp(dbRepo repository.Db, dbSqlRepo repository.DbSql, dbInstanceApp Instance, tagApp tagapp.TagTree) Db {
app := &dbAppImpl{
dbSqlRepo: dbSqlRepo,
dbInstanceApp: dbInstanceApp,
tagApp: tagApp,
}
app.Repo = dbRepo
return app
}
type dbAppImpl struct {
base.AppImpl[*entity.Db, repository.Db]
dbSqlRepo repository.DbSql
dbInstanceApp Instance
tagApp tagapp.TagTree
DbSqlRepo repository.DbSql `inject:""`
DbInstanceApp Instance `inject:""`
TagApp tagapp.TagTree `inject:"TagTreeApp"`
}
// 注入DbRepo
func (d *dbAppImpl) InjectDbRepo(repo repository.Db) {
d.Repo = repo
}
// 分页获取数据库信息列表
@@ -83,7 +78,7 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db, tagIds ...u
return d.Tx(ctx, func(ctx context.Context) error {
return d.Insert(ctx, dbEntity)
}, func(ctx context.Context) error {
return d.tagApp.RelateResource(ctx, resouceCode, consts.TagResourceTypeDb, tagIds)
return d.TagApp.RelateResource(ctx, resouceCode, consts.TagResourceTypeDb, tagIds)
})
}
@@ -107,13 +102,13 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db, tagIds ...u
// 关闭数据库连接
dbm.CloseDb(dbEntity.Id, v)
// 删除该库关联的所有sql记录
d.dbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: dbId, Db: v})
d.DbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: dbId, Db: v})
}
return d.Tx(ctx, func(ctx context.Context) error {
return d.UpdateById(ctx, dbEntity)
}, func(ctx context.Context) error {
return d.tagApp.RelateResource(ctx, old.Code, consts.TagResourceTypeDb, tagIds)
return d.TagApp.RelateResource(ctx, old.Code, consts.TagResourceTypeDb, tagIds)
})
}
@@ -134,10 +129,10 @@ func (d *dbAppImpl) Delete(ctx context.Context, id uint64) error {
},
func(ctx context.Context) error {
// 删除该库下用户保存的所有sql信息
return d.dbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: id})
return d.DbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: id})
}, func(ctx context.Context) error {
var tagIds []uint64
return d.tagApp.RelateResource(ctx, db.Code, consts.TagResourceTypeDb, tagIds)
return d.TagApp.RelateResource(ctx, db.Code, consts.TagResourceTypeDb, tagIds)
})
}
@@ -148,7 +143,7 @@ func (d *dbAppImpl) GetDbConn(dbId uint64, dbName string) (*dbi.DbConn, error) {
return nil, errorx.NewBiz("数据库信息不存在")
}
instance, err := d.dbInstanceApp.GetById(new(entity.DbInstance), db.InstanceId)
instance, err := d.DbInstanceApp.GetById(new(entity.DbInstance), db.InstanceId)
if err != nil {
return nil, errorx.NewBiz("数据库实例不存在")
}
@@ -169,7 +164,7 @@ func (d *dbAppImpl) GetDbConn(dbId uint64, dbName string) (*dbi.DbConn, error) {
if err := instance.PwdDecrypt(); err != nil {
return nil, errorx.NewBiz(err.Error())
}
return toDbInfo(instance, dbId, dbName, d.tagApp.ListTagPathByResource(consts.TagResourceTypeDb, db.Code)...), nil
return toDbInfo(instance, dbId, dbName, d.TagApp.ListTagPathByResource(consts.TagResourceTypeDb, db.Code)...), nil
})
}

View File

@@ -38,17 +38,14 @@ type DataSyncTask interface {
GetTaskLogList(condition *entity.DataSyncLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}
func newDataSyncApp(dataSyncRepo repository.DataSyncTask, dataSyncLogRepo repository.DataSyncLog) DataSyncTask {
app := new(dataSyncAppImpl)
app.Repo = dataSyncRepo
app.dataSyncLogRepo = dataSyncLogRepo
return app
}
type dataSyncAppImpl struct {
base.AppImpl[*entity.DataSyncTask, repository.DataSyncTask]
dataSyncLogRepo repository.DataSyncLog
DbDataSyncLogRepo repository.DataSyncLog `inject:""`
}
func (d *dataSyncAppImpl) InjectDbDataSyncTaskRepo(repo repository.DataSyncTask) {
d.Repo = repo
}
func (app *dataSyncAppImpl) GetPageList(condition *entity.DataSyncTaskQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
@@ -328,7 +325,7 @@ func (app *dataSyncAppImpl) endRunning(taskEntity *entity.DataSyncTask, log *ent
}
func (app *dataSyncAppImpl) saveLog(log *entity.DataSyncLog) {
app.dataSyncLogRepo.Save(context.Background(), log)
app.DbDataSyncLogRepo.Save(context.Background(), log)
}
func (app *dataSyncAppImpl) InitCronJob() {
@@ -374,5 +371,5 @@ func (app *dataSyncAppImpl) InitCronJob() {
}
func (app *dataSyncAppImpl) GetTaskLogList(condition *entity.DataSyncLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return app.dataSyncLogRepo.GetTaskLogList(condition, pageParam, toEntity, orderBy...)
return app.DbDataSyncLogRepo.GetTaskLogList(condition, pageParam, toEntity, orderBy...)
}

View File

@@ -33,7 +33,7 @@ type dbScheduler struct {
func newDbScheduler(repositories *repository.Repositories) (*dbScheduler, error) {
scheduler := &dbScheduler{
dbApp: dbApp,
dbApp: GetDbApp(),
backupRepo: repositories.Backup,
backupHistoryRepo: repositories.BackupHistory,
restoreRepo: repositories.Restore,

View File

@@ -14,8 +14,7 @@ type dbSqlAppImpl struct {
base.AppImpl[*entity.DbSql, repository.DbSql]
}
func newDbSqlApp(dbSqlRepo repository.DbSql) DbSql {
app := new(dbSqlAppImpl)
app.Repo = dbSqlRepo
return app
// 注入DbSqlRepo
func (d *dbSqlAppImpl) InjectDbSqlRepo(repo repository.DbSql) {
d.Repo = repo
}

View File

@@ -56,14 +56,8 @@ type DbSqlExec interface {
GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
}
func newDbSqlExecApp(dbExecSqlRepo repository.DbSqlExec) DbSqlExec {
return &dbSqlExecAppImpl{
dbSqlExecRepo: dbExecSqlRepo,
}
}
type dbSqlExecAppImpl struct {
dbSqlExecRepo repository.DbSqlExec
DbSqlExecRepo repository.DbSqlExec `inject:""`
}
func createSqlExecRecord(ctx context.Context, execSqlReq *DbSqlExecReq) *entity.DbSqlExec {
@@ -144,23 +138,23 @@ func (d *dbSqlExecAppImpl) Exec(ctx context.Context, execSqlReq *DbSqlExecReq) (
// 保存sql执行记录如果是查询类则根据系统配置判断是否保存
func (d *dbSqlExecAppImpl) saveSqlExecLog(isQuery bool, dbSqlExecRecord *entity.DbSqlExec) {
if !isQuery {
d.dbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
d.DbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
return
}
if config.GetDbSaveQuerySql() {
dbSqlExecRecord.Table = "-"
dbSqlExecRecord.OldValue = "-"
dbSqlExecRecord.Type = entity.DbSqlExecTypeQuery
d.dbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
d.DbSqlExecRepo.Insert(context.TODO(), dbSqlExecRecord)
}
}
func (d *dbSqlExecAppImpl) DeleteBy(ctx context.Context, condition *entity.DbSqlExec) {
d.dbSqlExecRepo.DeleteByCond(ctx, condition)
d.DbSqlExecRepo.DeleteByCond(ctx, condition)
}
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return d.dbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
return d.DbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}
func doSelect(ctx context.Context, selectStmt *sqlparser.Select, execSqlReq *DbSqlExecReq) (*DbSqlExecRes, error) {

View File

@@ -30,16 +30,15 @@ type Instance interface {
GetDatabases(entity *entity.DbInstance) ([]string, error)
}
func newInstanceApp(instanceRepo repository.Instance) Instance {
app := new(instanceAppImpl)
app.Repo = instanceRepo
return app
}
type instanceAppImpl struct {
base.AppImpl[*entity.DbInstance, repository.Instance]
}
// 注入DbInstanceRepo
func (i *instanceAppImpl) InjectDbInstanceRepo(repo repository.Instance) {
i.Repo = repo
}
// GetPageList 分页获取数据库实例
func (app *instanceAppImpl) GetPageList(condition *entity.InstanceQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return app.GetRepo().GetInstanceList(condition, pageParam, toEntity, orderBy...)

View File

@@ -1,46 +1,49 @@
package persistence
import "mayfly-go/internal/db/domain/repository"
var (
instanceRepo repository.Instance = newInstanceRepo()
dbRepo repository.Db = newDbRepo()
dbSqlRepo repository.DbSql = newDbSqlRepo()
dbSqlExecRepo repository.DbSqlExec = newDbSqlExecRepo()
dbBackupHistoryRepo = NewDbBackupHistoryRepo()
dbRestoreHistoryRepo = NewDbRestoreHistoryRepo()
dbDataSyncTaskRepo repository.DataSyncTask = newDataSyncTaskRepo()
dbDataSyncLogRepo repository.DataSyncLog = newDataSyncLogRepo()
import (
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/ioc"
)
func Init() {
ioc.Register(newInstanceRepo(), ioc.WithComponentName("DbInstanceRepo"))
ioc.Register(newDbRepo(), ioc.WithComponentName("DbRepo"))
ioc.Register(newDbSqlRepo(), ioc.WithComponentName("DbSqlRepo"))
ioc.Register(newDbSqlExecRepo(), ioc.WithComponentName("DbSqlExecRepo"))
ioc.Register(NewDbBackupHistoryRepo(), ioc.WithComponentName("DbBackupHistoryRepo"))
ioc.Register(NewDbRestoreHistoryRepo(), ioc.WithComponentName("DbRestoreHistoryRepo"))
ioc.Register(newDataSyncTaskRepo(), ioc.WithComponentName("DbDataSyncTaskRepo"))
ioc.Register(newDataSyncLogRepo(), ioc.WithComponentName("DbDataSyncLogRepo"))
}
func GetInstanceRepo() repository.Instance {
return instanceRepo
return ioc.Get[repository.Instance]("DbInstanceRepo")
}
func GetDbRepo() repository.Db {
return dbRepo
return ioc.Get[repository.Db]("DbRepo")
}
func GetDbSqlRepo() repository.DbSql {
return dbSqlRepo
return ioc.Get[repository.DbSql]("DbSqlRepo")
}
func GetDbSqlExecRepo() repository.DbSqlExec {
return dbSqlExecRepo
return ioc.Get[repository.DbSqlExec]("DbSqlExecRepo")
}
func GetDbBackupHistoryRepo() repository.DbBackupHistory {
return dbBackupHistoryRepo
return ioc.Get[repository.DbBackupHistory]("DbBackupHistoryRepo")
}
func GetDbRestoreHistoryRepo() repository.DbRestoreHistory {
return dbRestoreHistoryRepo
return ioc.Get[repository.DbRestoreHistory]("DbRestoreHistoryRepo")
}
func GetDataSyncLogRepo() repository.DataSyncLog {
return dbDataSyncLogRepo
return ioc.Get[repository.DataSyncLog]("DataSyncLogRepo")
}
func GetDataSyncTaskRepo() repository.DataSyncTask {
return dbDataSyncTaskRepo
return ioc.Get[repository.DataSyncTask]("DataSyncTaskRepo")
}

View File

@@ -2,9 +2,8 @@ package router
import (
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
msgapp "mayfly-go/internal/msg/application"
tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ioc"
"mayfly-go/pkg/req"
"github.com/gin-gonic/gin"
@@ -13,13 +12,8 @@ import (
func InitDbRouter(router *gin.RouterGroup) {
db := router.Group("dbs")
d := &api.Db{
InstanceApp: application.GetInstanceApp(),
DbApp: application.GetDbApp(),
DbSqlExecApp: application.GetDbSqlExecApp(),
MsgApp: msgapp.GetMsgApp(),
TagApp: tagapp.GetTagTreeApp(),
}
d := new(api.Db)
biz.ErrIsNil(ioc.Inject(d))
reqs := [...]*req.Conf{
// 获取数据库列表

View File

@@ -2,7 +2,8 @@ package router
import (
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ioc"
"mayfly-go/pkg/req"
"github.com/gin-gonic/gin"
@@ -11,9 +12,8 @@ import (
func InitDbDataSyncRouter(router *gin.RouterGroup) {
instances := router.Group("/datasync/tasks")
d := &api.DataSyncTask{
DataSyncTaskApp: application.GetDataSyncTaskApp(),
}
d := new(api.DataSyncTask)
biz.ErrIsNil(ioc.Inject(d))
reqs := [...]*req.Conf{
// 获取任务列表 /datasync

View File

@@ -2,7 +2,8 @@ package router
import (
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ioc"
"mayfly-go/pkg/req"
"github.com/gin-gonic/gin"
@@ -11,9 +12,8 @@ import (
func InitDbSqlRouter(router *gin.RouterGroup) {
db := router.Group("dbs")
dbSql := &api.DbSql{
DbSqlApp: application.GetDbSqlApp(),
}
dbSql := new(api.DbSql)
biz.ErrIsNil(ioc.Inject(dbSql))
reqs := [...]*req.Conf{

View File

@@ -2,7 +2,8 @@ package router
import (
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ioc"
"mayfly-go/pkg/req"
"github.com/gin-gonic/gin"
@@ -11,9 +12,8 @@ import (
func InitDbSqlExecRouter(router *gin.RouterGroup) {
db := router.Group("/dbs/:dbId/sql-execs")
d := &api.DbSqlExec{
DbSqlExecApp: application.GetDbSqlExecApp(),
}
d := new(api.DbSqlExec)
biz.ErrIsNil(ioc.Inject(d))
// 获取所有数据库sql执行记录列表
req.NewGet("", d.DbSqlExecs).Group(db)

View File

@@ -2,7 +2,8 @@ package router
import (
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ioc"
"mayfly-go/pkg/req"
"github.com/gin-gonic/gin"
@@ -11,10 +12,8 @@ import (
func InitInstanceRouter(router *gin.RouterGroup) {
instances := router.Group("/instances")
d := &api.Instance{
InstanceApp: application.GetInstanceApp(),
DbApp: application.GetDbApp(),
}
d := new(api.Instance)
biz.ErrIsNil(ioc.Inject(d))
reqs := [...]*req.Conf{
// 获取数据库列表