refactor: 新增base.Repo与base.App,重构repo与app层代码

This commit is contained in:
meilin.huang
2023-10-26 17:15:49 +08:00
parent 10f6b03fb5
commit a1303b52eb
115 changed files with 1867 additions and 1696 deletions

View File

@@ -18,14 +18,17 @@ type AuthCert struct {
func (ac *AuthCert) BaseAuthCerts(rc *req.Ctx) {
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.AuthCertQuery))
rc.ResData = ac.AuthCertApp.GetPageList(queryCond, page, new([]vo.AuthCertBaseVO))
res, err := ac.AuthCertApp.GetPageList(queryCond, page, new([]vo.AuthCertBaseVO))
biz.ErrIsNil(err)
rc.ResData = res
}
func (ac *AuthCert) AuthCerts(rc *req.Ctx) {
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.AuthCertQuery))
res := new([]*entity.AuthCert)
pageRes := ac.AuthCertApp.GetPageList(queryCond, page, res)
pageRes, err := ac.AuthCertApp.GetPageList(queryCond, page, res)
biz.ErrIsNil(err)
for _, r := range *res {
r.PwdDecrypt()
}
@@ -42,7 +45,7 @@ func (c *AuthCert) SaveAuthCert(rc *req.Ctx) {
rc.ReqParam = acForm
ac.SetBaseInfo(rc.LoginAccount)
c.AuthCertApp.Save(ac)
biz.ErrIsNil(c.AuthCertApp.Save(ac))
}
func (c *AuthCert) Delete(rc *req.Ctx) {

View File

@@ -11,9 +11,11 @@ import (
tagapp "mayfly-go/internal/tag/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/config"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/anyx"
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/stringx"
"mayfly-go/pkg/ws"
@@ -44,7 +46,8 @@ func (m *Machine) Machines(rc *req.Ctx) {
}
condition.TagIds = tagIds
res := m.MachineApp.GetMachineList(condition, pageParam, new([]*vo.MachineVO))
res, err := m.MachineApp.GetMachineList(condition, pageParam, new([]*vo.MachineVO))
biz.ErrIsNil(err)
if res.Total == 0 {
rc.ResData = res
return
@@ -61,8 +64,9 @@ func (m *Machine) MachineTags(rc *req.Ctx) {
}
func (m *Machine) MachineStats(rc *req.Ctx) {
stats := m.MachineApp.GetCli(GetMachineId(rc.GinCtx)).GetAllStats()
rc.ResData = stats
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
rc.ResData = cli.GetAllStats()
}
// 保存机器信息
@@ -74,12 +78,13 @@ func (m *Machine) SaveMachine(rc *req.Ctx) {
rc.ReqParam = machineForm
me.SetBaseInfo(rc.LoginAccount)
m.MachineApp.Save(me)
biz.ErrIsNil(m.MachineApp.Save(me))
}
func (m *Machine) TestConn(rc *req.Ctx) {
me := ginx.BindJsonAndCopyTo(rc.GinCtx, new(form.MachineForm), new(entity.Machine))
m.MachineApp.TestConn(me)
// 测试连接
biz.ErrIsNilAppendErr(m.MachineApp.TestConn(me), "该机器无法连接: %s")
}
func (m *Machine) ChangeStatus(rc *req.Ctx) {
@@ -87,7 +92,7 @@ func (m *Machine) ChangeStatus(rc *req.Ctx) {
id := uint64(ginx.PathParamInt(g, "machineId"))
status := int8(ginx.PathParamInt(g, "status"))
rc.ReqParam = collx.Kvs("id", id, "status", status)
m.MachineApp.ChangeStatus(id, status)
biz.ErrIsNil(m.MachineApp.ChangeStatus(id, status))
}
func (m *Machine) DeleteMachine(rc *req.Ctx) {
@@ -126,7 +131,8 @@ func (m *Machine) GetProcess(rc *req.Ctx) {
count := ginx.QueryInt(g, "count", 10)
cmd += "| head -n " + fmt.Sprintf("%d", count)
cli := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
res, err := cli.Run(cmd)
@@ -139,7 +145,8 @@ func (m *Machine) KillProcess(rc *req.Ctx) {
pid := rc.GinCtx.Query("pid")
biz.NotEmpty(pid, "进程id不能为空")
cli := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
res, err := cli.Run("sudo kill -9 " + pid)
@@ -151,7 +158,7 @@ func (m *Machine) WsSSH(g *gin.Context) {
defer func() {
if wsConn != nil {
if err := recover(); err != nil {
wsConn.WriteMessage(websocket.TextMessage, []byte(err.(error).Error()))
wsConn.WriteMessage(websocket.TextMessage, []byte(anyx.ToString(err)))
}
wsConn.Close()
}
@@ -161,10 +168,11 @@ func (m *Machine) WsSSH(g *gin.Context) {
// 权限校验
rc := req.NewCtxWithGin(g).WithRequiredPermission(req.NewPermission("machine:terminal"))
if err = req.PermissionHandler(rc); err != nil {
panic(biz.NewBizErr("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
panic(errorx.NewBiz("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
}
cli := m.MachineApp.GetCli(GetMachineId(g))
cli, err := m.MachineApp.GetCli(GetMachineId(g))
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
cols := ginx.QueryInt(g, "cols", 80)

View File

@@ -22,12 +22,13 @@ func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
cond, pageParam := ginx.BindQueryAndPage(rc.GinCtx, new(entity.MachineCronJob))
vos := new([]*vo.MachineCronJobVO)
pr := m.MachineCronJobApp.GetPageList(cond, pageParam, vos)
pageRes, err := m.MachineCronJobApp.GetPageList(cond, pageParam, vos)
biz.ErrIsNil(err)
for _, mcj := range *vos {
mcj.Running = scheduler.ExistKey(mcj.Key)
}
rc.ResData = pr
rc.ResData = pageRes
}
func (m *MachineCronJob) Save(rc *req.Ctx) {
@@ -35,7 +36,8 @@ func (m *MachineCronJob) Save(rc *req.Ctx) {
mcj := ginx.BindJsonAndCopyTo[*entity.MachineCronJob](rc.GinCtx, jobForm, new(entity.MachineCronJob))
rc.ReqParam = jobForm
mcj.SetBaseInfo(rc.LoginAccount)
cronJobId := m.MachineCronJobApp.Save(mcj)
cronJobId, err := m.MachineCronJobApp.Save(mcj)
biz.ErrIsNil(err)
// 关联机器
m.MachineCronJobApp.CronJobRelateMachines(cronJobId, jobForm.MachineIds, rc.LoginAccount)
@@ -63,5 +65,7 @@ func (m *MachineCronJob) GetRelateCronJobIds(rc *req.Ctx) {
func (m *MachineCronJob) CronJobExecs(rc *req.Ctx) {
cond, pageParam := ginx.BindQueryAndPage[*entity.MachineCronJobExec](rc.GinCtx, new(entity.MachineCronJobExec))
rc.ResData = m.MachineCronJobApp.GetExecPageList(cond, pageParam, new([]entity.MachineCronJobExec))
res, err := m.MachineCronJobApp.GetExecPageList(cond, pageParam, new([]entity.MachineCronJobExec))
biz.ErrIsNil(err)
rc.ResData = res
}

View File

@@ -12,6 +12,7 @@ import (
msgapp "mayfly-go/internal/msg/application"
msgdto "mayfly-go/internal/msg/application/dto"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/req"
@@ -43,7 +44,9 @@ const (
func (m *MachineFile) MachineFiles(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.MachineFile{MachineId: GetMachineId(g)}
rc.ResData = m.MachineFileApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineFileVO))
res, err := m.MachineFileApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineFileVO))
biz.ErrIsNil(err)
rc.ResData = res
}
func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
@@ -52,11 +55,11 @@ func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
entity.SetBaseInfo(rc.LoginAccount)
rc.ReqParam = fileForm
m.MachineFileApp.Save(entity)
biz.ErrIsNil(m.MachineFileApp.Save(entity))
}
func (m *MachineFile) DeleteFile(rc *req.Ctx) {
m.MachineFileApp.Delete(GetMachineFileId(rc.GinCtx))
biz.ErrIsNil(m.MachineFileApp.Delete(GetMachineFileId(rc.GinCtx)))
}
/*** sftp相关操作 */
@@ -121,7 +124,9 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
if !strings.HasSuffix(readPath, "/") {
readPath = readPath + "/"
}
fis := m.MachineFileApp.ReadDir(fid, readPath)
fis, err := m.MachineFileApp.ReadDir(fid, readPath)
biz.ErrIsNilAppendErr(err, "读取目录失败: %s")
fisVO := make([]vo.MachineFileInfo, 0)
for _, fi := range fis {
fisVO = append(fisVO, vo.MachineFileInfo{
@@ -143,7 +148,9 @@ func (m *MachineFile) GetDirSize(rc *req.Ctx) {
fid := GetMachineFileId(g)
readPath := g.Query("path")
rc.ResData = m.MachineFileApp.GetDirSize(fid, readPath)
size, err := m.MachineFileApp.GetDirSize(fid, readPath)
biz.ErrIsNil(err)
rc.ResData = size
}
func (m *MachineFile) GetFileStat(rc *req.Ctx) {
@@ -151,7 +158,9 @@ func (m *MachineFile) GetFileStat(rc *req.Ctx) {
fid := GetMachineFileId(g)
readPath := g.Query("path")
rc.ResData = m.MachineFileApp.FileStat(fid, readPath)
res, err := m.MachineFileApp.FileStat(fid, readPath)
biz.ErrIsNil(err, res)
rc.ResData = res
}
func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
@@ -220,9 +229,12 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
paths := mf.Value["paths"]
folderName := filepath.Dir(paths[0])
mcli := m.MachineFileApp.GetMachineCli(fid, basePath+"/"+folderName)
mcli, err := m.MachineFileApp.GetMachineCli(fid, basePath+"/"+folderName)
biz.ErrIsNil(err)
mi := mcli.GetMachine()
sftpCli := mcli.GetSftpCli()
sftpCli, err := mcli.GetSftpCli()
biz.ErrIsNil(err)
rc.ReqParam = collx.Kvs("machine", mi, "path", fmt.Sprintf("%s/%s", basePath, folderName))
folderFiles := make([]FolderFile, len(paths))
@@ -258,7 +270,7 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
if err := recover(); err != nil {
logx.Errorf("文件上传失败: %s", err)
switch t := err.(type) {
case biz.BizError:
case errorx.BizError:
m.MsgApp.CreateAndSend(la, msgdto.ErrSysMsg("文件上传失败", fmt.Sprintf("执行文件上传失败:\n<-e errCode: %d, errMsg: %s", t.Code(), t.Error())))
}
}

View File

@@ -27,7 +27,9 @@ type MachineScript struct {
func (m *MachineScript) MachineScripts(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.MachineScript{MachineId: GetMachineId(g)}
rc.ResData = m.MachineScriptApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineScriptVO))
res, err := m.MachineScriptApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineScriptVO))
biz.ErrIsNil(err)
rc.ResData = res
}
func (m *MachineScript) SaveMachineScript(rc *req.Ctx) {
@@ -37,7 +39,7 @@ func (m *MachineScript) SaveMachineScript(rc *req.Ctx) {
rc.ReqParam = form
machineScript.SetBaseInfo(rc.LoginAccount)
m.MachineScriptApp.Save(machineScript)
biz.ErrIsNil(m.MachineScriptApp.Save(machineScript))
}
func (m *MachineScript) DeleteMachineScript(rc *req.Ctx) {
@@ -57,8 +59,8 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
scriptId := GetMachineScriptId(g)
machineId := GetMachineId(g)
ms := m.MachineScriptApp.GetById(scriptId, "MachineId", "Name", "Script")
biz.NotNil(ms, "该脚本不存在")
ms, err := m.MachineScriptApp.GetById(new(entity.MachineScript), scriptId, "MachineId", "Name", "Script")
biz.ErrIsNil(err, "该脚本不存在")
biz.IsTrue(ms.MachineId == application.Common_Script_Machine_Id || ms.MachineId == machineId, "该脚本不属于该机器")
script := ms.Script
@@ -66,7 +68,8 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
if params := g.Query("params"); params != "" {
script = stringx.TemplateParse(ms.Script, jsonx.ToMap(params))
}
cli := m.MachineApp.GetCli(machineId)
cli, err := m.MachineApp.GetCli(machineId)
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.LoginAccount.Id, cli.GetMachine().TagPath), "%s")
res, err := cli.Run(script)