refactor: 代码结构调整

This commit is contained in:
meilin.huang
2022-09-09 18:26:08 +08:00
parent fb3f89c594
commit be00b90c1d
120 changed files with 944 additions and 561 deletions

View File

@@ -1,23 +1,30 @@
package api
import (
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
dbapp "mayfly-go/internal/db/application"
dbentity "mayfly-go/internal/db/domain/entity"
machineentity "mayfly-go/internal/machine/domain/entity"
projectentity "mayfly-go/internal/project/domain/entity"
redisentity "mayfly-go/internal/redis/domain/entity"
machineapp "mayfly-go/internal/machine/application"
projectapp "mayfly-go/internal/project/application"
redisapp "mayfly-go/internal/redis/application"
"mayfly-go/pkg/ctx"
)
type Index struct {
ProjectApp application.Project
MachineApp application.Machine
DbApp application.Db
RedisApp application.Redis
ProjectApp projectapp.Project
MachineApp machineapp.Machine
DbApp dbapp.Db
RedisApp redisapp.Redis
}
func (i *Index) Count(rc *ctx.ReqCtx) {
rc.ResData = map[string]interface{}{
"projectNum": i.ProjectApp.Count(new(entity.Project)),
"machineNum": i.MachineApp.Count(new(entity.Machine)),
"dbNum": i.DbApp.Count(new(entity.Db)),
"redisNum": i.RedisApp.Count(new(entity.Redis)),
"projectNum": i.ProjectApp.Count(new(projectentity.Project)),
"machineNum": i.MachineApp.Count(new(machineentity.Machine)),
"dbNum": i.DbApp.Count(new(dbentity.Db)),
"redisNum": i.RedisApp.Count(new(redisentity.Redis)),
}
}

View File

@@ -2,7 +2,10 @@ package router
import (
"mayfly-go/internal/common/api"
devops_app "mayfly-go/internal/devops/application"
dbapp "mayfly-go/internal/db/application"
machineapp "mayfly-go/internal/machine/application"
projectapp "mayfly-go/internal/project/application"
redisapp "mayfly-go/internal/redis/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -11,10 +14,10 @@ import (
func InitIndexRouter(router *gin.RouterGroup) {
index := router.Group("common/index")
i := &api.Index{
ProjectApp: devops_app.ProjectApp,
MachineApp: devops_app.MachineApp,
DbApp: devops_app.DbApp,
RedisApp: devops_app.RedisApp,
ProjectApp: projectapp.GetProjectApp(),
MachineApp: machineapp.GetMachineApp(),
DbApp: dbapp.GetDbApp(),
RedisApp: redisapp.GetRedisApp(),
}
{
// 首页基本信息统计

View File

@@ -3,11 +3,12 @@ package api
import (
"fmt"
"io"
"mayfly-go/internal/devops/api/form"
"mayfly-go/internal/devops/api/vo"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
sysApplication "mayfly-go/internal/sys/application"
"mayfly-go/internal/db/api/form"
"mayfly-go/internal/db/api/vo"
"mayfly-go/internal/db/application"
"mayfly-go/internal/db/domain/entity"
projectapp "mayfly-go/internal/project/application"
sysapp "mayfly-go/internal/sys/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
@@ -25,8 +26,8 @@ import (
type Db struct {
DbApp application.Db
DbSqlExecApp application.DbSqlExec
MsgApp sysApplication.Msg
ProjectApp application.Project
MsgApp sysapp.Msg
ProjectApp projectapp.Project
}
const DEFAULT_COLUMN_SIZE = 500

View File

@@ -1,8 +1,8 @@
package api
import (
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/db/application"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
)

View File

@@ -0,0 +1,16 @@
package application
import "mayfly-go/internal/db/infrastructure/persistence"
var (
dbApp Db = newDbApp(persistence.GetDbRepo(), persistence.GetDbSqlRepo())
dbSqlExecApp DbSqlExec = newDbSqlExecApp(persistence.GetDbSqlExecRepo())
)
func GetDbApp() Db {
return dbApp
}
func GetDbSqlExecApp() DbSqlExec {
return dbSqlExecApp
}

View File

@@ -6,10 +6,10 @@ import (
"errors"
"fmt"
"mayfly-go/internal/constant"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/machine"
"mayfly-go/internal/devops/infrastructure/persistence"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
machineapp "mayfly-go/internal/machine/application"
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/global"
@@ -52,16 +52,18 @@ type Db interface {
GetDatabases(entity *entity.Db) []string
}
func newDbApp(dbRepo repository.Db, dbSqlRepo repository.DbSql) Db {
return &dbAppImpl{
dbRepo: dbRepo,
dbSqlRepo: dbSqlRepo,
}
}
type dbAppImpl struct {
dbRepo repository.Db
dbSqlRepo repository.DbSql
}
var DbApp Db = &dbAppImpl{
dbRepo: persistence.DbDao,
dbSqlRepo: persistence.DbSqlDao,
}
// 分页获取数据库信息列表
func (d *dbAppImpl) GetPageList(condition *entity.Db, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return d.dbRepo.GetDbList(condition, pageParam, toEntity, orderBy...)
@@ -265,7 +267,7 @@ func TestConnection(d *entity.Db) {
func GetDbConn(d *entity.Db, db string) (*sql.DB, error) {
// SSH Conect
if d.EnableSshTunnel == 1 && d.SshTunnelMachineId != 0 {
sshTunnelMachine := MachineApp.GetSshTunnelMachine(d.SshTunnelMachineId)
sshTunnelMachine := machineapp.GetMachineApp().GetSshTunnelMachine(d.SshTunnelMachineId)
if d.Type == entity.DbTypeMysql {
mysql.RegisterDialContext(d.Network, func(ctx context.Context, addr string) (net.Conn, error) {
return sshTunnelMachine.GetDialConn("tcp", addr)

View File

@@ -4,9 +4,8 @@ import (
"encoding/json"
"fmt"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/persistence"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/global"
"mayfly-go/pkg/model"
"strings"
@@ -15,6 +14,7 @@ import (
)
type DbSqlExec interface {
// 生成sql执行记录
GenExecLog(loginAccount *model.LoginAccount, dbId uint64, db string, sql string, dbInstance *DbInstance) *entity.DbSqlExec
@@ -28,12 +28,14 @@ type DbSqlExec interface {
GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
}
type dbSqlExecAppImpl struct {
dbSqlExecRepo repository.DbSqlExec
func newDbSqlExecApp(dbExecSqlRepo repository.DbSqlExec) DbSqlExec {
return &dbSqlExecAppImpl{
dbSqlExecRepo: dbExecSqlRepo,
}
}
var DbSqlExecApp DbSqlExec = &dbSqlExecAppImpl{
dbSqlExecRepo: persistence.DbSqlExecDao,
type dbSqlExecAppImpl struct {
dbSqlExecRepo repository.DbSqlExec
}
func (d *dbSqlExecAppImpl) GenExecLog(loginAccount *model.LoginAccount, dbId uint64, db string, sql string, dbInstance *DbInstance) *entity.DbSqlExec {

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -1,6 +1,6 @@
package repository
import "mayfly-go/internal/devops/domain/entity"
import "mayfly-go/internal/db/domain/entity"
type DbSql interface {
DeleteBy(condition *entity.DbSql)

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -2,18 +2,20 @@ package persistence
import (
"fmt"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type dbRepo struct{}
type dbRepoImpl struct{}
var DbDao repository.Db = &dbRepo{}
func newDbRepo() repository.Db {
return new(dbRepoImpl)
}
// 分页获取数据库信息列表
func (d *dbRepo) GetDbList(condition *entity.Db, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
func (d *dbRepoImpl) GetDbList(condition *entity.Db, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
sql := "SELECT d.* FROM t_db d JOIN t_project_member pm ON d.project_id = pm.project_id WHERE 1 = 1 "
if condition.CreatorId != 0 {
// 使用创建者id模拟项目成员id
@@ -35,17 +37,17 @@ func (d *dbRepo) GetDbList(condition *entity.Db, pageParam *model.PageParam, toE
return model.GetPageBySql(sql, pageParam, toEntity)
}
func (d *dbRepo) Count(condition *entity.Db) int64 {
func (d *dbRepoImpl) Count(condition *entity.Db) int64 {
return model.CountBy(condition)
}
// 根据条件获取账号信息
func (d *dbRepo) GetDb(condition *entity.Db, cols ...string) error {
func (d *dbRepoImpl) GetDb(condition *entity.Db, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
func (d *dbRepo) GetById(id uint64, cols ...string) *entity.Db {
func (d *dbRepoImpl) GetById(id uint64, cols ...string) *entity.Db {
db := new(entity.Db)
if err := model.GetById(db, id, cols...); err != nil {
return nil
@@ -54,14 +56,14 @@ func (d *dbRepo) GetById(id uint64, cols ...string) *entity.Db {
return db
}
func (d *dbRepo) Insert(db *entity.Db) {
func (d *dbRepoImpl) Insert(db *entity.Db) {
biz.ErrIsNil(model.Insert(db), "新增数据库信息失败")
}
func (d *dbRepo) Update(db *entity.Db) {
func (d *dbRepoImpl) Update(db *entity.Db) {
biz.ErrIsNil(model.UpdateById(db), "更新数据库信息失败")
}
func (d *dbRepo) Delete(id uint64) {
func (d *dbRepoImpl) Delete(id uint64) {
model.DeleteById(new(entity.Db), id)
}

View File

@@ -0,0 +1,19 @@
package persistence
import (
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type dbSqlRepoImpl struct{}
func newDbSqlRepo() repository.DbSql {
return new(dbSqlRepoImpl)
}
// 分页获取数据库信息列表
func (d *dbSqlRepoImpl) DeleteBy(condition *entity.DbSql) {
biz.ErrIsNil(model.DeleteByCondition(condition), "删除sql失败")
}

View File

@@ -0,0 +1,27 @@
package persistence
import (
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type dbSqlExecRepoImpl struct{}
func newDbSqlExecRepo() repository.DbSqlExec {
return new(dbSqlExecRepoImpl)
}
func (d *dbSqlExecRepoImpl) Insert(dse *entity.DbSqlExec) {
model.Insert(dse)
}
func (d *dbSqlExecRepoImpl) DeleteBy(condition *entity.DbSqlExec) {
biz.ErrIsNil(model.DeleteByCondition(condition), "删除sql执行记录失败")
}
// 分页获取
func (d *dbSqlExecRepoImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}

View File

@@ -0,0 +1,21 @@
package persistence
import "mayfly-go/internal/db/domain/repository"
var (
dbRepo repository.Db = newDbRepo()
dbSqlRepo repository.DbSql = newDbSqlRepo()
dbSqlExecRepo repository.DbSqlExec = newDbSqlExecRepo()
)
func GetDbRepo() repository.Db {
return dbRepo
}
func GetDbSqlRepo() repository.DbSql {
return dbSqlRepo
}
func GetDbSqlExecRepo() repository.DbSqlExec {
return dbSqlExecRepo
}

View File

@@ -1,9 +1,10 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
sysApplication "mayfly-go/internal/sys/application"
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
projectapp "mayfly-go/internal/project/application"
sysapp "mayfly-go/internal/sys/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -13,10 +14,10 @@ func InitDbRouter(router *gin.RouterGroup) {
db := router.Group("dbs")
{
d := &api.Db{
DbApp: application.DbApp,
DbSqlExecApp: application.DbSqlExecApp,
MsgApp: sysApplication.MsgApp,
ProjectApp: application.ProjectApp,
DbApp: application.GetDbApp(),
DbSqlExecApp: application.GetDbSqlExecApp(),
MsgApp: sysapp.GetMsgApp(),
ProjectApp: projectapp.GetProjectApp(),
}
// 获取所有数据库列表
db.GET("", func(c *gin.Context) {

View File

@@ -1,8 +1,8 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/db/api"
"mayfly-go/internal/db/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -12,7 +12,7 @@ func InitDbSqlExecRouter(router *gin.RouterGroup) {
db := router.Group("/dbs/:dbId/sql-execs")
{
d := &api.DbSqlExec{
DbSqlExecApp: application.DbSqlExecApp,
DbSqlExecApp: application.GetDbSqlExecApp(),
}
// 获取所有数据库sql执行记录列表
db.GET("", func(c *gin.Context) {

View File

@@ -0,0 +1,8 @@
package router
import "github.com/gin-gonic/gin"
func Init(router *gin.RouterGroup) {
InitDbRouter(router)
InitDbSqlExecRouter(router)
}

View File

@@ -1,25 +0,0 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type dbSqlExecRepo struct{}
var DbSqlExecDao repository.DbSqlExec = &dbSqlExecRepo{}
func (d *dbSqlExecRepo) Insert(dse *entity.DbSqlExec) {
model.Insert(dse)
}
func (d *dbSqlExecRepo) DeleteBy(condition *entity.DbSqlExec) {
biz.ErrIsNil(model.DeleteByCondition(condition), "删除sql执行记录失败")
}
// 分页获取
func (d *dbSqlExecRepo) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}

View File

@@ -1,17 +0,0 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type dbSqlRepo struct{}
var DbSqlDao repository.DbSql = &dbSqlRepo{}
// 分页获取数据库信息列表
func (d *dbSqlRepo) DeleteBy(condition *entity.DbSql) {
biz.ErrIsNil(model.DeleteByCondition(condition), "删除sql失败")
}

View File

@@ -1,45 +0,0 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type machineFileRepo struct{}
var MachineFileDao repository.MachineFile = &machineFileRepo{}
// 分页获取机器文件信息列表
func (m *machineFileRepo) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
// 根据条件获取账号信息
func (m *machineFileRepo) GetMachineFile(condition *entity.MachineFile, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineFileRepo) GetById(id uint64, cols ...string) *entity.MachineFile {
ms := new(entity.MachineFile)
if err := model.GetById(ms, id, cols...); err != nil {
return nil
}
return ms
}
// 根据id获取
func (m *machineFileRepo) Delete(id uint64) {
biz.ErrIsNil(model.DeleteById(new(entity.MachineFile), id), "删除失败")
}
func (m *machineFileRepo) Create(entity *entity.MachineFile) {
model.Insert(entity)
}
func (m *machineFileRepo) UpdateById(entity *entity.MachineFile) {
model.UpdateById(entity)
}

View File

@@ -1,45 +0,0 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type machineScriptRepo struct{}
var MachineScriptDao repository.MachineScript = &machineScriptRepo{}
// 分页获取机器信息列表
func (m *machineScriptRepo) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
// 根据条件获取账号信息
func (m *machineScriptRepo) GetMachineScript(condition *entity.MachineScript, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineScriptRepo) GetById(id uint64, cols ...string) *entity.MachineScript {
ms := new(entity.MachineScript)
if err := model.GetById(ms, id, cols...); err != nil {
return nil
}
return ms
}
// 根据id获取
func (m *machineScriptRepo) Delete(id uint64) {
biz.ErrIsNil(model.DeleteById(new(entity.MachineScript), id), "删除失败")
}
func (m *machineScriptRepo) Create(entity *entity.MachineScript) {
model.Insert(entity)
}
func (m *machineScriptRepo) UpdateById(entity *entity.MachineScript) {
model.UpdateById(entity)
}

View File

@@ -1,28 +0,0 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type projectEnvRepo struct{}
var ProjectEnvRepo repository.ProjectEnv = &projectEnvRepo{}
func (p *projectEnvRepo) ListEnv(condition *entity.ProjectEnv, toEntity interface{}, orderBy ...string) {
model.ListByOrder(condition, toEntity, orderBy...)
}
func (p *projectEnvRepo) Save(entity *entity.ProjectEnv) {
biz.ErrIsNilAppendErr(model.Insert(entity), "保存环境失败:%s")
}
func (p *projectEnvRepo) DeleteEnvs(projectId uint64) {
model.DeleteByCondition(&entity.ProjectEnv{ProjectId: projectId})
}
func (p *projectEnvRepo) DeleteEnv(envId uint64) {
model.DeleteById(new(entity.ProjectEnv), envId)
}

View File

@@ -1,36 +0,0 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type projectMemeberRepo struct{}
var ProjectMemberRepo repository.ProjectMemeber = &projectMemeberRepo{}
func (p *projectMemeberRepo) ListMemeber(condition *entity.ProjectMember, toEntity interface{}, orderBy ...string) {
model.ListByOrder(condition, toEntity, orderBy...)
}
func (p *projectMemeberRepo) Save(pm *entity.ProjectMember) {
biz.ErrIsNilAppendErr(model.Insert(pm), "保存项目成员失败:%s")
}
func (p *projectMemeberRepo) GetPageList(condition *entity.ProjectMember, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
func (p *projectMemeberRepo) DeleteByPidMid(projectId, accountId uint64) {
model.DeleteByCondition(&entity.ProjectMember{ProjectId: projectId, AccountId: accountId})
}
func (p *projectMemeberRepo) DeleteMems(projectId uint64) {
model.DeleteByCondition(&entity.ProjectMember{ProjectId: projectId})
}
func (p *projectMemeberRepo) IsExist(projectId, accountId uint64) bool {
return model.CountBy(&entity.ProjectMember{ProjectId: projectId, AccountId: accountId}) > 0
}

View File

@@ -1,36 +0,0 @@
package persistence
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type projectRepo struct{}
var ProjectRepo repository.Project = &projectRepo{}
func (p *projectRepo) GetPageList(condition *entity.Project, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
func (p *projectRepo) Count(condition *entity.Project) int64 {
return model.CountBy(condition)
}
func (p *projectRepo) GetByIdIn(ids []uint64, toEntity interface{}, orderBy ...string) {
model.GetByIdIn(new(entity.Project), toEntity, ids, orderBy...)
}
func (p *projectRepo) Save(project *entity.Project) {
biz.ErrIsNil(model.Insert(project), "保存项目失败")
}
func (p *projectRepo) Update(project *entity.Project) {
biz.ErrIsNil(model.UpdateById(project), "更新项目信息")
}
func (p *projectRepo) Delete(id uint64) {
model.DeleteById(new(entity.Project), id)
}

View File

@@ -3,11 +3,12 @@ package api
import (
"encoding/base64"
"fmt"
"mayfly-go/internal/devops/api/form"
"mayfly-go/internal/devops/api/vo"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/infrastructure/machine"
"mayfly-go/internal/machine/api/form"
"mayfly-go/internal/machine/api/vo"
"mayfly-go/internal/machine/application"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/infrastructure/machine"
projectapp "mayfly-go/internal/project/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/config"
"mayfly-go/pkg/ctx"
@@ -26,7 +27,7 @@ import (
type Machine struct {
MachineApp application.Machine
ProjectApp application.Project
ProjectApp projectapp.Project
}
func (m *Machine) Machines(rc *ctx.ReqCtx) {

View File

@@ -4,10 +4,10 @@ import (
"fmt"
"io"
"io/fs"
"mayfly-go/internal/devops/api/form"
"mayfly-go/internal/devops/api/vo"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/machine/api/form"
"mayfly-go/internal/machine/api/vo"
"mayfly-go/internal/machine/application"
"mayfly-go/internal/machine/domain/entity"
sysApplication "mayfly-go/internal/sys/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"

View File

@@ -2,10 +2,11 @@ package api
import (
"fmt"
"mayfly-go/internal/devops/api/form"
"mayfly-go/internal/devops/api/vo"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/machine/api/form"
"mayfly-go/internal/machine/api/vo"
"mayfly-go/internal/machine/application"
"mayfly-go/internal/machine/domain/entity"
projectapp "mayfly-go/internal/project/application"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
@@ -18,7 +19,7 @@ import (
type MachineScript struct {
MachineScriptApp application.MachineScript
MachineApp application.Machine
ProjectApp application.Project
ProjectApp projectapp.Project
}
func (m *MachineScript) MachineScripts(rc *ctx.ReqCtx) {

View File

@@ -0,0 +1,21 @@
package application
import "mayfly-go/internal/machine/infrastructure/persistence"
var (
machineApp Machine = newMachineApp(persistence.GetMachineRepo())
machineFileApp MachineFile = newMachineFileApp(persistence.GetMachineFileRepo(), persistence.GetMachineRepo())
machineScriptApp MachineScript = newMachineScriptApp(persistence.GetMachineScriptRepo(), persistence.GetMachineRepo())
)
func GetMachineApp() Machine {
return machineApp
}
func GetMachineFileApp() MachineFile {
return machineFileApp
}
func GetMachineScriptApp() MachineScript {
return machineScriptApp
}

View File

@@ -1,10 +1,9 @@
package application
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/machine"
"mayfly-go/internal/devops/infrastructure/persistence"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
@@ -37,12 +36,15 @@ type Machine interface {
GetSshTunnelMachine(id uint64) *machine.SshTunnelMachine
}
func newMachineApp(machineRepo repository.Machine) Machine {
return &machineAppImpl{machineRepo: machineRepo}
}
type machineAppImpl struct {
machineRepo repository.Machine
}
var MachineApp Machine = &machineAppImpl{machineRepo: persistence.MachineDao}
// 分页获取机器信息列表
func (m *machineAppImpl) GetMachineList(condition *entity.Machine, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return m.machineRepo.GetMachineList(condition, pageParam, toEntity, orderBy...)

View File

@@ -4,9 +4,8 @@ import (
"fmt"
"io"
"io/fs"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/persistence"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
"os"
@@ -53,17 +52,16 @@ type MachineFile interface {
RemoveFile(fileId uint64, path string)
}
func newMachineFileApp(machineFileRepo repository.MachineFile, machineRepo repository.Machine) MachineFile {
return &machineFileAppImpl{machineRepo: machineRepo, machineFileRepo: machineFileRepo}
}
type machineFileAppImpl struct {
machineFileRepo repository.MachineFile
machineRepo repository.Machine
}
// 实现类单例
var MachineFileApp MachineFile = &machineFileAppImpl{
machineRepo: persistence.MachineDao,
machineFileRepo: persistence.MachineFileDao,
}
// 分页获取机器脚本信息列表
func (m *machineFileAppImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return m.machineFileRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
@@ -176,7 +174,7 @@ func (m *machineFileAppImpl) RemoveFile(fileId uint64, path string) {
err = sftpCli.RemoveDirectory(path)
// 如果文件夹有内容会删除失败则使用rm -rf命令删除
if err != nil {
MachineApp.GetCli(machineId).Run(fmt.Sprintf("rm -rf %s", path))
GetMachineApp().GetCli(machineId).Run(fmt.Sprintf("rm -rf %s", path))
err = nil
}
} else {
@@ -187,7 +185,7 @@ func (m *machineFileAppImpl) RemoveFile(fileId uint64, path string) {
// 获取sftp client
func (m *machineFileAppImpl) getSftpCli(machineId uint64) *sftp.Client {
return MachineApp.GetCli(machineId).GetSftpCli()
return GetMachineApp().GetCli(machineId).GetSftpCli()
}
// 校验并返回实际可访问的文件path

View File

@@ -1,9 +1,8 @@
package application
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/persistence"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
@@ -23,6 +22,11 @@ type MachineScript interface {
Delete(id uint64)
}
func newMachineScriptApp(machineScriptRepo repository.MachineScript, machineRepo repository.Machine) MachineScript {
return &machineScriptAppImpl{machineRepo: machineRepo, machineScriptRepo: machineScriptRepo}
}
type machineScriptAppImpl struct {
machineScriptRepo repository.MachineScript
machineRepo repository.Machine
@@ -30,10 +34,10 @@ type machineScriptAppImpl struct {
const Common_Script_Machine_Id = 9999999
// 实现类单例
var MachineScriptApp MachineScript = &machineScriptAppImpl{
machineRepo: persistence.MachineDao,
machineScriptRepo: persistence.MachineScriptDao}
// // 实现类单例
// var MachineScriptApp MachineScript = &machineScriptAppImpl{
// machineRepo: persistence.MachineDao,
// machineScriptRepo: persistence.MachineScriptDao}
// 分页获取机器脚本信息列表
func (m *machineScriptAppImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"mayfly-go/internal/constant"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/global"

View File

@@ -3,7 +3,7 @@ package machine
import (
"fmt"
"io"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/pkg/global"
"mayfly-go/pkg/scheduler"
"mayfly-go/pkg/utils"

View File

@@ -279,16 +279,21 @@ func getCPU(cpuInfo string, stats *Stats) (err error) {
value := strings.Split(cpuInfo, ":")[1]
values := strings.Split(value, ",")
us, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[0]), " ")[0], 32)
separator := " "
// 兼容旧版本使用%的情况
if strings.Contains(values[0], "%") {
separator = "%"
}
us, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[0]), separator)[0], 32)
stats.CPU.User = float32(us)
sy, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[1]), " ")[0], 32)
sy, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[1]), separator)[0], 32)
stats.CPU.System = float32(sy)
id, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[3]), " ")[0], 32)
id, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[3]), separator)[0], 32)
stats.CPU.Idle = float32(id)
wa, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[4]), " ")[0], 32)
wa, _ := strconv.ParseFloat(strings.Split(strings.TrimSpace(values[4]), separator)[0], 32)
stats.CPU.Iowait = float32(wa)
return nil

View File

@@ -2,18 +2,20 @@ package persistence
import (
"fmt"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type machineRepo struct{}
type machineRepoImpl struct{}
var MachineDao repository.Machine = &machineRepo{}
func newMachineRepo() repository.Machine {
return new(machineRepoImpl)
}
// 分页获取机器信息列表
func (m *machineRepo) GetMachineList(condition *entity.Machine, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
func (m *machineRepoImpl) GetMachineList(condition *entity.Machine, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
sql := "SELECT m.* FROM t_machine m JOIN t_project_member pm ON m.project_id = pm.project_id WHERE 1 = 1 "
if condition.CreatorId != 0 {
// 使用创建者id模拟项目成员id
@@ -32,17 +34,17 @@ func (m *machineRepo) GetMachineList(condition *entity.Machine, pageParam *model
return model.GetPageBySql(sql, pageParam, toEntity)
}
func (m *machineRepo) Count(condition *entity.Machine) int64 {
func (m *machineRepoImpl) Count(condition *entity.Machine) int64 {
return model.CountBy(condition)
}
// 根据条件获取账号信息
func (m *machineRepo) GetMachine(condition *entity.Machine, cols ...string) error {
func (m *machineRepoImpl) GetMachine(condition *entity.Machine, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineRepo) GetById(id uint64, cols ...string) *entity.Machine {
func (m *machineRepoImpl) GetById(id uint64, cols ...string) *entity.Machine {
machine := new(entity.Machine)
if err := model.GetById(machine, id, cols...); err != nil {
return nil
@@ -51,10 +53,10 @@ func (m *machineRepo) GetById(id uint64, cols ...string) *entity.Machine {
return machine
}
func (m *machineRepo) Create(entity *entity.Machine) {
func (m *machineRepoImpl) Create(entity *entity.Machine) {
biz.ErrIsNilAppendErr(model.Insert(entity), "创建机器信息失败: %s")
}
func (m *machineRepo) UpdateById(entity *entity.Machine) {
func (m *machineRepoImpl) UpdateById(entity *entity.Machine) {
biz.ErrIsNilAppendErr(model.UpdateById(entity), "更新机器信息失败: %s")
}

View File

@@ -0,0 +1,47 @@
package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type machineFileRepoImpl struct{}
func newMachineFileRepo() repository.MachineFile {
return new(machineFileRepoImpl)
}
// 分页获取机器文件信息列表
func (m *machineFileRepoImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
// 根据条件获取账号信息
func (m *machineFileRepoImpl) GetMachineFile(condition *entity.MachineFile, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineFileRepoImpl) GetById(id uint64, cols ...string) *entity.MachineFile {
ms := new(entity.MachineFile)
if err := model.GetById(ms, id, cols...); err != nil {
return nil
}
return ms
}
// 根据id获取
func (m *machineFileRepoImpl) Delete(id uint64) {
biz.ErrIsNil(model.DeleteById(new(entity.MachineFile), id), "删除失败")
}
func (m *machineFileRepoImpl) Create(entity *entity.MachineFile) {
model.Insert(entity)
}
func (m *machineFileRepoImpl) UpdateById(entity *entity.MachineFile) {
model.UpdateById(entity)
}

View File

@@ -0,0 +1,47 @@
package persistence
import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type machineScriptRepoImpl struct{}
func newMachineScriptRepo() repository.MachineScript {
return new(machineScriptRepoImpl)
}
// 分页获取机器信息列表
func (m *machineScriptRepoImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
// 根据条件获取账号信息
func (m *machineScriptRepoImpl) GetMachineScript(condition *entity.MachineScript, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
func (m *machineScriptRepoImpl) GetById(id uint64, cols ...string) *entity.MachineScript {
ms := new(entity.MachineScript)
if err := model.GetById(ms, id, cols...); err != nil {
return nil
}
return ms
}
// 根据id获取
func (m *machineScriptRepoImpl) Delete(id uint64) {
biz.ErrIsNil(model.DeleteById(new(entity.MachineScript), id), "删除失败")
}
func (m *machineScriptRepoImpl) Create(entity *entity.MachineScript) {
model.Insert(entity)
}
func (m *machineScriptRepoImpl) UpdateById(entity *entity.MachineScript) {
model.UpdateById(entity)
}

View File

@@ -0,0 +1,21 @@
package persistence
import "mayfly-go/internal/machine/domain/repository"
var (
machineRepo repository.Machine = newMachineRepo()
machineFileRepo repository.MachineFile = newMachineFileRepo()
machineScriptRepo repository.MachineScript = newMachineScriptRepo()
)
func GetMachineRepo() repository.Machine {
return machineRepo
}
func GetMachineFileRepo() repository.MachineFile {
return machineFileRepo
}
func GetMachineScriptRepo() repository.MachineScript {
return machineScriptRepo
}

View File

@@ -1,8 +1,9 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/machine/api"
"mayfly-go/internal/machine/application"
projectapp "mayfly-go/internal/project/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -10,8 +11,8 @@ import (
func InitMachineRouter(router *gin.RouterGroup) {
m := &api.Machine{
MachineApp: application.MachineApp,
ProjectApp: application.ProjectApp,
MachineApp: application.GetMachineApp(),
ProjectApp: projectapp.GetProjectApp(),
}
machines := router.Group("machines")

View File

@@ -1,8 +1,8 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/machine/api"
"mayfly-go/internal/machine/application"
sysApplication "mayfly-go/internal/sys/application"
"mayfly-go/pkg/ctx"
@@ -13,9 +13,9 @@ func InitMachineFileRouter(router *gin.RouterGroup) {
machineFile := router.Group("machines")
{
mf := &api.MachineFile{
MachineFileApp: application.MachineFileApp,
MachineApp: application.MachineApp,
MsgApp: sysApplication.MsgApp,
MachineFileApp: application.GetMachineFileApp(),
MachineApp: application.GetMachineApp(),
MsgApp: sysApplication.GetMsgApp(),
}
// 获取指定机器文件列表

View File

@@ -1,8 +1,9 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/machine/api"
"mayfly-go/internal/machine/application"
projectapp "mayfly-go/internal/project/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -12,9 +13,9 @@ func InitMachineScriptRouter(router *gin.RouterGroup) {
machines := router.Group("machines")
{
ms := &api.MachineScript{
MachineScriptApp: application.MachineScriptApp,
MachineApp: application.MachineApp,
ProjectApp: application.ProjectApp,
MachineScriptApp: application.GetMachineScriptApp(),
MachineApp: application.GetMachineApp(),
ProjectApp: projectapp.GetProjectApp(),
}
// 获取指定机器脚本列表

View File

@@ -0,0 +1,9 @@
package router
import "github.com/gin-gonic/gin"
func Init(router *gin.RouterGroup) {
InitMachineRouter(router)
InitMachineFileRouter(router)
InitMachineScriptRouter(router)
}

View File

@@ -2,9 +2,9 @@ package api
import (
"context"
"mayfly-go/internal/devops/api/form"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/mongo/api/form"
"mayfly-go/internal/mongo/application"
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"

View File

@@ -0,0 +1,11 @@
package application
import "mayfly-go/internal/mongo/infrastructure/persistence"
var (
mongoApp Mongo = newMongoAppImpl(persistence.GetMongoRepo())
)
func GetMongoApp() Mongo {
return mongoApp
}

View File

@@ -3,10 +3,10 @@ package application
import (
"context"
"mayfly-go/internal/constant"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/machine"
"mayfly-go/internal/devops/infrastructure/persistence"
machineapp "mayfly-go/internal/machine/application"
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/internal/mongo/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/global"
@@ -42,12 +42,14 @@ type Mongo interface {
GetMongoCli(id uint64) *mongo.Client
}
type mongoAppImpl struct {
mongoRepo repository.Mongo
func newMongoAppImpl(mongoRepo repository.Mongo) Mongo {
return &mongoAppImpl{
mongoRepo: mongoRepo,
}
}
var MongoApp Mongo = &mongoAppImpl{
mongoRepo: persistence.MongoDao,
type mongoAppImpl struct {
mongoRepo repository.Mongo
}
// 分页获取数据库信息列表
@@ -191,7 +193,7 @@ type MongoSshDialer struct {
}
func (sd *MongoSshDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if sshConn, err := MachineApp.GetSshTunnelMachine(sd.machineId).GetDialConn(network, address); err == nil {
if sshConn, err := machineapp.GetMachineApp().GetSshTunnelMachine(sd.machineId).GetDialConn(network, address); err == nil {
// 将ssh conn包装否则内部部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
return &utils.WrapSshConn{Conn: sshConn}, nil
} else {

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -2,18 +2,20 @@ package persistence
import (
"fmt"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/internal/mongo/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type mongoRepo struct{}
type mongoRepoImpl struct{}
var MongoDao repository.Mongo = &mongoRepo{}
func newMongoRepo() repository.Mongo {
return new(mongoRepoImpl)
}
// 分页获取数据库信息列表
func (d *mongoRepo) GetList(condition *entity.Mongo, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
func (d *mongoRepoImpl) GetList(condition *entity.Mongo, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
sql := "SELECT d.* FROM t_mongo d JOIN t_project_member pm ON d.project_id = pm.project_id WHERE 1 = 1 "
if condition.CreatorId != 0 {
// 使用创建者id模拟项目成员id
@@ -29,17 +31,17 @@ func (d *mongoRepo) GetList(condition *entity.Mongo, pageParam *model.PageParam,
return model.GetPageBySql(sql, pageParam, toEntity)
}
func (d *mongoRepo) Count(condition *entity.Mongo) int64 {
func (d *mongoRepoImpl) Count(condition *entity.Mongo) int64 {
return model.CountBy(condition)
}
// 根据条件获取
func (d *mongoRepo) Get(condition *entity.Mongo, cols ...string) error {
func (d *mongoRepoImpl) Get(condition *entity.Mongo, cols ...string) error {
return model.GetBy(condition, cols...)
}
// 根据id获取
func (d *mongoRepo) GetById(id uint64, cols ...string) *entity.Mongo {
func (d *mongoRepoImpl) GetById(id uint64, cols ...string) *entity.Mongo {
db := new(entity.Mongo)
if err := model.GetById(db, id, cols...); err != nil {
return nil
@@ -48,14 +50,14 @@ func (d *mongoRepo) GetById(id uint64, cols ...string) *entity.Mongo {
return db
}
func (d *mongoRepo) Insert(db *entity.Mongo) {
func (d *mongoRepoImpl) Insert(db *entity.Mongo) {
biz.ErrIsNil(model.Insert(db), "新增mongo信息失败")
}
func (d *mongoRepo) Update(db *entity.Mongo) {
func (d *mongoRepoImpl) Update(db *entity.Mongo) {
biz.ErrIsNil(model.UpdateById(db), "更新mongo信息失败")
}
func (d *mongoRepo) Delete(id uint64) {
func (d *mongoRepoImpl) Delete(id uint64) {
model.DeleteById(new(entity.Mongo), id)
}

View File

@@ -0,0 +1,13 @@
package persistence
import (
"mayfly-go/internal/mongo/domain/repository"
)
var (
mongoRepo repository.Mongo = newMongoRepo()
)
func GetMongoRepo() repository.Mongo {
return mongoRepo
}

View File

@@ -1,8 +1,8 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/mongo/api"
"mayfly-go/internal/mongo/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -12,7 +12,7 @@ func InitMongoRouter(router *gin.RouterGroup) {
m := router.Group("mongos")
{
ma := &api.Mongo{
MongoApp: application.MongoApp,
MongoApp: application.GetMongoApp(),
}
// 获取所有mongo列表

View File

@@ -0,0 +1,7 @@
package router
import "github.com/gin-gonic/gin"
func Init(router *gin.RouterGroup) {
InitMongoRouter(router)
}

View File

@@ -2,9 +2,9 @@ package api
import (
"fmt"
"mayfly-go/internal/devops/api/vo"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/project/api/vo"
projectapp "mayfly-go/internal/project/application"
"mayfly-go/internal/project/domain/entity"
sys_applicaiton "mayfly-go/internal/sys/application"
sys_entity "mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
@@ -13,7 +13,7 @@ import (
)
type Project struct {
ProjectApp application.Project
ProjectApp projectapp.Project
AccountApp sys_applicaiton.Account
}

View File

@@ -0,0 +1,25 @@
package application
import (
dbapp "mayfly-go/internal/db/application"
machineapp "mayfly-go/internal/machine/application"
mongoapp "mayfly-go/internal/mongo/application"
"mayfly-go/internal/project/infrastructure/persistence"
redisapp "mayfly-go/internal/redis/application"
)
var (
projectApp Project = newProjectApp(
persistence.GetProjectRepo(),
persistence.GetProjectEnvRepo(),
persistence.GetProjectMemberRepo(),
machineapp.GetMachineApp(),
redisapp.GetRedisApp(),
dbapp.GetDbApp(),
mongoapp.GetMongoApp(),
)
)
func GetProjectApp() Project {
return projectApp
}

View File

@@ -1,9 +1,16 @@
package application
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/persistence"
dbapp "mayfly-go/internal/db/application"
dbentity "mayfly-go/internal/db/domain/entity"
machineapp "mayfly-go/internal/machine/application"
machineentity "mayfly-go/internal/machine/domain/entity"
mongoapp "mayfly-go/internal/mongo/application"
mongoentity "mayfly-go/internal/mongo/domain/entity"
"mayfly-go/internal/project/domain/entity"
"mayfly-go/internal/project/domain/repository"
redisapp "mayfly-go/internal/redis/application"
redisentity "mayfly-go/internal/redis/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
@@ -42,22 +49,31 @@ type Project interface {
CanAccess(accountId, projectId uint64) error
}
func newProjectApp(projectRepo repository.Project,
projectEnvRepo repository.ProjectEnv,
projectMemberRepo repository.ProjectMemeber,
machineApp machineapp.Machine,
redisApp redisapp.Redis,
dbApp dbapp.Db,
mongoApp mongoapp.Mongo) Project {
return &projectAppImpl{
projectRepo: projectRepo,
projectEnvRepo: projectEnvRepo,
projectMemberRepo: projectMemberRepo,
machineApp: machineApp,
redisApp: redisApp,
dbApp: dbApp,
}
}
type projectAppImpl struct {
projectRepo repository.Project
projectEnvRepo repository.ProjectEnv
projectMemberRepo repository.ProjectMemeber
machineRepo repository.Machine
redisRepo repository.Redis
dbRepo repository.Db
}
var ProjectApp Project = &projectAppImpl{
projectRepo: persistence.ProjectRepo,
projectEnvRepo: persistence.ProjectEnvRepo,
projectMemberRepo: persistence.ProjectMemberRepo,
machineRepo: persistence.MachineDao,
redisRepo: persistence.RedisDao,
dbRepo: persistence.DbDao,
machineApp machineapp.Machine
redisApp redisapp.Redis
mongoApp mongoapp.Mongo
dbApp dbapp.Db
}
// 分页获取项目信息列表
@@ -84,9 +100,10 @@ func (p *projectAppImpl) SaveProject(project *entity.Project) {
}
func (p *projectAppImpl) DelProject(id uint64) {
biz.IsTrue(p.machineRepo.Count(&entity.Machine{ProjectId: id}) == 0, "请先删除该项目关联的机器信息")
biz.IsTrue(p.redisRepo.Count(&entity.Redis{ProjectId: id}) == 0, "请先删除该项目关联的redis信息")
biz.IsTrue(p.dbRepo.Count(&entity.Db{ProjectId: id}) == 0, "请先删除该项目关联的数据库信息")
biz.IsTrue(p.machineApp.Count(&machineentity.Machine{ProjectId: id}) == 0, "请先删除该项目关联的机器信息")
biz.IsTrue(p.redisApp.Count(&redisentity.Redis{ProjectId: id}) == 0, "请先删除该项目关联的redis信息")
biz.IsTrue(p.dbApp.Count(&dbentity.Db{ProjectId: id}) == 0, "请先删除该项目关联的数据库信息")
biz.IsTrue(p.mongoApp.Count(&mongoentity.Mongo{ProjectId: id}) == 0, "请先删除该项目关联的Mongo信息")
p.projectRepo.Delete(id)
p.projectEnvRepo.DeleteEnvs(id)
p.projectMemberRepo.DeleteMems(id)
@@ -104,8 +121,9 @@ func (p *projectAppImpl) SaveProjectEnv(projectEnv *entity.ProjectEnv) {
// 删除项目环境信息
func (p *projectAppImpl) DelProjectEnv(id uint64) {
biz.IsTrue(p.redisRepo.Count(&entity.Redis{EnvId: id}) == 0, "请先删除该项目环境关联的redis信息")
biz.IsTrue(p.dbRepo.Count(&entity.Db{EnvId: id}) == 0, "请先删除该项目环境关联的数据库信息")
biz.IsTrue(p.redisApp.Count(&redisentity.Redis{EnvId: id}) == 0, "请先删除该项目环境关联的redis信息")
biz.IsTrue(p.dbApp.Count(&dbentity.Db{EnvId: id}) == 0, "请先删除该项目环境关联的数据库信息")
biz.IsTrue(p.mongoApp.Count(&mongoentity.Mongo{ProjectId: id}) == 0, "请先删除该项目环境关联的Mongo信息")
p.projectEnvRepo.DeleteEnv(id)
}

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/project/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -1,6 +1,6 @@
package repository
import "mayfly-go/internal/devops/domain/entity"
import "mayfly-go/internal/project/domain/entity"
type ProjectEnv interface {
// 获取项目环境列表

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/project/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -0,0 +1,21 @@
package persistence
import "mayfly-go/internal/project/domain/repository"
var (
projectRepo repository.Project = newProjectRepo()
projectEnvRepo repository.ProjectEnv = newProjectEnvRepo()
projectMemberRepo repository.ProjectMemeber = newProjectMemberRepo()
)
func GetProjectRepo() repository.Project {
return projectRepo
}
func GetProjectEnvRepo() repository.ProjectEnv {
return projectEnvRepo
}
func GetProjectMemberRepo() repository.ProjectMemeber {
return projectMemberRepo
}

View File

@@ -0,0 +1,38 @@
package persistence
import (
"mayfly-go/internal/project/domain/entity"
"mayfly-go/internal/project/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type projectRepoImpl struct{}
func newProjectRepo() repository.Project {
return new(projectRepoImpl)
}
func (p *projectRepoImpl) GetPageList(condition *entity.Project, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
func (p *projectRepoImpl) Count(condition *entity.Project) int64 {
return model.CountBy(condition)
}
func (p *projectRepoImpl) GetByIdIn(ids []uint64, toEntity interface{}, orderBy ...string) {
model.GetByIdIn(new(entity.Project), toEntity, ids, orderBy...)
}
func (p *projectRepoImpl) Save(project *entity.Project) {
biz.ErrIsNil(model.Insert(project), "保存项目失败")
}
func (p *projectRepoImpl) Update(project *entity.Project) {
biz.ErrIsNil(model.UpdateById(project), "更新项目信息")
}
func (p *projectRepoImpl) Delete(id uint64) {
model.DeleteById(new(entity.Project), id)
}

View File

@@ -0,0 +1,30 @@
package persistence
import (
"mayfly-go/internal/project/domain/entity"
"mayfly-go/internal/project/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type projectEnvRepoImpl struct{}
func newProjectEnvRepo() repository.ProjectEnv {
return new(projectEnvRepoImpl)
}
func (p *projectEnvRepoImpl) ListEnv(condition *entity.ProjectEnv, toEntity interface{}, orderBy ...string) {
model.ListByOrder(condition, toEntity, orderBy...)
}
func (p *projectEnvRepoImpl) Save(entity *entity.ProjectEnv) {
biz.ErrIsNilAppendErr(model.Insert(entity), "保存环境失败:%s")
}
func (p *projectEnvRepoImpl) DeleteEnvs(projectId uint64) {
model.DeleteByCondition(&entity.ProjectEnv{ProjectId: projectId})
}
func (p *projectEnvRepoImpl) DeleteEnv(envId uint64) {
model.DeleteById(new(entity.ProjectEnv), envId)
}

View File

@@ -0,0 +1,38 @@
package persistence
import (
"mayfly-go/internal/project/domain/entity"
"mayfly-go/internal/project/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type projectMemberRepoImpl struct{}
func newProjectMemberRepo() repository.ProjectMemeber {
return new(projectMemberRepoImpl)
}
func (p *projectMemberRepoImpl) ListMemeber(condition *entity.ProjectMember, toEntity interface{}, orderBy ...string) {
model.ListByOrder(condition, toEntity, orderBy...)
}
func (p *projectMemberRepoImpl) Save(pm *entity.ProjectMember) {
biz.ErrIsNilAppendErr(model.Insert(pm), "保存项目成员失败:%s")
}
func (p *projectMemberRepoImpl) GetPageList(condition *entity.ProjectMember, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
return model.GetPage(pageParam, condition, toEntity, orderBy...)
}
func (p *projectMemberRepoImpl) DeleteByPidMid(projectId, accountId uint64) {
model.DeleteByCondition(&entity.ProjectMember{ProjectId: projectId, AccountId: accountId})
}
func (p *projectMemberRepoImpl) DeleteMems(projectId uint64) {
model.DeleteByCondition(&entity.ProjectMember{ProjectId: projectId})
}
func (p *projectMemberRepoImpl) IsExist(projectId, accountId uint64) bool {
return model.CountBy(&entity.ProjectMember{ProjectId: projectId, AccountId: accountId}) > 0
}

View File

@@ -1,9 +1,9 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
sys_applicaiton "mayfly-go/internal/sys/application"
"mayfly-go/internal/project/api"
projectapp "mayfly-go/internal/project/application"
sysapp "mayfly-go/internal/sys/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -11,8 +11,8 @@ import (
func InitProjectRouter(router *gin.RouterGroup) {
m := &api.Project{
ProjectApp: application.ProjectApp,
AccountApp: sys_applicaiton.AccountApp}
ProjectApp: projectapp.GetProjectApp(),
AccountApp: sysapp.GetAccountApp()}
project := router.Group("/projects")
{

View File

@@ -0,0 +1,7 @@
package router
import "github.com/gin-gonic/gin"
func Init(router *gin.RouterGroup) {
InitProjectRouter(router)
}

View File

@@ -2,10 +2,11 @@ package api
import (
"context"
"mayfly-go/internal/devops/api/form"
"mayfly-go/internal/devops/api/vo"
"mayfly-go/internal/devops/application"
"mayfly-go/internal/devops/domain/entity"
projectapp "mayfly-go/internal/project/application"
"mayfly-go/internal/redis/api/form"
"mayfly-go/internal/redis/api/vo"
"mayfly-go/internal/redis/application"
"mayfly-go/internal/redis/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ctx"
"mayfly-go/pkg/ginx"
@@ -20,7 +21,7 @@ import (
type Redis struct {
RedisApp application.Redis
ProjectApp application.Project
ProjectApp projectapp.Project
}
func (r *Redis) RedisList(rc *ctx.ReqCtx) {

View File

@@ -0,0 +1,11 @@
package application
import "mayfly-go/internal/redis/infrastructure/persistence"
var (
redisApp Redis = newRedisApp(persistence.GetRedisRepo())
)
func GetRedisApp() Redis {
return redisApp
}

View File

@@ -4,10 +4,10 @@ import (
"context"
"fmt"
"mayfly-go/internal/constant"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/devops/infrastructure/machine"
"mayfly-go/internal/devops/infrastructure/persistence"
machineapp "mayfly-go/internal/machine/application"
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/internal/redis/domain/entity"
"mayfly-go/internal/redis/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/global"
@@ -41,12 +41,14 @@ type Redis interface {
GetRedisInstance(id uint64) *RedisInstance
}
type redisAppImpl struct {
redisRepo repository.Redis
func newRedisApp(redisRepo repository.Redis) Redis {
return &redisAppImpl{
redisRepo: redisRepo,
}
}
var RedisApp Redis = &redisAppImpl{
redisRepo: persistence.RedisDao,
type redisAppImpl struct {
redisRepo repository.Redis
}
// 分页获取机器脚本信息列表
@@ -207,7 +209,7 @@ func getRedisSentinelCient(re *entity.Redis) *RedisInstance {
}
func getRedisDialer(machineId uint64) func(ctx context.Context, network, addr string) (net.Conn, error) {
sshTunnel := MachineApp.GetSshTunnelMachine(machineId)
sshTunnel := machineapp.GetMachineApp().GetSshTunnelMachine(machineId)
return func(_ context.Context, network, addr string) (net.Conn, error) {
if sshConn, err := sshTunnel.GetDialConn(network, addr); err == nil {
// 将ssh conn包装否则redis内部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported

View File

@@ -1,7 +1,7 @@
package repository
import (
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/redis/domain/entity"
"mayfly-go/pkg/model"
)

View File

@@ -0,0 +1,11 @@
package persistence
import "mayfly-go/internal/redis/domain/repository"
var (
redisRepo repository.Redis = newRedisRepo()
)
func GetRedisRepo() repository.Redis {
return redisRepo
}

View File

@@ -2,18 +2,20 @@ package persistence
import (
"fmt"
"mayfly-go/internal/devops/domain/entity"
"mayfly-go/internal/devops/domain/repository"
"mayfly-go/internal/redis/domain/entity"
"mayfly-go/internal/redis/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
)
type redisRepo struct{}
type redisRepoImpl struct{}
var RedisDao repository.Redis = &redisRepo{}
func newRedisRepo() repository.Redis {
return new(redisRepoImpl)
}
// 分页获取机器信息列表
func (r *redisRepo) GetRedisList(condition *entity.Redis, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
func (r *redisRepoImpl) GetRedisList(condition *entity.Redis, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
sql := "SELECT d.* FROM t_redis d JOIN t_project_member pm ON d.project_id = pm.project_id WHERE 1 = 1 "
if condition.CreatorId != 0 {
// 使用创建者id模拟项目成员id
@@ -32,12 +34,12 @@ func (r *redisRepo) GetRedisList(condition *entity.Redis, pageParam *model.PageP
return model.GetPageBySql(sql, pageParam, toEntity)
}
func (r *redisRepo) Count(condition *entity.Redis) int64 {
func (r *redisRepoImpl) Count(condition *entity.Redis) int64 {
return model.CountBy(condition)
}
// 根据id获取
func (r *redisRepo) GetById(id uint64, cols ...string) *entity.Redis {
func (r *redisRepoImpl) GetById(id uint64, cols ...string) *entity.Redis {
rd := new(entity.Redis)
if err := model.GetById(rd, id, cols...); err != nil {
return nil
@@ -45,18 +47,18 @@ func (r *redisRepo) GetById(id uint64, cols ...string) *entity.Redis {
return rd
}
func (r *redisRepo) GetRedis(condition *entity.Redis, cols ...string) error {
func (r *redisRepoImpl) GetRedis(condition *entity.Redis, cols ...string) error {
return model.GetBy(condition, cols...)
}
func (r *redisRepo) Insert(redis *entity.Redis) {
func (r *redisRepoImpl) Insert(redis *entity.Redis) {
biz.ErrIsNilAppendErr(model.Insert(redis), "新增失败: %s")
}
func (r *redisRepo) Update(redis *entity.Redis) {
func (r *redisRepoImpl) Update(redis *entity.Redis) {
biz.ErrIsNilAppendErr(model.UpdateById(redis), "更新失败: %s")
}
func (r *redisRepo) Delete(id uint64) {
func (r *redisRepoImpl) Delete(id uint64) {
biz.ErrIsNilAppendErr(model.DeleteById(new(entity.Redis), id), "删除失败: %s")
}

View File

@@ -1,8 +1,9 @@
package router
import (
"mayfly-go/internal/devops/api"
"mayfly-go/internal/devops/application"
projectapp "mayfly-go/internal/project/application"
"mayfly-go/internal/redis/api"
"mayfly-go/internal/redis/application"
"mayfly-go/pkg/ctx"
"github.com/gin-gonic/gin"
@@ -12,8 +13,8 @@ func InitRedisRouter(router *gin.RouterGroup) {
redis := router.Group("redis")
{
rs := &api.Redis{
RedisApp: application.RedisApp,
ProjectApp: application.ProjectApp,
RedisApp: application.GetRedisApp(),
ProjectApp: projectapp.GetProjectApp(),
}
// 获取redis list

View File

@@ -0,0 +1,7 @@
package router
import "github.com/gin-gonic/gin"
func Init(router *gin.RouterGroup) {
InitRedisRouter(router)
}

View File

@@ -3,7 +3,6 @@ package application
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/internal/sys/infrastructure/persistence"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils"
@@ -23,12 +22,14 @@ type Account interface {
Delete(id uint64)
}
type accountAppImpl struct {
accountRepo repository.Account
func newAccountApp(accountRepo repository.Account) Account {
return &accountAppImpl{
accountRepo: accountRepo,
}
}
var AccountApp Account = &accountAppImpl{
accountRepo: persistence.AccountDao,
type accountAppImpl struct {
accountRepo repository.Account
}
// 根据条件获取账号信息

View File

@@ -0,0 +1,36 @@
package application
import "mayfly-go/internal/sys/infrastructure/persistence"
var (
accountApp = newAccountApp(persistence.GetAccountRepo())
configApp = newConfigApp(persistence.GetConfigRepo())
msgApp = newMsgApp(persistence.GetMsgRepo())
resourceApp = newResourceApp(persistence.GetResourceRepo())
roleApp = newRoleApp(persistence.GetRoleRepo())
syslogApp = newSyslogApp(persistence.GetSyslogRepo())
)
func GetAccountApp() Account {
return accountApp
}
func GetConfigApp() Config {
return configApp
}
func GetMsgApp() Msg {
return msgApp
}
func GetResourceApp() Resource {
return resourceApp
}
func GetRoleApp() Role {
return roleApp
}
func GetSyslogApp() Syslog {
return syslogApp
}

View File

@@ -3,7 +3,6 @@ package application
import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/domain/repository"
"mayfly-go/internal/sys/infrastructure/persistence"
"mayfly-go/pkg/global"
"mayfly-go/pkg/model"
)
@@ -17,12 +16,14 @@ type Config interface {
GetConfig(key string) *entity.Config
}
type configAppImpl struct {
configRepo repository.Config
func newConfigApp(configRepo repository.Config) Config {
return &configAppImpl{
configRepo: configRepo,
}
}
var ConfigApp Config = &configAppImpl{
configRepo: persistence.ConfigDao,
type configAppImpl struct {
configRepo repository.Config
}
func (a *configAppImpl) GetPageList(condition *entity.Config, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {

Some files were not shown because too many files have changed in this diff Show More