From 594ca43505fb4f90e82e4acb821689d4b19793c3 Mon Sep 17 00:00:00 2001 From: "meilin.huang" <954537473@qq.com> Date: Sat, 14 Jan 2023 16:29:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=8C=85=E5=90=8D=E5=8F=98?= =?UTF-8?q?=E6=9B=B4ctx=20->=20req?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/go.mod | 4 +- server/initialize/savelog.go | 4 +- server/internal/common/api/common.go | 4 +- server/internal/common/api/index.go | 4 +- server/internal/common/router/common.go | 4 +- server/internal/common/router/index.go | 4 +- server/internal/db/api/db.go | 38 +++++++------- server/internal/db/api/db_sql_exec.go | 4 +- server/internal/db/router/db.go | 48 ++++++++--------- server/internal/db/router/db_sql_exec.go | 4 +- server/internal/machine/api/machine.go | 30 +++++------ server/internal/machine/api/machine_file.go | 20 +++---- server/internal/machine/api/machine_script.go | 10 ++-- .../machine/infrastructure/machine/machine.go | 13 +++-- .../machine/infrastructure/machine/stats.go | 2 +- server/internal/machine/router/machine.go | 36 ++++++------- .../internal/machine/router/machine_file.go | 46 ++++++++-------- .../internal/machine/router/machine_script.go | 22 ++++---- server/internal/mongo/api/mongo.go | 22 ++++---- server/internal/mongo/router/mongo.go | 32 ++++++------ server/internal/redis/api/redis.go | 42 +++++++-------- server/internal/redis/router/redis.go | 52 +++++++++---------- server/internal/sys/api/account.go | 32 ++++++------ server/internal/sys/api/captcha.go | 4 +- server/internal/sys/api/config.go | 8 +-- server/internal/sys/api/resource.go | 12 ++--- server/internal/sys/api/role.go | 14 ++--- server/internal/sys/api/syslog.go | 4 +- server/internal/sys/api/system.go | 6 +-- server/internal/sys/application/syslog_app.go | 6 +-- server/internal/sys/router/account.go | 46 ++++++++-------- server/internal/sys/router/captcha.go | 4 +- server/internal/sys/router/config.go | 10 ++-- server/internal/sys/router/resource.go | 24 ++++----- server/internal/sys/router/role.go | 26 +++++----- server/internal/sys/router/syslog.go | 4 +- server/internal/tag/api/tag_tree.go | 10 ++-- server/internal/tag/api/team.go | 18 +++---- server/internal/tag/router/tag_tree.go | 18 +++---- server/internal/tag/router/team.go | 38 +++++++------- server/pkg/{ctx => req}/log_handler.go | 10 ++-- server/pkg/{ctx => req}/permission_handler.go | 4 +- server/pkg/{ctx => req}/req_ctx.go | 39 ++++++-------- server/pkg/{ctx => req}/token.go | 2 +- server/pkg/starter/run.go | 4 +- server/pkg/starter/web-server.go | 8 +-- 46 files changed, 395 insertions(+), 401 deletions(-) rename server/pkg/{ctx => req}/log_handler.go (92%) rename server/pkg/{ctx => req}/permission_handler.go (98%) rename server/pkg/{ctx => req}/req_ctx.go (71%) rename server/pkg/{ctx => req}/token.go (99%) diff --git a/server/go.mod b/server/go.mod index 1665ec1b..183f602b 100644 --- a/server/go.mod +++ b/server/go.mod @@ -18,8 +18,8 @@ require ( golang.org/x/crypto v0.5.0 // ssh gopkg.in/yaml.v3 v3.0.1 // gorm - gorm.io/driver/mysql v1.4.4 - gorm.io/gorm v1.24.2 + gorm.io/driver/mysql v1.4.5 + gorm.io/gorm v1.24.3 ) require ( diff --git a/server/initialize/savelog.go b/server/initialize/savelog.go index 23002022..a1dcdb06 100644 --- a/server/initialize/savelog.go +++ b/server/initialize/savelog.go @@ -2,9 +2,9 @@ package initialize import ( sysapp "mayfly-go/internal/sys/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" ) -func InitSaveLogFunc() ctx.SaveLogFunc { +func InitSaveLogFunc() req.SaveLogFunc { return sysapp.GetSyslogApp().SaveFromReq } diff --git a/server/internal/common/api/common.go b/server/internal/common/api/common.go index 43f1a323..d0fcd41a 100644 --- a/server/internal/common/api/common.go +++ b/server/internal/common/api/common.go @@ -2,14 +2,14 @@ package api import ( "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" ) type Common struct { } -func (i *Common) RasPublicKey(rc *ctx.ReqCtx) { +func (i *Common) RasPublicKey(rc *req.Ctx) { publicKeyStr, err := utils.GetRsaPublicKey() biz.ErrIsNilAppendErr(err, "rsa生成公私钥失败") rc.ResData = publicKeyStr diff --git a/server/internal/common/api/index.go b/server/internal/common/api/index.go index 9464d550..6564f675 100644 --- a/server/internal/common/api/index.go +++ b/server/internal/common/api/index.go @@ -10,7 +10,7 @@ import ( redisapp "mayfly-go/internal/redis/application" redisentity "mayfly-go/internal/redis/domain/entity" tagapp "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" ) type Index struct { @@ -21,7 +21,7 @@ type Index struct { MongoApp mongoapp.Mongo } -func (i *Index) Count(rc *ctx.ReqCtx) { +func (i *Index) Count(rc *req.Ctx) { accountId := rc.LoginAccount.Id tagIds := i.TagApp.ListTagIdByAccountId(accountId) diff --git a/server/internal/common/router/common.go b/server/internal/common/router/common.go index 5504d463..15bec118 100644 --- a/server/internal/common/router/common.go +++ b/server/internal/common/router/common.go @@ -2,7 +2,7 @@ package router import ( "mayfly-go/internal/common/api" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -13,7 +13,7 @@ func InitCommonRouter(router *gin.RouterGroup) { { // 获取公钥 common.GET("public-key", func(g *gin.Context) { - ctx.NewReqCtxWithGin(g). + req.NewCtxWithGin(g). WithNeedToken(false). Handle(c.RasPublicKey) }) diff --git a/server/internal/common/router/index.go b/server/internal/common/router/index.go index d64033d9..b1fcf93d 100644 --- a/server/internal/common/router/index.go +++ b/server/internal/common/router/index.go @@ -7,7 +7,7 @@ import ( mongoapp "mayfly-go/internal/mongo/application" redisapp "mayfly-go/internal/redis/application" tagapp "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -24,7 +24,7 @@ func InitIndexRouter(router *gin.RouterGroup) { { // 首页基本信息统计 index.GET("count", func(g *gin.Context) { - ctx.NewReqCtxWithGin(g). + req.NewCtxWithGin(g). Handle(i.Count) }) } diff --git a/server/internal/db/api/db.go b/server/internal/db/api/db.go index 798e427e..421ab6c2 100644 --- a/server/internal/db/api/db.go +++ b/server/internal/db/api/db.go @@ -10,9 +10,9 @@ import ( sysapp "mayfly-go/internal/sys/application" tagapp "mayfly-go/internal/tag/application" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" "mayfly-go/pkg/model" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "mayfly-go/pkg/ws" "strconv" @@ -33,7 +33,7 @@ type Db struct { const DEFAULT_ROW_SIZE = 1800 // @router /api/dbs [get] -func (d *Db) Dbs(rc *ctx.ReqCtx) { +func (d *Db) Dbs(rc *req.Ctx) { condition := new(entity.DbQuery) condition.TagPathLike = rc.GinCtx.Query("tagPath") @@ -47,7 +47,7 @@ func (d *Db) Dbs(rc *ctx.ReqCtx) { rc.ResData = d.DbApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.SelectDataDbVO)) } -func (d *Db) Save(rc *ctx.ReqCtx) { +func (d *Db) Save(rc *req.Ctx) { form := &form.DbForm{} ginx.BindJsonAndValid(rc.GinCtx, form) @@ -68,7 +68,7 @@ func (d *Db) Save(rc *ctx.ReqCtx) { } // 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码 -func (d *Db) GetDbPwd(rc *ctx.ReqCtx) { +func (d *Db) GetDbPwd(rc *req.Ctx) { dbId := GetDbId(rc.GinCtx) dbEntity := d.DbApp.GetById(dbId, "Password") dbEntity.PwdDecrypt() @@ -76,7 +76,7 @@ func (d *Db) GetDbPwd(rc *ctx.ReqCtx) { } // 获取数据库实例的所有数据库名 -func (d *Db) GetDatabaseNames(rc *ctx.ReqCtx) { +func (d *Db) GetDatabaseNames(rc *req.Ctx) { form := &form.DbForm{} ginx.BindJsonAndValid(rc.GinCtx, form) @@ -95,30 +95,30 @@ func (d *Db) GetDatabaseNames(rc *ctx.ReqCtx) { rc.ResData = d.DbApp.GetDatabases(db) } -func (d *Db) DeleteDb(rc *ctx.ReqCtx) { +func (d *Db) DeleteDb(rc *req.Ctx) { dbId := GetDbId(rc.GinCtx) d.DbApp.Delete(dbId) // 删除该库的sql执行记录 d.DbSqlExecApp.DeleteBy(&entity.DbSqlExec{DbId: dbId}) } -func (d *Db) TableInfos(rc *ctx.ReqCtx) { +func (d *Db) TableInfos(rc *req.Ctx) { rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableInfos() } -func (d *Db) TableIndex(rc *ctx.ReqCtx) { +func (d *Db) TableIndex(rc *req.Ctx) { tn := rc.GinCtx.Query("tableName") biz.NotEmpty(tn, "tableName不能为空") rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetTableIndex(tn) } -func (d *Db) GetCreateTableDdl(rc *ctx.ReqCtx) { +func (d *Db) GetCreateTableDdl(rc *req.Ctx) { tn := rc.GinCtx.Query("tableName") biz.NotEmpty(tn, "tableName不能为空") rc.ResData = d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)).GetMeta().GetCreateTableDdl(tn) } -func (d *Db) ExecSql(rc *ctx.ReqCtx) { +func (d *Db) ExecSql(rc *req.Ctx) { g := rc.GinCtx form := &form.DbSqlExecForm{} ginx.BindJsonAndValid(g, form) @@ -171,7 +171,7 @@ func (d *Db) ExecSql(rc *ctx.ReqCtx) { } // 执行sql文件 -func (d *Db) ExecSqlFile(rc *ctx.ReqCtx) { +func (d *Db) ExecSqlFile(rc *req.Ctx) { g := rc.GinCtx fileheader, err := g.FormFile("file") biz.ErrIsNilAppendErr(err, "读取sql文件失败: %s") @@ -233,7 +233,7 @@ func (d *Db) ExecSqlFile(rc *ctx.ReqCtx) { } // 数据库dump -func (d *Db) DumpSql(rc *ctx.ReqCtx) { +func (d *Db) DumpSql(rc *req.Ctx) { g := rc.GinCtx dbId, db := GetIdAndDb(g) dumpType := g.Query("type") @@ -315,13 +315,13 @@ func (d *Db) DumpSql(rc *ctx.ReqCtx) { } // @router /api/db/:dbId/t-metadata [get] -func (d *Db) TableMA(rc *ctx.ReqCtx) { +func (d *Db) TableMA(rc *req.Ctx) { dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)) rc.ResData = dbi.GetMeta().GetTables() } // @router /api/db/:dbId/c-metadata [get] -func (d *Db) ColumnMA(rc *ctx.ReqCtx) { +func (d *Db) ColumnMA(rc *req.Ctx) { g := rc.GinCtx tn := g.Query("tableName") biz.NotEmpty(tn, "tableName不能为空") @@ -331,7 +331,7 @@ func (d *Db) ColumnMA(rc *ctx.ReqCtx) { } // @router /api/db/:dbId/hint-tables [get] -func (d *Db) HintTables(rc *ctx.ReqCtx) { +func (d *Db) HintTables(rc *req.Ctx) { dbi := d.DbApp.GetDbInstance(GetIdAndDb(rc.GinCtx)) dm := dbi.GetMeta() @@ -371,7 +371,7 @@ func (d *Db) HintTables(rc *ctx.ReqCtx) { } // @router /api/db/:dbId/sql [post] -func (d *Db) SaveSql(rc *ctx.ReqCtx) { +func (d *Db) SaveSql(rc *req.Ctx) { g := rc.GinCtx account := rc.LoginAccount dbSqlForm := &form.DbSqlSaveForm{} @@ -399,7 +399,7 @@ func (d *Db) SaveSql(rc *ctx.ReqCtx) { } // 获取所有保存的sql names -func (d *Db) GetSqlNames(rc *ctx.ReqCtx) { +func (d *Db) GetSqlNames(rc *req.Ctx) { id, db := GetIdAndDb(rc.GinCtx) // 获取用于是否有该dbsql的保存记录,有则更改,否则新增 dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db} @@ -411,7 +411,7 @@ func (d *Db) GetSqlNames(rc *ctx.ReqCtx) { } // 删除保存的sql -func (d *Db) DeleteSql(rc *ctx.ReqCtx) { +func (d *Db) DeleteSql(rc *req.Ctx) { dbSql := &entity.DbSql{Type: 1, DbId: GetDbId(rc.GinCtx)} dbSql.CreatorId = rc.LoginAccount.Id dbSql.Name = rc.GinCtx.Query("name") @@ -421,7 +421,7 @@ func (d *Db) DeleteSql(rc *ctx.ReqCtx) { } // @router /api/db/:dbId/sql [get] -func (d *Db) GetSql(rc *ctx.ReqCtx) { +func (d *Db) GetSql(rc *req.Ctx) { id, db := GetIdAndDb(rc.GinCtx) // 根据创建者id, 数据库id,以及sql模板名称查询保存的sql信息 dbSql := &entity.DbSql{Type: 1, DbId: id, Db: db} diff --git a/server/internal/db/api/db_sql_exec.go b/server/internal/db/api/db_sql_exec.go index 6ec9fba4..b7e02a80 100644 --- a/server/internal/db/api/db_sql_exec.go +++ b/server/internal/db/api/db_sql_exec.go @@ -3,15 +3,15 @@ package api import ( "mayfly-go/internal/db/application" "mayfly-go/internal/db/domain/entity" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" ) type DbSqlExec struct { DbSqlExecApp application.DbSqlExec } -func (d *DbSqlExec) DbSqlExecs(rc *ctx.ReqCtx) { +func (d *DbSqlExec) DbSqlExecs(rc *req.Ctx) { g := rc.GinCtx m := &entity.DbSqlExec{DbId: uint64(ginx.QueryInt(g, "dbId", 0)), Db: g.Query("db"), diff --git a/server/internal/db/router/db.go b/server/internal/db/router/db.go index f855a390..7dfd2df5 100644 --- a/server/internal/db/router/db.go +++ b/server/internal/db/router/db.go @@ -5,7 +5,7 @@ import ( "mayfly-go/internal/db/application" sysapp "mayfly-go/internal/sys/application" tagapp "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -21,94 +21,94 @@ func InitDbRouter(router *gin.RouterGroup) { } // 获取所有数据库列表 db.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.Dbs) + req.NewCtxWithGin(c).Handle(d.Dbs) }) - saveDb := ctx.NewLogInfo("db-保存数据库信息").WithSave(true) + saveDb := req.NewLogInfo("db-保存数据库信息").WithSave(true) db.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(saveDb). Handle(d.Save) }) // 获取数据库实例的所有数据库名 db.POST("databases", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). Handle(d.GetDatabaseNames) }) db.GET(":dbId/pwd", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.GetDbPwd) + req.NewCtxWithGin(c).Handle(d.GetDbPwd) }) - deleteDb := ctx.NewLogInfo("db-删除数据库信息").WithSave(true) + deleteDb := req.NewLogInfo("db-删除数据库信息").WithSave(true) db.DELETE(":dbId", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(deleteDb). Handle(d.DeleteDb) }) db.GET(":dbId/t-infos", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.TableInfos) + req.NewCtxWithGin(c).Handle(d.TableInfos) }) db.GET(":dbId/t-index", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.TableIndex) + req.NewCtxWithGin(c).Handle(d.TableIndex) }) db.GET(":dbId/t-create-ddl", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.GetCreateTableDdl) + req.NewCtxWithGin(c).Handle(d.GetCreateTableDdl) }) - execSqlLog := ctx.NewLogInfo("db-执行Sql") + execSqlLog := req.NewLogInfo("db-执行Sql") db.POST(":dbId/exec-sql", func(g *gin.Context) { - rc := ctx.NewReqCtxWithGin(g).WithLog(execSqlLog) + rc := req.NewCtxWithGin(g).WithLog(execSqlLog) rc.Handle(d.ExecSql) }) - execSqlFileLog := ctx.NewLogInfo("db-执行Sql文件").WithSave(true) + execSqlFileLog := req.NewLogInfo("db-执行Sql文件").WithSave(true) db.POST(":dbId/exec-sql-file", func(g *gin.Context) { - ctx.NewReqCtxWithGin(g). + req.NewCtxWithGin(g). WithLog(execSqlFileLog). Handle(d.ExecSqlFile) }) - dumpLog := ctx.NewLogInfo("db-导出sql文件").WithSave(true) + dumpLog := req.NewLogInfo("db-导出sql文件").WithSave(true) db.GET(":dbId/dump", func(g *gin.Context) { - ctx.NewReqCtxWithGin(g). + req.NewCtxWithGin(g). WithLog(dumpLog). Handle(d.DumpSql) }) db.GET(":dbId/t-metadata", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.TableMA) + req.NewCtxWithGin(c).Handle(d.TableMA) }) db.GET(":dbId/c-metadata", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.ColumnMA) + req.NewCtxWithGin(c).Handle(d.ColumnMA) }) db.GET(":dbId/hint-tables", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.HintTables) + req.NewCtxWithGin(c).Handle(d.HintTables) }) /** db sql相关接口 */ db.POST(":dbId/sql", func(c *gin.Context) { - rc := ctx.NewReqCtxWithGin(c) + rc := req.NewCtxWithGin(c) rc.Handle(d.SaveSql) }) db.GET(":dbId/sql", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.GetSql) + req.NewCtxWithGin(c).Handle(d.GetSql) }) db.DELETE(":dbId/sql", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.DeleteSql) + req.NewCtxWithGin(c).Handle(d.DeleteSql) }) db.GET(":dbId/sql-names", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(d.GetSqlNames) + req.NewCtxWithGin(c).Handle(d.GetSqlNames) }) } } diff --git a/server/internal/db/router/db_sql_exec.go b/server/internal/db/router/db_sql_exec.go index 8ad30b67..5cdbbf77 100644 --- a/server/internal/db/router/db_sql_exec.go +++ b/server/internal/db/router/db_sql_exec.go @@ -3,7 +3,7 @@ package router import ( "mayfly-go/internal/db/api" "mayfly-go/internal/db/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -16,7 +16,7 @@ func InitDbSqlExecRouter(router *gin.RouterGroup) { } // 获取所有数据库sql执行记录列表 db.GET("", func(c *gin.Context) { - rc := ctx.NewReqCtxWithGin(c) + rc := req.NewCtxWithGin(c) rc.Handle(d.DbSqlExecs) }) } diff --git a/server/internal/machine/api/machine.go b/server/internal/machine/api/machine.go index 62e06b3a..8501f53c 100644 --- a/server/internal/machine/api/machine.go +++ b/server/internal/machine/api/machine.go @@ -11,9 +11,9 @@ import ( tagapp "mayfly-go/internal/tag/application" "mayfly-go/pkg/biz" "mayfly-go/pkg/config" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" "mayfly-go/pkg/model" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "mayfly-go/pkg/ws" "os" @@ -31,7 +31,7 @@ type Machine struct { TagApp tagapp.TagTree } -func (m *Machine) Machines(rc *ctx.ReqCtx) { +func (m *Machine) Machines(rc *req.Ctx) { condition := new(entity.MachineQuery) condition.Ip = rc.GinCtx.Query("ip") condition.Name = rc.GinCtx.Query("name") @@ -58,12 +58,12 @@ func (m *Machine) Machines(rc *ctx.ReqCtx) { rc.ResData = res } -func (m *Machine) MachineStats(rc *ctx.ReqCtx) { +func (m *Machine) MachineStats(rc *req.Ctx) { stats := m.MachineApp.GetCli(GetMachineId(rc.GinCtx)).GetAllStats() rc.ResData = stats } -func (m *Machine) SaveMachine(rc *ctx.ReqCtx) { +func (m *Machine) SaveMachine(rc *req.Ctx) { g := rc.GinCtx machineForm := new(form.MachineForm) ginx.BindJsonAndValid(g, machineForm) @@ -87,14 +87,14 @@ func (m *Machine) SaveMachine(rc *ctx.ReqCtx) { } // 获取机器实例密码,由于数据库是加密存储,故提供该接口展示原文密码 -func (m *Machine) GetMachinePwd(rc *ctx.ReqCtx) { +func (m *Machine) GetMachinePwd(rc *req.Ctx) { mid := GetMachineId(rc.GinCtx) me := m.MachineApp.GetById(mid, "Password") me.PwdDecrypt() rc.ResData = me.Password } -func (m *Machine) ChangeStatus(rc *ctx.ReqCtx) { +func (m *Machine) ChangeStatus(rc *req.Ctx) { g := rc.GinCtx id := uint64(ginx.PathParamInt(g, "machineId")) status := int8(ginx.PathParamInt(g, "status")) @@ -102,19 +102,19 @@ func (m *Machine) ChangeStatus(rc *ctx.ReqCtx) { m.MachineApp.ChangeStatus(id, status) } -func (m *Machine) DeleteMachine(rc *ctx.ReqCtx) { +func (m *Machine) DeleteMachine(rc *req.Ctx) { id := uint64(ginx.PathParamInt(rc.GinCtx, "machineId")) rc.ReqParam = id m.MachineApp.Delete(id) } // 关闭机器客户端 -func (m *Machine) CloseCli(rc *ctx.ReqCtx) { +func (m *Machine) CloseCli(rc *req.Ctx) { machine.DeleteCli(GetMachineId(rc.GinCtx)) } // 获取进程列表信息 -func (m *Machine) GetProcess(rc *ctx.ReqCtx) { +func (m *Machine) GetProcess(rc *req.Ctx) { g := rc.GinCtx cmd := "ps -aux " sortType := g.Query("sortType") @@ -145,7 +145,7 @@ func (m *Machine) GetProcess(rc *ctx.ReqCtx) { } // 终止进程 -func (m *Machine) KillProcess(rc *ctx.ReqCtx) { +func (m *Machine) KillProcess(rc *req.Ctx) { pid := rc.GinCtx.Query("pid") biz.NotEmpty(pid, "进程id不能为空") @@ -169,8 +169,8 @@ func (m *Machine) WsSSH(g *gin.Context) { biz.ErrIsNilAppendErr(err, "升级websocket失败: %s") // 权限校验 - rc := ctx.NewReqCtxWithGin(g).WithRequiredPermission(ctx.NewPermission("machine:terminal")) - if err = ctx.PermissionHandler(rc); err != nil { + rc := req.NewCtxWithGin(g).WithRequiredPermission(req.NewPermission("machine:terminal")) + if err = req.PermissionHandler(rc); err != nil { panic(biz.NewBizErr("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m")) } @@ -197,16 +197,16 @@ func (m *Machine) WsSSH(g *gin.Context) { biz.ErrIsNilAppendErr(err, "\033[1;31m连接失败: %s\033[0m") // 记录系统操作日志 - rc.WithLog(ctx.NewLogInfo("机器-终端操作").WithSave(true)) + rc.WithLog(req.NewLogInfo("机器-终端操作").WithSave(true)) rc.ReqParam = cli.GetMachine().GetLogDesc() - ctx.LogHandler(rc) + req.LogHandler(rc) mts.Start() defer mts.Stop() } // 获取机器终端回放记录的相应文件夹名或文件内容 -func (m *Machine) MachineRecDirNames(rc *ctx.ReqCtx) { +func (m *Machine) MachineRecDirNames(rc *req.Ctx) { readPath := rc.GinCtx.Query("path") biz.NotEmpty(readPath, "path不能为空") path_ := path.Join(config.Conf.Server.GetMachineRecPath(), readPath) diff --git a/server/internal/machine/api/machine_file.go b/server/internal/machine/api/machine_file.go index 8724134e..ed19d14c 100644 --- a/server/internal/machine/api/machine_file.go +++ b/server/internal/machine/api/machine_file.go @@ -10,8 +10,8 @@ import ( "mayfly-go/internal/machine/domain/entity" sysApplication "mayfly-go/internal/sys/application" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "mayfly-go/pkg/ws" "sort" @@ -33,13 +33,13 @@ const ( max_read_size = 1 * 1024 * 1024 ) -func (m *MachineFile) MachineFiles(rc *ctx.ReqCtx) { +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)) } -func (m *MachineFile) SaveMachineFiles(rc *ctx.ReqCtx) { +func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) { g := rc.GinCtx fileForm := new(form.MachineFileForm) ginx.BindJsonAndValid(g, fileForm) @@ -52,7 +52,7 @@ func (m *MachineFile) SaveMachineFiles(rc *ctx.ReqCtx) { m.MachineFileApp.Save(entity) } -func (m *MachineFile) DeleteFile(rc *ctx.ReqCtx) { +func (m *MachineFile) DeleteFile(rc *req.Ctx) { g := rc.GinCtx fid := GetMachineFileId(g) m.MachineFileApp.Delete(fid) @@ -60,7 +60,7 @@ func (m *MachineFile) DeleteFile(rc *ctx.ReqCtx) { /*** sftp相关操作 */ -func (m *MachineFile) CreateFile(rc *ctx.ReqCtx) { +func (m *MachineFile) CreateFile(rc *req.Ctx) { g := rc.GinCtx fid := GetMachineFileId(g) @@ -79,7 +79,7 @@ func (m *MachineFile) CreateFile(rc *ctx.ReqCtx) { } -func (m *MachineFile) ReadFileContent(rc *ctx.ReqCtx) { +func (m *MachineFile) ReadFileContent(rc *req.Ctx) { g := rc.GinCtx fid := GetMachineFileId(g) readPath := g.Query("path") @@ -109,7 +109,7 @@ func (m *MachineFile) ReadFileContent(rc *ctx.ReqCtx) { } } -func (m *MachineFile) GetDirEntry(rc *ctx.ReqCtx) { +func (m *MachineFile) GetDirEntry(rc *req.Ctx) { g := rc.GinCtx fid := GetMachineFileId(g) readPath := g.Query("path") @@ -134,7 +134,7 @@ func (m *MachineFile) GetDirEntry(rc *ctx.ReqCtx) { rc.ReqParam = fmt.Sprintf("path: %s", readPath) } -func (m *MachineFile) WriteFileContent(rc *ctx.ReqCtx) { +func (m *MachineFile) WriteFileContent(rc *req.Ctx) { g := rc.GinCtx fid := GetMachineFileId(g) @@ -148,7 +148,7 @@ func (m *MachineFile) WriteFileContent(rc *ctx.ReqCtx) { rc.ReqParam = fmt.Sprintf("%s -> 修改文件内容: %s", mi.GetLogDesc(), path) } -func (m *MachineFile) UploadFile(rc *ctx.ReqCtx) { +func (m *MachineFile) UploadFile(rc *req.Ctx) { g := rc.GinCtx fid := GetMachineFileId(g) path := g.PostForm("path") @@ -179,7 +179,7 @@ func (m *MachineFile) UploadFile(rc *ctx.ReqCtx) { }() } -func (m *MachineFile) RemoveFile(rc *ctx.ReqCtx) { +func (m *MachineFile) RemoveFile(rc *req.Ctx) { g := rc.GinCtx fid := GetMachineFileId(g) path := g.Query("path") diff --git a/server/internal/machine/api/machine_script.go b/server/internal/machine/api/machine_script.go index aca11f51..3b7f7995 100644 --- a/server/internal/machine/api/machine_script.go +++ b/server/internal/machine/api/machine_script.go @@ -8,8 +8,8 @@ import ( "mayfly-go/internal/machine/domain/entity" tagapp "mayfly-go/internal/tag/application" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "strconv" @@ -22,13 +22,13 @@ type MachineScript struct { TagApp tagapp.TagTree } -func (m *MachineScript) MachineScripts(rc *ctx.ReqCtx) { +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)) } -func (m *MachineScript) SaveMachineScript(rc *ctx.ReqCtx) { +func (m *MachineScript) SaveMachineScript(rc *req.Ctx) { form := new(form.MachineScriptForm) ginx.BindJsonAndValid(rc.GinCtx, form) rc.ReqParam = form @@ -41,7 +41,7 @@ func (m *MachineScript) SaveMachineScript(rc *ctx.ReqCtx) { m.MachineScriptApp.Save(machineScript) } -func (m *MachineScript) DeleteMachineScript(rc *ctx.ReqCtx) { +func (m *MachineScript) DeleteMachineScript(rc *req.Ctx) { msa := m.MachineScriptApp sid := GetMachineScriptId(rc.GinCtx) ms := msa.GetById(sid) @@ -50,7 +50,7 @@ func (m *MachineScript) DeleteMachineScript(rc *ctx.ReqCtx) { msa.Delete(sid) } -func (m *MachineScript) RunMachineScript(rc *ctx.ReqCtx) { +func (m *MachineScript) RunMachineScript(rc *req.Ctx) { g := rc.GinCtx scriptId := GetMachineScriptId(g) diff --git a/server/internal/machine/infrastructure/machine/machine.go b/server/internal/machine/infrastructure/machine/machine.go index e7dc651c..e6978fdf 100644 --- a/server/internal/machine/infrastructure/machine/machine.go +++ b/server/internal/machine/infrastructure/machine/machine.go @@ -92,19 +92,18 @@ func (c *Cli) GetSession() (*ssh.Session, error) { // 执行shell // @param shell shell脚本命令 -func (c *Cli) Run(shell string) (*string, error) { +func (c *Cli) Run(shell string) (string, error) { session, err := c.GetSession() if err != nil { c.Close() - return nil, err + return "", err } defer session.Close() - buf, rerr := session.CombinedOutput(shell) - if rerr != nil { - return nil, rerr + buf, err := session.CombinedOutput(shell) + if err != nil { + return "", err } - res := string(buf) - return &res, nil + return string(buf), nil } func (c *Cli) GetMachine() *entity.Machine { diff --git a/server/internal/machine/infrastructure/machine/stats.go b/server/internal/machine/infrastructure/machine/stats.go index 8dd95382..c4cebaf6 100644 --- a/server/internal/machine/infrastructure/machine/stats.go +++ b/server/internal/machine/infrastructure/machine/stats.go @@ -73,7 +73,7 @@ top -b -n 1 | grep Cpu func (c *Cli) GetAllStats() *Stats { res, _ := c.Run(StatsShell) - infos := strings.Split(*res, "-----") + infos := strings.Split(res, "-----") stats := new(Stats) getUptime(infos[0], stats) getHostname(infos[1], stats) diff --git a/server/internal/machine/router/machine.go b/server/internal/machine/router/machine.go index 6a11533d..409a7be5 100644 --- a/server/internal/machine/router/machine.go +++ b/server/internal/machine/router/machine.go @@ -4,7 +4,7 @@ import ( "mayfly-go/internal/machine/api" "mayfly-go/internal/machine/application" tagapp "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -18,64 +18,64 @@ func InitMachineRouter(router *gin.RouterGroup) { machines := router.Group("machines") { machines.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.Machines) + req.NewCtxWithGin(c).Handle(m.Machines) }) machines.GET(":machineId/pwd", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.GetMachinePwd) + req.NewCtxWithGin(c).Handle(m.GetMachinePwd) }) machines.GET(":machineId/stats", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.MachineStats) + req.NewCtxWithGin(c).Handle(m.MachineStats) }) machines.GET(":machineId/process", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.GetProcess) + req.NewCtxWithGin(c).Handle(m.GetProcess) }) // 终止进程 - killProcessL := ctx.NewLogInfo("终止进程").WithSave(true) - killProcessP := ctx.NewPermission("machine:killprocess") + killProcessL := req.NewLogInfo("终止进程").WithSave(true) + killProcessP := req.NewPermission("machine:killprocess") machines.DELETE(":machineId/process", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(killProcessL). WithRequiredPermission(killProcessP). Handle(m.KillProcess) }) - saveMachine := ctx.NewLogInfo("保存机器信息").WithSave(true) - saveMachineP := ctx.NewPermission("machine:update") + saveMachine := req.NewLogInfo("保存机器信息").WithSave(true) + saveMachineP := req.NewPermission("machine:update") machines.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(saveMachine). WithRequiredPermission(saveMachineP). Handle(m.SaveMachine) }) - changeStatus := ctx.NewLogInfo("调整机器状态").WithSave(true) + changeStatus := req.NewLogInfo("调整机器状态").WithSave(true) machines.PUT(":machineId/:status", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(changeStatus). Handle(m.ChangeStatus) }) - delMachine := ctx.NewLogInfo("删除机器").WithSave(true) + delMachine := req.NewLogInfo("删除机器").WithSave(true) machines.DELETE(":machineId", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(delMachine). Handle(m.DeleteMachine) }) - closeCli := ctx.NewLogInfo("关闭机器客户端").WithSave(true) + closeCli := req.NewLogInfo("关闭机器客户端").WithSave(true) machines.DELETE(":machineId/close-cli", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(closeCli).Handle(m.CloseCli) + req.NewCtxWithGin(c).WithLog(closeCli).Handle(m.CloseCli) }) machines.GET(":machineId/terminal", m.WsSSH) // 获取机器终端回放记录的相应文件夹名或文件名,目前具有保存机器信息的权限标识才有权限查看终端回放 machines.GET("rec/names", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithRequiredPermission(saveMachineP). Handle(m.MachineRecDirNames) }) diff --git a/server/internal/machine/router/machine_file.go b/server/internal/machine/router/machine_file.go index a7a8401d..b4bdaaa8 100644 --- a/server/internal/machine/router/machine_file.go +++ b/server/internal/machine/router/machine_file.go @@ -4,7 +4,7 @@ import ( "mayfly-go/internal/machine/api" "mayfly-go/internal/machine/application" sysApplication "mayfly-go/internal/sys/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -19,66 +19,66 @@ func InitMachineFileRouter(router *gin.RouterGroup) { // 获取指定机器文件列表 machineFile.GET(":machineId/files", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(mf.MachineFiles) + req.NewCtxWithGin(c).Handle(mf.MachineFiles) }) // 新增修改机器文件 - addFileConf := ctx.NewLogInfo("机器-新增文件配置").WithSave(true) - afcP := ctx.NewPermission("machine:file:add") + addFileConf := req.NewLogInfo("机器-新增文件配置").WithSave(true) + afcP := req.NewPermission("machine:file:add") machineFile.POST(":machineId/files", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(addFileConf). + req.NewCtxWithGin(c).WithLog(addFileConf). WithRequiredPermission(afcP). Handle(mf.SaveMachineFiles) }) // 删除机器文件 - delFileConf := ctx.NewLogInfo("机器-删除文件配置").WithSave(true) - dfcP := ctx.NewPermission("machine:file:del") + delFileConf := req.NewLogInfo("机器-删除文件配置").WithSave(true) + dfcP := req.NewPermission("machine:file:del") machineFile.DELETE(":machineId/files/:fileId", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(delFileConf). + req.NewCtxWithGin(c).WithLog(delFileConf). WithRequiredPermission(dfcP). Handle(mf.DeleteFile) }) - getContent := ctx.NewLogInfo("机器-获取文件内容").WithSave(true) + getContent := req.NewLogInfo("机器-获取文件内容").WithSave(true) machineFile.GET(":machineId/files/:fileId/read", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(getContent). + req.NewCtxWithGin(c).WithLog(getContent). Handle(mf.ReadFileContent) }) - getDir := ctx.NewLogInfo("机器-获取目录") + getDir := req.NewLogInfo("机器-获取目录") machineFile.GET(":machineId/files/:fileId/read-dir", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(getDir). + req.NewCtxWithGin(c).WithLog(getDir). Handle(mf.GetDirEntry) }) - writeFile := ctx.NewLogInfo("机器-修改文件内容").WithSave(true) - wfP := ctx.NewPermission("machine:file:write") + writeFile := req.NewLogInfo("机器-修改文件内容").WithSave(true) + wfP := req.NewPermission("machine:file:write") machineFile.POST(":machineId/files/:fileId/write", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(writeFile). + req.NewCtxWithGin(c).WithLog(writeFile). WithRequiredPermission(wfP). Handle(mf.WriteFileContent) }) - createFile := ctx.NewLogInfo("机器-创建文件or目录").WithSave(true) + createFile := req.NewLogInfo("机器-创建文件or目录").WithSave(true) machineFile.POST(":machineId/files/:fileId/create-file", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(createFile). + req.NewCtxWithGin(c).WithLog(createFile). WithRequiredPermission(wfP). Handle(mf.CreateFile) }) - uploadFile := ctx.NewLogInfo("机器-文件上传").WithSave(true) - ufP := ctx.NewPermission("machine:file:upload") + uploadFile := req.NewLogInfo("机器-文件上传").WithSave(true) + ufP := req.NewPermission("machine:file:upload") machineFile.POST(":machineId/files/:fileId/upload", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(uploadFile). + req.NewCtxWithGin(c).WithLog(uploadFile). WithRequiredPermission(ufP). Handle(mf.UploadFile) }) - removeFile := ctx.NewLogInfo("机器-删除文件or文件夹").WithSave(true) - rfP := ctx.NewPermission("machine:file:rm") + removeFile := req.NewLogInfo("机器-删除文件or文件夹").WithSave(true) + rfP := req.NewPermission("machine:file:rm") machineFile.DELETE(":machineId/files/:fileId/remove", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(removeFile). + req.NewCtxWithGin(c).WithLog(removeFile). WithRequiredPermission(rfP). Handle(mf.RemoveFile) }) diff --git a/server/internal/machine/router/machine_script.go b/server/internal/machine/router/machine_script.go index fea8d226..efa0c10b 100644 --- a/server/internal/machine/router/machine_script.go +++ b/server/internal/machine/router/machine_script.go @@ -4,7 +4,7 @@ import ( "mayfly-go/internal/machine/api" "mayfly-go/internal/machine/application" tagapp "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -20,32 +20,32 @@ func InitMachineScriptRouter(router *gin.RouterGroup) { // 获取指定机器脚本列表 machines.GET(":machineId/scripts", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(ms.MachineScripts) + req.NewCtxWithGin(c).Handle(ms.MachineScripts) }) - saveMachienScriptLog := ctx.NewLogInfo("机器-保存脚本").WithSave(true) - smsP := ctx.NewPermission("machine:script:save") + saveMachienScriptLog := req.NewLogInfo("机器-保存脚本").WithSave(true) + smsP := req.NewPermission("machine:script:save") // 保存脚本 machines.POST(":machineId/scripts", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(saveMachienScriptLog). + req.NewCtxWithGin(c).WithLog(saveMachienScriptLog). WithRequiredPermission(smsP). Handle(ms.SaveMachineScript) }) - deleteLog := ctx.NewLogInfo("机器-删除脚本").WithSave(true) - dP := ctx.NewPermission("machine:script:del") + deleteLog := req.NewLogInfo("机器-删除脚本").WithSave(true) + dP := req.NewPermission("machine:script:del") // 保存脚本 machines.DELETE(":machineId/scripts/:scriptId", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(deleteLog). + req.NewCtxWithGin(c).WithLog(deleteLog). WithRequiredPermission(dP). Handle(ms.DeleteMachineScript) }) - runLog := ctx.NewLogInfo("机器-执行脚本").WithSave(true) - rP := ctx.NewPermission("machine:script:run") + runLog := req.NewLogInfo("机器-执行脚本").WithSave(true) + rP := req.NewPermission("machine:script:run") // 运行脚本 machines.GET(":machineId/scripts/:scriptId/run", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(runLog). + req.NewCtxWithGin(c).WithLog(runLog). WithRequiredPermission(rP). Handle(ms.RunMachineScript) }) diff --git a/server/internal/mongo/api/mongo.go b/server/internal/mongo/api/mongo.go index a48909e2..3ee247e4 100644 --- a/server/internal/mongo/api/mongo.go +++ b/server/internal/mongo/api/mongo.go @@ -7,9 +7,9 @@ import ( "mayfly-go/internal/mongo/domain/entity" tagapp "mayfly-go/internal/tag/application" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" "mayfly-go/pkg/model" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "regexp" "strconv" @@ -25,7 +25,7 @@ type Mongo struct { TagApp tagapp.TagTree } -func (m *Mongo) Mongos(rc *ctx.ReqCtx) { +func (m *Mongo) Mongos(rc *req.Ctx) { condition := new(entity.MongoQuery) condition.TagPathLike = rc.GinCtx.Query("tagPath") @@ -39,7 +39,7 @@ func (m *Mongo) Mongos(rc *ctx.ReqCtx) { rc.ResData = m.MongoApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]entity.Mongo)) } -func (m *Mongo) Save(rc *ctx.ReqCtx) { +func (m *Mongo) Save(rc *req.Ctx) { form := &form.Mongo{} ginx.BindJsonAndValid(rc.GinCtx, form) @@ -57,18 +57,18 @@ func (m *Mongo) Save(rc *ctx.ReqCtx) { m.MongoApp.Save(mongo) } -func (m *Mongo) DeleteMongo(rc *ctx.ReqCtx) { +func (m *Mongo) DeleteMongo(rc *req.Ctx) { m.MongoApp.Delete(m.GetMongoId(rc.GinCtx)) } -func (m *Mongo) Databases(rc *ctx.ReqCtx) { +func (m *Mongo) Databases(rc *req.Ctx) { cli := m.MongoApp.GetMongoCli(m.GetMongoId(rc.GinCtx)) res, err := cli.ListDatabases(context.TODO(), bson.D{}) biz.ErrIsNilAppendErr(err, "获取mongo所有库信息失败: %s") rc.ResData = res } -func (m *Mongo) Collections(rc *ctx.ReqCtx) { +func (m *Mongo) Collections(rc *req.Ctx) { cli := m.MongoApp.GetMongoCli(m.GetMongoId(rc.GinCtx)) db := rc.GinCtx.Query("database") biz.NotEmpty(db, "database不能为空") @@ -78,7 +78,7 @@ func (m *Mongo) Collections(rc *ctx.ReqCtx) { rc.ResData = res } -func (m *Mongo) RunCommand(rc *ctx.ReqCtx) { +func (m *Mongo) RunCommand(rc *req.Ctx) { commandForm := new(form.MongoRunCommand) ginx.BindJsonAndValid(rc.GinCtx, commandForm) cli := m.MongoApp.GetMongoCli(m.GetMongoId(rc.GinCtx)) @@ -94,7 +94,7 @@ func (m *Mongo) RunCommand(rc *ctx.ReqCtx) { rc.ResData = bm } -func (m *Mongo) FindCommand(rc *ctx.ReqCtx) { +func (m *Mongo) FindCommand(rc *req.Ctx) { g := rc.GinCtx cli := m.MongoApp.GetMongoCli(m.GetMongoId(g)) commandForm := new(form.MongoFindCommand) @@ -127,7 +127,7 @@ func (m *Mongo) FindCommand(rc *ctx.ReqCtx) { rc.ResData = res } -func (m *Mongo) UpdateByIdCommand(rc *ctx.ReqCtx) { +func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) { g := rc.GinCtx cli := m.MongoApp.GetMongoCli(m.GetMongoId(g)) commandForm := new(form.MongoUpdateByIdCommand) @@ -150,7 +150,7 @@ func (m *Mongo) UpdateByIdCommand(rc *ctx.ReqCtx) { rc.ResData = res } -func (m *Mongo) DeleteByIdCommand(rc *ctx.ReqCtx) { +func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) { g := rc.GinCtx cli := m.MongoApp.GetMongoCli(m.GetMongoId(g)) commandForm := new(form.MongoUpdateByIdCommand) @@ -173,7 +173,7 @@ func (m *Mongo) DeleteByIdCommand(rc *ctx.ReqCtx) { rc.ResData = res } -func (m *Mongo) InsertOneCommand(rc *ctx.ReqCtx) { +func (m *Mongo) InsertOneCommand(rc *req.Ctx) { g := rc.GinCtx cli := m.MongoApp.GetMongoCli(m.GetMongoId(g)) commandForm := new(form.MongoInsertCommand) diff --git a/server/internal/mongo/router/mongo.go b/server/internal/mongo/router/mongo.go index 98c6496f..2ef9b50c 100644 --- a/server/internal/mongo/router/mongo.go +++ b/server/internal/mongo/router/mongo.go @@ -4,7 +4,7 @@ import ( "mayfly-go/internal/mongo/api" "mayfly-go/internal/mongo/application" tagapp "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -19,68 +19,68 @@ func InitMongoRouter(router *gin.RouterGroup) { // 获取所有mongo列表 m.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). Handle(ma.Mongos) }) - saveMongo := ctx.NewLogInfo("mongo-保存信息") + saveMongo := req.NewLogInfo("mongo-保存信息") m.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(saveMongo). Handle(ma.Save) }) - deleteMongo := ctx.NewLogInfo("mongo-删除信息") + deleteMongo := req.NewLogInfo("mongo-删除信息") m.DELETE(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(deleteMongo). Handle(ma.DeleteMongo) }) // 获取mongo下的所有数据库 m.GET(":id/databases", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). Handle(ma.Databases) }) // 获取mongo指定库的所有集合 m.GET(":id/collections", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). Handle(ma.Collections) }) // 获取mongo runCommand m.POST(":id/run-command", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). Handle(ma.RunCommand) }) // 执行mongo find命令 m.POST(":id/command/find", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). Handle(ma.FindCommand) }) // 执行mongo update by id命令 - updateDocById := ctx.NewLogInfo("mongo-更新文档").WithSave(true) + updateDocById := req.NewLogInfo("mongo-更新文档").WithSave(true) m.POST(":id/command/update-by-id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(updateDocById). Handle(ma.UpdateByIdCommand) }) // 执行mongo delete by id命令 - deleteDoc := ctx.NewLogInfo("mongo-删除文档").WithSave(true) + deleteDoc := req.NewLogInfo("mongo-删除文档").WithSave(true) m.POST(":id/command/delete-by-id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(deleteDoc). Handle(ma.DeleteByIdCommand) }) // 执行mongo insert 命令 - insertDoc := ctx.NewLogInfo("mongo-新增文档").WithSave(true) + insertDoc := req.NewLogInfo("mongo-新增文档").WithSave(true) m.POST(":id/command/insert", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(insertDoc). Handle(ma.InsertOneCommand) }) diff --git a/server/internal/redis/api/redis.go b/server/internal/redis/api/redis.go index 0909a612..1d64f1b4 100644 --- a/server/internal/redis/api/redis.go +++ b/server/internal/redis/api/redis.go @@ -9,9 +9,9 @@ import ( "mayfly-go/internal/redis/domain/entity" tagapp "mayfly-go/internal/tag/application" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" "mayfly-go/pkg/model" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "strconv" "strings" @@ -26,7 +26,7 @@ type Redis struct { TagApp tagapp.TagTree } -func (r *Redis) RedisList(rc *ctx.ReqCtx) { +func (r *Redis) RedisList(rc *req.Ctx) { condition := new(entity.RedisQuery) condition.TagPathLike = rc.GinCtx.Query("tagPath") @@ -40,7 +40,7 @@ func (r *Redis) RedisList(rc *ctx.ReqCtx) { rc.ResData = r.RedisApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.Redis)) } -func (r *Redis) Save(rc *ctx.ReqCtx) { +func (r *Redis) Save(rc *req.Ctx) { form := &form.Redis{} ginx.BindJsonAndValid(rc.GinCtx, form) @@ -61,18 +61,18 @@ func (r *Redis) Save(rc *ctx.ReqCtx) { } // 获取redis实例密码,由于数据库是加密存储,故提供该接口展示原文密码 -func (r *Redis) GetRedisPwd(rc *ctx.ReqCtx) { +func (r *Redis) GetRedisPwd(rc *req.Ctx) { rid := uint64(ginx.PathParamInt(rc.GinCtx, "id")) re := r.RedisApp.GetById(rid, "Password") re.PwdDecrypt() rc.ResData = re.Password } -func (r *Redis) DeleteRedis(rc *ctx.ReqCtx) { +func (r *Redis) DeleteRedis(rc *req.Ctx) { r.RedisApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } -func (r *Redis) RedisInfo(rc *ctx.ReqCtx) { +func (r *Redis) RedisInfo(rc *req.Ctx) { g := rc.GinCtx ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")), 0) @@ -144,7 +144,7 @@ func (r *Redis) RedisInfo(rc *ctx.ReqCtx) { rc.ResData = parseMap } -func (r *Redis) ClusterInfo(rc *ctx.ReqCtx) { +func (r *Redis) ClusterInfo(rc *req.Ctx) { g := rc.GinCtx ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")), 0) biz.IsEquals(ri.Info.Mode, entity.RedisModeCluster, "非集群模式") @@ -189,7 +189,7 @@ func (r *Redis) ClusterInfo(rc *ctx.ReqCtx) { } // scan获取redis的key列表信息 -func (r *Redis) Scan(rc *ctx.ReqCtx) { +func (r *Redis) Scan(rc *req.Ctx) { g := rc.GinCtx ri := r.RedisApp.GetRedisInstance(uint64(ginx.PathParamInt(g, "id")), ginx.PathParamInt(g, "db")) biz.ErrIsNilAppendErr(r.TagApp.CanAccess(rc.LoginAccount.Id, ri.Info.TagPath), "%s") @@ -318,7 +318,7 @@ func (r *Redis) Scan(rc *ctx.ReqCtx) { rc.ResData = &vo.Keys{Cursor: cursorRes, Keys: kis, DbSize: size} } -func (r *Redis) DeleteKey(rc *ctx.ReqCtx) { +func (r *Redis) DeleteKey(rc *req.Ctx) { g := rc.GinCtx key := g.Query("key") biz.NotEmpty(key, "key不能为空") @@ -330,7 +330,7 @@ func (r *Redis) DeleteKey(rc *ctx.ReqCtx) { ri.GetCmdable().Del(context.Background(), key) } -func (r *Redis) checkKey(rc *ctx.ReqCtx) (*application.RedisInstance, string) { +func (r *Redis) checkKey(rc *req.Ctx) (*application.RedisInstance, string) { g := rc.GinCtx key := g.Query("key") biz.NotEmpty(key, "key不能为空") @@ -341,14 +341,14 @@ func (r *Redis) checkKey(rc *ctx.ReqCtx) (*application.RedisInstance, string) { return ri, key } -func (r *Redis) GetStringValue(rc *ctx.ReqCtx) { +func (r *Redis) GetStringValue(rc *req.Ctx) { ri, key := r.checkKey(rc) str, err := ri.GetCmdable().Get(context.TODO(), key).Result() biz.ErrIsNilAppendErr(err, "获取字符串值失败: %s") rc.ResData = str } -func (r *Redis) SetStringValue(rc *ctx.ReqCtx) { +func (r *Redis) SetStringValue(rc *req.Ctx) { g := rc.GinCtx keyValue := new(form.StringValue) ginx.BindJsonAndValid(g, keyValue) @@ -363,7 +363,7 @@ func (r *Redis) SetStringValue(rc *ctx.ReqCtx) { rc.ResData = str } -func (r *Redis) Hscan(rc *ctx.ReqCtx) { +func (r *Redis) Hscan(rc *req.Ctx) { ri, key := r.checkKey(rc) g := rc.GinCtx count := ginx.QueryInt(g, "count", 10) @@ -384,7 +384,7 @@ func (r *Redis) Hscan(rc *ctx.ReqCtx) { } } -func (r *Redis) Hdel(rc *ctx.ReqCtx) { +func (r *Redis) Hdel(rc *req.Ctx) { ri, key := r.checkKey(rc) field := rc.GinCtx.Query("field") @@ -393,7 +393,7 @@ func (r *Redis) Hdel(rc *ctx.ReqCtx) { rc.ResData = delRes } -func (r *Redis) Hget(rc *ctx.ReqCtx) { +func (r *Redis) Hget(rc *req.Ctx) { ri, key := r.checkKey(rc) field := rc.GinCtx.Query("field") @@ -402,7 +402,7 @@ func (r *Redis) Hget(rc *ctx.ReqCtx) { rc.ResData = res } -func (r *Redis) SetHashValue(rc *ctx.ReqCtx) { +func (r *Redis) SetHashValue(rc *req.Ctx) { g := rc.GinCtx hashValue := new(form.HashValue) ginx.BindJsonAndValid(g, hashValue) @@ -422,14 +422,14 @@ func (r *Redis) SetHashValue(rc *ctx.ReqCtx) { } } -func (r *Redis) GetSetValue(rc *ctx.ReqCtx) { +func (r *Redis) GetSetValue(rc *req.Ctx) { ri, key := r.checkKey(rc) res, err := ri.GetCmdable().SMembers(context.TODO(), key).Result() biz.ErrIsNilAppendErr(err, "获取set值失败: %s") rc.ResData = res } -func (r *Redis) SetSetValue(rc *ctx.ReqCtx) { +func (r *Redis) SetSetValue(rc *req.Ctx) { g := rc.GinCtx keyvalue := new(form.SetValue) ginx.BindJsonAndValid(g, keyvalue) @@ -448,7 +448,7 @@ func (r *Redis) SetSetValue(rc *ctx.ReqCtx) { } } -func (r *Redis) GetListValue(rc *ctx.ReqCtx) { +func (r *Redis) GetListValue(rc *req.Ctx) { ri, key := r.checkKey(rc) ctx := context.TODO() cmdable := ri.GetCmdable() @@ -468,7 +468,7 @@ func (r *Redis) GetListValue(rc *ctx.ReqCtx) { } } -func (r *Redis) SaveListValue(rc *ctx.ReqCtx) { +func (r *Redis) SaveListValue(rc *req.Ctx) { g := rc.GinCtx listValue := new(form.ListValue) ginx.BindJsonAndValid(g, listValue) @@ -488,7 +488,7 @@ func (r *Redis) SaveListValue(rc *ctx.ReqCtx) { } } -func (r *Redis) SetListValue(rc *ctx.ReqCtx) { +func (r *Redis) SetListValue(rc *req.Ctx) { g := rc.GinCtx listSetValue := new(form.ListSetValue) ginx.BindJsonAndValid(g, listSetValue) diff --git a/server/internal/redis/router/redis.go b/server/internal/redis/router/redis.go index 1c1ef650..60c3a4e1 100644 --- a/server/internal/redis/router/redis.go +++ b/server/internal/redis/router/redis.go @@ -4,7 +4,7 @@ import ( "mayfly-go/internal/redis/api" "mayfly-go/internal/redis/application" tagapp "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -19,94 +19,94 @@ func InitRedisRouter(router *gin.RouterGroup) { // 获取redis list redis.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.RedisList) + req.NewCtxWithGin(c).Handle(rs.RedisList) }) - save := ctx.NewLogInfo("redis-保存信息").WithSave(true) + save := req.NewLogInfo("redis-保存信息").WithSave(true) redis.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(save).Handle(rs.Save) + req.NewCtxWithGin(c).WithLog(save).Handle(rs.Save) }) redis.GET(":id/pwd", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.GetRedisPwd) + req.NewCtxWithGin(c).Handle(rs.GetRedisPwd) }) - delRedis := ctx.NewLogInfo("redis-删除信息").WithSave(true) + delRedis := req.NewLogInfo("redis-删除信息").WithSave(true) redis.DELETE(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(delRedis).Handle(rs.DeleteRedis) + req.NewCtxWithGin(c).WithLog(delRedis).Handle(rs.DeleteRedis) }) redis.GET(":id/info", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.RedisInfo) + req.NewCtxWithGin(c).Handle(rs.RedisInfo) }) redis.GET(":id/cluster-info", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.ClusterInfo) + req.NewCtxWithGin(c).Handle(rs.ClusterInfo) }) // 获取指定redis keys redis.POST(":id/:db/scan", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.Scan) + req.NewCtxWithGin(c).Handle(rs.Scan) }) // 删除key - deleteKeyL := ctx.NewLogInfo("redis-删除key").WithSave(true) + deleteKeyL := req.NewLogInfo("redis-删除key").WithSave(true) redis.DELETE(":id/:db/key", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(deleteKeyL).Handle(rs.DeleteKey) + req.NewCtxWithGin(c).WithLog(deleteKeyL).Handle(rs.DeleteKey) }) // 获取string类型值 redis.GET(":id/:db/string-value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.GetStringValue) + req.NewCtxWithGin(c).Handle(rs.GetStringValue) }) // 设置string类型值 - setStringL := ctx.NewLogInfo("redis-setString").WithSave(true) + setStringL := req.NewLogInfo("redis-setString").WithSave(true) redis.POST(":id/:db/string-value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(setStringL).Handle(rs.SetStringValue) + req.NewCtxWithGin(c).WithLog(setStringL).Handle(rs.SetStringValue) }) // hscan redis.GET(":id/:db/hscan", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.Hscan) + req.NewCtxWithGin(c).Handle(rs.Hscan) }) redis.GET(":id/:db/hget", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.Hget) + req.NewCtxWithGin(c).Handle(rs.Hget) }) - hdelL := ctx.NewLogInfo("redis-hdel").WithSave(true) + hdelL := req.NewLogInfo("redis-hdel").WithSave(true) redis.DELETE(":id/:db/hdel", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(hdelL).Handle(rs.Hdel) + req.NewCtxWithGin(c).WithLog(hdelL).Handle(rs.Hdel) }) // 设置hash类型值 - setHashValueL := ctx.NewLogInfo("redis-setHashValue").WithSave(true) + setHashValueL := req.NewLogInfo("redis-setHashValue").WithSave(true) redis.POST(":id/:db/hash-value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(setHashValueL).Handle(rs.SetHashValue) + req.NewCtxWithGin(c).WithLog(setHashValueL).Handle(rs.SetHashValue) }) // 获取set类型值 redis.GET(":id/:db/set-value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.GetSetValue) + req.NewCtxWithGin(c).Handle(rs.GetSetValue) }) // 设置set类型值 redis.POST(":id/:db/set-value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.SetSetValue) + req.NewCtxWithGin(c).Handle(rs.SetSetValue) }) // 获取list类型值 redis.GET(":id/:db/list-value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.GetListValue) + req.NewCtxWithGin(c).Handle(rs.GetListValue) }) redis.POST(":id/:db/list-value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.SaveListValue) + req.NewCtxWithGin(c).Handle(rs.SaveListValue) }) redis.POST(":id/:db/list-value/lset", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(rs.SetListValue) + req.NewCtxWithGin(c).Handle(rs.SetListValue) }) } } diff --git a/server/internal/sys/api/account.go b/server/internal/sys/api/account.go index 8e85d6ea..cae7b8bb 100644 --- a/server/internal/sys/api/account.go +++ b/server/internal/sys/api/account.go @@ -8,9 +8,9 @@ import ( "mayfly-go/internal/sys/domain/entity" "mayfly-go/pkg/biz" "mayfly-go/pkg/captcha" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" "mayfly-go/pkg/model" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "regexp" "strconv" @@ -29,7 +29,7 @@ type Account struct { /** 登录者个人相关操作 **/ // @router /accounts/login [post] -func (a *Account) Login(rc *ctx.ReqCtx) { +func (a *Account) Login(rc *req.Ctx) { loginForm := &form.LoginForm{} ginx.BindJsonAndValid(rc.GinCtx, loginForm) @@ -65,7 +65,7 @@ func (a *Account) Login(rc *ctx.ReqCtx) { } } // 保存该账号的权限codes - ctx.SavePermissionCodes(account.Id, permissions) + req.SavePermissionCodes(account.Id, permissions) clientIp := rc.GinCtx.ClientIP() // 保存登录消息 @@ -76,7 +76,7 @@ func (a *Account) Login(rc *ctx.ReqCtx) { rc.LoginAccount = &model.LoginAccount{Id: account.Id, Username: account.Username} rc.ResData = map[string]interface{}{ - "token": ctx.CreateToken(account.Id, account.Username), + "token": req.CreateToken(account.Id, account.Username), "name": account.Name, "username": account.Username, "lastLoginTime": account.LastLoginTime, @@ -86,7 +86,7 @@ func (a *Account) Login(rc *ctx.ReqCtx) { } } -func (a *Account) ChangePassword(rc *ctx.ReqCtx) { +func (a *Account) ChangePassword(rc *req.Ctx) { form := new(form.AccountChangePasswordForm) ginx.BindJsonAndValid(rc.GinCtx, form) @@ -153,7 +153,7 @@ func (a *Account) saveLogin(account *entity.Account, ip string) { } // 获取个人账号信息 -func (a *Account) AccountInfo(rc *ctx.ReqCtx) { +func (a *Account) AccountInfo(rc *req.Ctx) { ap := new(vo.AccountPersonVO) // 角色信息 roles := new([]vo.AccountRoleVO) @@ -164,7 +164,7 @@ func (a *Account) AccountInfo(rc *ctx.ReqCtx) { } // 更新个人账号信息 -func (a *Account) UpdateAccount(rc *ctx.ReqCtx) { +func (a *Account) UpdateAccount(rc *req.Ctx) { updateForm := &form.AccountUpdateForm{} ginx.BindJsonAndValid(rc.GinCtx, updateForm) @@ -181,7 +181,7 @@ func (a *Account) UpdateAccount(rc *ctx.ReqCtx) { } // 获取账号接收的消息列表 -func (a *Account) GetMsgs(rc *ctx.ReqCtx) { +func (a *Account) GetMsgs(rc *req.Ctx) { condition := &entity.Msg{ RecipientId: int64(rc.LoginAccount.Id), } @@ -191,14 +191,14 @@ func (a *Account) GetMsgs(rc *ctx.ReqCtx) { /** 后台账号操作 **/ // @router /accounts [get] -func (a *Account) Accounts(rc *ctx.ReqCtx) { +func (a *Account) Accounts(rc *req.Ctx) { condition := &entity.Account{} condition.Username = rc.GinCtx.Query("username") rc.ResData = a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO)) } // @router /accounts -func (a *Account) SaveAccount(rc *ctx.ReqCtx) { +func (a *Account) SaveAccount(rc *req.Ctx) { form := &form.AccountCreateForm{} ginx.BindJsonAndValid(rc.GinCtx, form) rc.ReqParam = form @@ -218,7 +218,7 @@ func (a *Account) SaveAccount(rc *ctx.ReqCtx) { } } -func (a *Account) ChangeStatus(rc *ctx.ReqCtx) { +func (a *Account) ChangeStatus(rc *req.Ctx) { g := rc.GinCtx account := &entity.Account{} @@ -228,25 +228,25 @@ func (a *Account) ChangeStatus(rc *ctx.ReqCtx) { a.AccountApp.Update(account) } -func (a *Account) DeleteAccount(rc *ctx.ReqCtx) { +func (a *Account) DeleteAccount(rc *req.Ctx) { id := uint64(ginx.PathParamInt(rc.GinCtx, "id")) rc.ReqParam = id a.AccountApp.Delete(id) } // 获取账号角色id列表,用户回显角色分配 -func (a *Account) AccountRoleIds(rc *ctx.ReqCtx) { +func (a *Account) AccountRoleIds(rc *req.Ctx) { rc.ResData = a.RoleApp.GetAccountRoleIds(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } // 获取账号角色id列表,用户回显角色分配 -func (a *Account) AccountRoles(rc *ctx.ReqCtx) { +func (a *Account) AccountRoles(rc *req.Ctx) { vos := new([]vo.AccountRoleVO) a.RoleApp.GetAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")), vos) rc.ResData = vos } -func (a *Account) AccountResources(rc *ctx.ReqCtx) { +func (a *Account) AccountResources(rc *req.Ctx) { var resources vo.ResourceManageVOList // 获取账号菜单资源 a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources) @@ -254,7 +254,7 @@ func (a *Account) AccountResources(rc *ctx.ReqCtx) { } // 保存账号角色信息 -func (a *Account) SaveRoles(rc *ctx.ReqCtx) { +func (a *Account) SaveRoles(rc *req.Ctx) { g := rc.GinCtx var form form.AccountRoleForm diff --git a/server/internal/sys/api/captcha.go b/server/internal/sys/api/captcha.go index 9ab9a8e1..7f164bfd 100644 --- a/server/internal/sys/api/captcha.go +++ b/server/internal/sys/api/captcha.go @@ -2,10 +2,10 @@ package api import ( "mayfly-go/pkg/captcha" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" ) -func GenerateCaptcha(rc *ctx.ReqCtx) { +func GenerateCaptcha(rc *req.Ctx) { id, image := captcha.Generate() rc.ResData = map[string]interface{}{"base64Captcha": image, "cid": id} } diff --git a/server/internal/sys/api/config.go b/server/internal/sys/api/config.go index e028590b..248f0427 100644 --- a/server/internal/sys/api/config.go +++ b/server/internal/sys/api/config.go @@ -5,8 +5,8 @@ import ( "mayfly-go/internal/sys/application" "mayfly-go/internal/sys/domain/entity" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" ) @@ -14,19 +14,19 @@ type Config struct { ConfigApp application.Config } -func (c *Config) Configs(rc *ctx.ReqCtx) { +func (c *Config) Configs(rc *req.Ctx) { g := rc.GinCtx condition := &entity.Config{Key: g.Query("key")} rc.ResData = c.ConfigApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Config)) } -func (c *Config) GetConfigValueByKey(rc *ctx.ReqCtx) { +func (c *Config) GetConfigValueByKey(rc *req.Ctx) { key := rc.GinCtx.Query("key") biz.NotEmpty(key, "key不能为空") rc.ResData = c.ConfigApp.GetConfig(key).Value } -func (c *Config) SaveConfig(rc *ctx.ReqCtx) { +func (c *Config) SaveConfig(rc *req.Ctx) { g := rc.GinCtx form := &form.ConfigForm{} ginx.BindJsonAndValid(g, form) diff --git a/server/internal/sys/api/resource.go b/server/internal/sys/api/resource.go index d601d966..7f924b5c 100644 --- a/server/internal/sys/api/resource.go +++ b/server/internal/sys/api/resource.go @@ -6,8 +6,8 @@ import ( "mayfly-go/internal/sys/api/vo" "mayfly-go/internal/sys/application" "mayfly-go/internal/sys/domain/entity" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" ) @@ -15,17 +15,17 @@ type Resource struct { ResourceApp application.Resource } -func (r *Resource) GetAllResourceTree(rc *ctx.ReqCtx) { +func (r *Resource) GetAllResourceTree(rc *req.Ctx) { var resources vo.ResourceManageVOList r.ResourceApp.GetResourceList(new(entity.Resource), &resources, "weight asc") rc.ResData = resources.ToTrees(0) } -func (r *Resource) GetById(rc *ctx.ReqCtx) { +func (r *Resource) GetById(rc *req.Ctx) { rc.ResData = r.ResourceApp.GetById(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } -func (r *Resource) SaveResource(rc *ctx.ReqCtx) { +func (r *Resource) SaveResource(rc *req.Ctx) { g := rc.GinCtx form := new(form.ResourceForm) ginx.BindJsonAndValid(g, form) @@ -41,11 +41,11 @@ func (r *Resource) SaveResource(rc *ctx.ReqCtx) { r.ResourceApp.Save(entity) } -func (r *Resource) DelResource(rc *ctx.ReqCtx) { +func (r *Resource) DelResource(rc *req.Ctx) { r.ResourceApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } -func (r *Resource) ChangeStatus(rc *ctx.ReqCtx) { +func (r *Resource) ChangeStatus(rc *req.Ctx) { re := &entity.Resource{} re.Id = uint64(ginx.PathParamInt(rc.GinCtx, "id")) re.Status = int8(ginx.PathParamInt(rc.GinCtx, "status")) diff --git a/server/internal/sys/api/role.go b/server/internal/sys/api/role.go index 4708244f..7ec79d35 100644 --- a/server/internal/sys/api/role.go +++ b/server/internal/sys/api/role.go @@ -5,8 +5,8 @@ import ( "mayfly-go/internal/sys/api/vo" "mayfly-go/internal/sys/application" "mayfly-go/internal/sys/domain/entity" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "strconv" "strings" @@ -18,14 +18,14 @@ type Role struct { ResourceApp application.Resource } -func (r *Role) Roles(rc *ctx.ReqCtx) { +func (r *Role) Roles(rc *req.Ctx) { g := rc.GinCtx condition := &entity.Role{} rc.ResData = r.RoleApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Role)) } // 保存角色信息 -func (r *Role) SaveRole(rc *ctx.ReqCtx) { +func (r *Role) SaveRole(rc *req.Ctx) { g := rc.GinCtx form := &form.RoleForm{} ginx.BindJsonAndValid(g, form) @@ -39,17 +39,17 @@ func (r *Role) SaveRole(rc *ctx.ReqCtx) { } // 删除角色及其资源关联关系 -func (r *Role) DelRole(rc *ctx.ReqCtx) { +func (r *Role) DelRole(rc *req.Ctx) { r.RoleApp.DeleteRole(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } // 获取角色关联的资源id数组,用于分配资源时回显已拥有的资源 -func (r *Role) RoleResourceIds(rc *ctx.ReqCtx) { +func (r *Role) RoleResourceIds(rc *req.Ctx) { rc.ResData = r.RoleApp.GetRoleResourceIds(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } // 查看角色关联的资源树信息 -func (r *Role) RoleResource(rc *ctx.ReqCtx) { +func (r *Role) RoleResource(rc *req.Ctx) { g := rc.GinCtx var resources vo.ResourceManageVOList @@ -59,7 +59,7 @@ func (r *Role) RoleResource(rc *ctx.ReqCtx) { } // 保存角色资源 -func (r *Role) SaveResource(rc *ctx.ReqCtx) { +func (r *Role) SaveResource(rc *req.Ctx) { g := rc.GinCtx var form form.RoleResourceForm diff --git a/server/internal/sys/api/syslog.go b/server/internal/sys/api/syslog.go index 832bcd93..3bb634ad 100644 --- a/server/internal/sys/api/syslog.go +++ b/server/internal/sys/api/syslog.go @@ -3,15 +3,15 @@ package api import ( "mayfly-go/internal/sys/application" "mayfly-go/internal/sys/domain/entity" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" ) type Syslog struct { SyslogApp application.Syslog } -func (r *Syslog) Syslogs(rc *ctx.ReqCtx) { +func (r *Syslog) Syslogs(rc *req.Ctx) { g := rc.GinCtx condition := &entity.Syslog{ Type: int8(ginx.QueryInt(g, "type", 0)), diff --git a/server/internal/sys/api/system.go b/server/internal/sys/api/system.go index ec3cdb38..ce721c01 100644 --- a/server/internal/sys/api/system.go +++ b/server/internal/sys/api/system.go @@ -2,8 +2,8 @@ package api import ( "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/global" + "mayfly-go/pkg/req" "mayfly-go/pkg/ws" "github.com/gin-gonic/gin" @@ -30,8 +30,8 @@ func (s *System) ConnectWs(g *gin.Context) { panic(biz.NewBizErr("升级websocket失败")) } // 权限校验 - rc := ctx.NewReqCtxWithGin(g) - if err = ctx.PermissionHandler(rc); err != nil { + rc := req.NewCtxWithGin(g) + if err = req.PermissionHandler(rc); err != nil { panic(biz.NewBizErr("没有权限")) } diff --git a/server/internal/sys/application/syslog_app.go b/server/internal/sys/application/syslog_app.go index d7f47b50..2daffe8c 100644 --- a/server/internal/sys/application/syslog_app.go +++ b/server/internal/sys/application/syslog_app.go @@ -6,8 +6,8 @@ import ( "mayfly-go/internal/sys/domain/entity" "mayfly-go/internal/sys/domain/repository" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/model" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" "reflect" "time" @@ -17,7 +17,7 @@ type Syslog interface { GetPageList(condition *entity.Syslog, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult // 从请求上下文的参数保存系统日志 - SaveFromReq(req *ctx.ReqCtx) + SaveFromReq(req *req.Ctx) } func newSyslogApp(syslogRepo repository.Syslog) Syslog { @@ -34,7 +34,7 @@ func (m *syslogAppImpl) GetPageList(condition *entity.Syslog, pageParam *model.P return m.syslogRepo.GetPageList(condition, pageParam, toEntity, orderBy...) } -func (m *syslogAppImpl) SaveFromReq(req *ctx.ReqCtx) { +func (m *syslogAppImpl) SaveFromReq(req *req.Ctx) { lg := req.LoginAccount if lg == nil { return diff --git a/server/internal/sys/router/account.go b/server/internal/sys/router/account.go index a07ddb31..46b575d9 100644 --- a/server/internal/sys/router/account.go +++ b/server/internal/sys/router/account.go @@ -3,7 +3,7 @@ package router import ( "mayfly-go/internal/sys/api" "mayfly-go/internal/sys/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -19,17 +19,17 @@ func InitAccountRouter(router *gin.RouterGroup) { } { // 用户登录 - loginLog := ctx.NewLogInfo("用户登录").WithSave(true) + loginLog := req.NewLogInfo("用户登录").WithSave(true) account.POST("login", func(g *gin.Context) { - ctx.NewReqCtxWithGin(g). + req.NewCtxWithGin(g). WithNeedToken(false). WithLog(loginLog). Handle(a.Login) }) - changePwdLog := ctx.NewLogInfo("用户修改密码").WithSave(true) + changePwdLog := req.NewLogInfo("用户修改密码").WithSave(true) account.POST("change-pwd", func(g *gin.Context) { - ctx.NewReqCtxWithGin(g). + req.NewCtxWithGin(g). WithNeedToken(false). WithLog(changePwdLog). Handle(a.ChangePassword) @@ -37,45 +37,45 @@ func InitAccountRouter(router *gin.RouterGroup) { // 获取个人账号信息 account.GET("/self", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(a.AccountInfo) + req.NewCtxWithGin(c).Handle(a.AccountInfo) }) // 更新个人账号信息 account.PUT("/self", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(a.UpdateAccount) + req.NewCtxWithGin(c).Handle(a.UpdateAccount) }) account.GET("/msgs", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(a.GetMsgs) + req.NewCtxWithGin(c).Handle(a.GetMsgs) }) /** 后台管理接口 **/ // 获取所有用户列表 account.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(a.Accounts) + req.NewCtxWithGin(c).Handle(a.Accounts) }) - createAccount := ctx.NewLogInfo("保存账号信息").WithSave(true) - addAccountPermission := ctx.NewPermission("account:add") + createAccount := req.NewLogInfo("保存账号信息").WithSave(true) + addAccountPermission := req.NewPermission("account:add") account.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithRequiredPermission(addAccountPermission). WithLog(createAccount). Handle(a.SaveAccount) }) - changeStatus := ctx.NewLogInfo("修改账号状态").WithSave(true) + changeStatus := req.NewLogInfo("修改账号状态").WithSave(true) account.PUT("change-status/:id/:status", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(changeStatus). Handle(a.ChangeStatus) }) - delAccount := ctx.NewLogInfo("删除账号").WithSave(true) - delAccountPermission := ctx.NewPermission("account:del") + delAccount := req.NewLogInfo("删除账号").WithSave(true) + delAccountPermission := req.NewPermission("account:del") account.DELETE(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithRequiredPermission(delAccountPermission). WithLog(delAccount). Handle(a.DeleteAccount) @@ -83,26 +83,26 @@ func InitAccountRouter(router *gin.RouterGroup) { // 获取所有用户角色id列表 account.GET(":id/roleIds", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(a.AccountRoleIds) + req.NewCtxWithGin(c).Handle(a.AccountRoleIds) }) // 保存用户角色 - saveAccountRole := ctx.NewLogInfo("保存用户角色").WithSave(true) - sarPermission := ctx.NewPermission("account:saveRoles") + saveAccountRole := req.NewLogInfo("保存用户角色").WithSave(true) + sarPermission := req.NewPermission("account:saveRoles") account.POST("/roles", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(saveAccountRole). + req.NewCtxWithGin(c).WithLog(saveAccountRole). WithRequiredPermission(sarPermission). Handle(a.SaveRoles) }) // 获取用户角色 account.GET(":id/roles", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(a.AccountRoles) + req.NewCtxWithGin(c).Handle(a.AccountRoles) }) // 获取用户资源列表 account.GET(":id/resources", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(a.AccountResources) + req.NewCtxWithGin(c).Handle(a.AccountResources) }) } } diff --git a/server/internal/sys/router/captcha.go b/server/internal/sys/router/captcha.go index a7defb74..27cd2ce6 100644 --- a/server/internal/sys/router/captcha.go +++ b/server/internal/sys/router/captcha.go @@ -2,7 +2,7 @@ package router import ( "mayfly-go/internal/sys/api" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -11,7 +11,7 @@ func InitCaptchaRouter(router *gin.RouterGroup) { captcha := router.Group("sys/captcha") { captcha.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithNeedToken(false).Handle(api.GenerateCaptcha) + req.NewCtxWithGin(c).WithNeedToken(false).Handle(api.GenerateCaptcha) }) } } diff --git a/server/internal/sys/router/config.go b/server/internal/sys/router/config.go index 7fbcd719..f9e4ce04 100644 --- a/server/internal/sys/router/config.go +++ b/server/internal/sys/router/config.go @@ -3,7 +3,7 @@ package router import ( "mayfly-go/internal/sys/api" "mayfly-go/internal/sys/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -13,16 +13,16 @@ func InitSysConfigRouter(router *gin.RouterGroup) { db := router.Group("sys/configs") { db.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(r.Configs) + req.NewCtxWithGin(c).Handle(r.Configs) }) db.GET("/value", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithNeedToken(false).Handle(r.GetConfigValueByKey) + req.NewCtxWithGin(c).WithNeedToken(false).Handle(r.GetConfigValueByKey) }) - saveConfig := ctx.NewLogInfo("保存系统配置信息").WithSave(true) + saveConfig := req.NewLogInfo("保存系统配置信息").WithSave(true) db.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(saveConfig). Handle(r.SaveConfig) }) diff --git a/server/internal/sys/router/resource.go b/server/internal/sys/router/resource.go index dd7c72c1..37e34406 100644 --- a/server/internal/sys/router/resource.go +++ b/server/internal/sys/router/resource.go @@ -3,7 +3,7 @@ package router import ( "mayfly-go/internal/sys/api" "mayfly-go/internal/sys/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -13,35 +13,35 @@ func InitResourceRouter(router *gin.RouterGroup) { db := router.Group("sys/resources") { db.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(r.GetAllResourceTree) + req.NewCtxWithGin(c).Handle(r.GetAllResourceTree) }) db.GET(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(r.GetById) + req.NewCtxWithGin(c).Handle(r.GetById) }) - saveResource := ctx.NewLogInfo("保存资源").WithSave(true) - srPermission := ctx.NewPermission("resource:add") + saveResource := req.NewLogInfo("保存资源").WithSave(true) + srPermission := req.NewPermission("resource:add") db.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(saveResource). WithRequiredPermission(srPermission). Handle(r.SaveResource) }) - changeStatus := ctx.NewLogInfo("修改资源状态").WithSave(true) - csPermission := ctx.NewPermission("resource:changeStatus") + changeStatus := req.NewLogInfo("修改资源状态").WithSave(true) + csPermission := req.NewPermission("resource:changeStatus") db.PUT(":id/:status", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(changeStatus). WithRequiredPermission(csPermission). Handle(r.ChangeStatus) }) - delResource := ctx.NewLogInfo("删除资源").WithSave(true) - dePermission := ctx.NewPermission("resource:delete") + delResource := req.NewLogInfo("删除资源").WithSave(true) + dePermission := req.NewPermission("resource:delete") db.DELETE(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(delResource). WithRequiredPermission(dePermission). Handle(r.DelResource) diff --git a/server/internal/sys/router/role.go b/server/internal/sys/router/role.go index 2a152b59..267f46d6 100644 --- a/server/internal/sys/router/role.go +++ b/server/internal/sys/router/role.go @@ -3,7 +3,7 @@ package router import ( "mayfly-go/internal/sys/api" "mayfly-go/internal/sys/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -17,37 +17,37 @@ func InitRoleRouter(router *gin.RouterGroup) { { db.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(r.Roles) + req.NewCtxWithGin(c).Handle(r.Roles) }) - saveRole := ctx.NewLogInfo("保存角色").WithSave(true) - sPermission := ctx.NewPermission("role:add") + saveRole := req.NewLogInfo("保存角色").WithSave(true) + sPermission := req.NewPermission("role:add") db.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(saveRole). + req.NewCtxWithGin(c).WithLog(saveRole). WithRequiredPermission(sPermission). Handle(r.SaveRole) }) - delRole := ctx.NewLogInfo("删除角色").WithSave(true) - drPermission := ctx.NewPermission("role:del") + delRole := req.NewLogInfo("删除角色").WithSave(true) + drPermission := req.NewPermission("role:del") db.DELETE(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(delRole). + req.NewCtxWithGin(c).WithLog(delRole). WithRequiredPermission(drPermission). Handle(r.DelRole) }) db.GET(":id/resourceIds", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(r.RoleResourceIds) + req.NewCtxWithGin(c).Handle(r.RoleResourceIds) }) db.GET(":id/resources", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(r.RoleResource) + req.NewCtxWithGin(c).Handle(r.RoleResource) }) - saveResource := ctx.NewLogInfo("保存角色资源").WithSave(true) - srPermission := ctx.NewPermission("role:saveResources") + saveResource := req.NewLogInfo("保存角色资源").WithSave(true) + srPermission := req.NewPermission("role:saveResources") db.POST(":id/resources", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(saveResource). + req.NewCtxWithGin(c).WithLog(saveResource). WithRequiredPermission(srPermission). Handle(r.SaveResource) }) diff --git a/server/internal/sys/router/syslog.go b/server/internal/sys/router/syslog.go index 42ea26f5..aa8a05bc 100644 --- a/server/internal/sys/router/syslog.go +++ b/server/internal/sys/router/syslog.go @@ -3,7 +3,7 @@ package router import ( "mayfly-go/internal/sys/api" "mayfly-go/internal/sys/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -15,7 +15,7 @@ func InitSyslogRouter(router *gin.RouterGroup) { sys := router.Group("syslogs") { sys.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(s.Syslogs) + req.NewCtxWithGin(c).Handle(s.Syslogs) }) } } diff --git a/server/internal/tag/api/tag_tree.go b/server/internal/tag/api/tag_tree.go index 14023b95..1795c879 100644 --- a/server/internal/tag/api/tag_tree.go +++ b/server/internal/tag/api/tag_tree.go @@ -5,15 +5,15 @@ import ( "mayfly-go/internal/tag/api/vo" "mayfly-go/internal/tag/application" "mayfly-go/internal/tag/domain/entity" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" ) type TagTree struct { TagTreeApp application.TagTree } -func (p *TagTree) GetAccountTags(rc *ctx.ReqCtx) { +func (p *TagTree) GetAccountTags(rc *req.Ctx) { tagPaths := p.TagTreeApp.ListTagByAccountId(rc.LoginAccount.Id) allTagPath := make([]string, 0) if len(tagPaths) > 0 { @@ -25,13 +25,13 @@ func (p *TagTree) GetAccountTags(rc *ctx.ReqCtx) { rc.ResData = allTagPath } -func (p *TagTree) GetTagTree(rc *ctx.ReqCtx) { +func (p *TagTree) GetTagTree(rc *req.Ctx) { var tagTrees vo.TagTreeVOS p.TagTreeApp.ListByQuery(new(entity.TagTreeQuery), &tagTrees) rc.ResData = tagTrees.ToTrees(0) } -func (p *TagTree) SaveTagTree(rc *ctx.ReqCtx) { +func (p *TagTree) SaveTagTree(rc *req.Ctx) { projectTree := &entity.TagTree{} ginx.BindJsonAndValid(rc.GinCtx, projectTree) @@ -42,6 +42,6 @@ func (p *TagTree) SaveTagTree(rc *ctx.ReqCtx) { rc.ReqParam = fmt.Sprintf("tagTreeId: %d, tagName: %s, codePath: %s", projectTree.Id, projectTree.Name, projectTree.CodePath) } -func (p *TagTree) DelTagTree(rc *ctx.ReqCtx) { +func (p *TagTree) DelTagTree(rc *req.Ctx) { p.TagTreeApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } diff --git a/server/internal/tag/api/team.go b/server/internal/tag/api/team.go index f98d571a..bc91c33d 100644 --- a/server/internal/tag/api/team.go +++ b/server/internal/tag/api/team.go @@ -9,8 +9,8 @@ import ( "mayfly-go/internal/tag/application" "mayfly-go/internal/tag/domain/entity" "mayfly-go/pkg/biz" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/ginx" + "mayfly-go/pkg/req" "mayfly-go/pkg/utils" ) @@ -20,12 +20,12 @@ type Team struct { AccountApp sys_applicaiton.Account } -func (p *Team) GetTeams(rc *ctx.ReqCtx) { +func (p *Team) GetTeams(rc *req.Ctx) { teams := &[]entity.Team{} rc.ResData = p.TeamApp.GetPageList(&entity.Team{}, ginx.GetPageParam(rc.GinCtx), teams) } -func (p *Team) SaveTeam(rc *ctx.ReqCtx) { +func (p *Team) SaveTeam(rc *req.Ctx) { team := &entity.Team{} ginx.BindJsonAndValid(rc.GinCtx, team) @@ -47,12 +47,12 @@ func (p *Team) SaveTeam(rc *ctx.ReqCtx) { } } -func (p *Team) DelTeam(rc *ctx.ReqCtx) { +func (p *Team) DelTeam(rc *req.Ctx) { p.TeamApp.Delete(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } // 获取团队的成员信息 -func (p *Team) GetTeamMembers(rc *ctx.ReqCtx) { +func (p *Team) GetTeamMembers(rc *req.Ctx) { condition := &entity.TeamMember{TeamId: uint64(ginx.PathParamInt(rc.GinCtx, "id"))} condition.Username = rc.GinCtx.Query("username") @@ -60,7 +60,7 @@ func (p *Team) GetTeamMembers(rc *ctx.ReqCtx) { } // 保存团队信息 -func (p *Team) SaveTeamMember(rc *ctx.ReqCtx) { +func (p *Team) SaveTeamMember(rc *req.Ctx) { teamMems := &form.TeamMember{} ginx.BindJsonAndValid(rc.GinCtx, teamMems) @@ -88,7 +88,7 @@ func (p *Team) SaveTeamMember(rc *ctx.ReqCtx) { } // 删除团队成员 -func (p *Team) DelTeamMember(rc *ctx.ReqCtx) { +func (p *Team) DelTeamMember(rc *req.Ctx) { g := rc.GinCtx tid := ginx.PathParamInt(g, "id") aid := ginx.PathParamInt(g, "accountId") @@ -98,12 +98,12 @@ func (p *Team) DelTeamMember(rc *ctx.ReqCtx) { } // 获取团队关联的标签id -func (p *Team) GetTagIds(rc *ctx.ReqCtx) { +func (p *Team) GetTagIds(rc *req.Ctx) { rc.ResData = p.TeamApp.ListTagIds(uint64(ginx.PathParamInt(rc.GinCtx, "id"))) } // 保存团队关联标签信息 -func (p *Team) SaveTags(rc *ctx.ReqCtx) { +func (p *Team) SaveTags(rc *req.Ctx) { g := rc.GinCtx var form form.TagTreeTeam diff --git a/server/internal/tag/router/tag_tree.go b/server/internal/tag/router/tag_tree.go index 17217c83..3b1c2b75 100644 --- a/server/internal/tag/router/tag_tree.go +++ b/server/internal/tag/router/tag_tree.go @@ -3,7 +3,7 @@ package router import ( "mayfly-go/internal/tag/api" "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -17,28 +17,28 @@ func InitTagTreeRouter(router *gin.RouterGroup) { { // 获取标签树列表 project.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.GetTagTree) + req.NewCtxWithGin(c).Handle(m.GetTagTree) }) // 获取登录账号拥有的标签信息 project.GET("account-has", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.GetAccountTags) + req.NewCtxWithGin(c).Handle(m.GetAccountTags) }) - saveProjectTreeLog := ctx.NewLogInfo("标签树-保存信息").WithSave(true) - savePP := ctx.NewPermission("tag:save") + saveProjectTreeLog := req.NewLogInfo("标签树-保存信息").WithSave(true) + savePP := req.NewPermission("tag:save") // 保存项目树下的环境信息 project.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(saveProjectTreeLog). + req.NewCtxWithGin(c).WithLog(saveProjectTreeLog). WithRequiredPermission(savePP). Handle(m.SaveTagTree) }) - delProjectLog := ctx.NewLogInfo("标签树-删除信息").WithSave(true) - delPP := ctx.NewPermission("tag:del") + delProjectLog := req.NewLogInfo("标签树-删除信息").WithSave(true) + delPP := req.NewPermission("tag:del") // 删除标签 project.DELETE(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(delProjectLog). + req.NewCtxWithGin(c).WithLog(delProjectLog). WithRequiredPermission(delPP). Handle(m.DelTagTree) }) diff --git a/server/internal/tag/router/team.go b/server/internal/tag/router/team.go index 30b88070..4a5403b9 100644 --- a/server/internal/tag/router/team.go +++ b/server/internal/tag/router/team.go @@ -4,7 +4,7 @@ import ( sysapp "mayfly-go/internal/sys/application" "mayfly-go/internal/tag/api" "mayfly-go/internal/tag/application" - "mayfly-go/pkg/ctx" + "mayfly-go/pkg/req" "github.com/gin-gonic/gin" ) @@ -20,59 +20,59 @@ func InitTeamRouter(router *gin.RouterGroup) { { // 获取团队列表 project.GET("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.GetTeams) + req.NewCtxWithGin(c).Handle(m.GetTeams) }) - saveProjectTeamLog := ctx.NewLogInfo("团队-保存信息").WithSave(true) - savePP := ctx.NewPermission("team:save") + saveProjectTeamLog := req.NewLogInfo("团队-保存信息").WithSave(true) + savePP := req.NewPermission("team:save") // 保存项目团队信息 project.POST("", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(saveProjectTeamLog). + req.NewCtxWithGin(c).WithLog(saveProjectTeamLog). WithRequiredPermission(savePP). Handle(m.SaveTeam) }) - delProjectTeamLog := ctx.NewLogInfo("团队-删除信息").WithSave(true) - delPP := ctx.NewPermission("team:del") + delProjectTeamLog := req.NewLogInfo("团队-删除信息").WithSave(true) + delPP := req.NewPermission("team:del") project.DELETE(":id", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(delProjectTeamLog). + req.NewCtxWithGin(c).WithLog(delProjectTeamLog). WithRequiredPermission(delPP). Handle(m.DelTeam) }) // 获取团队的成员信息列表 project.GET("/:id/members", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.GetTeamMembers) + req.NewCtxWithGin(c).Handle(m.GetTeamMembers) }) // 保存团队成员 - saveProjectTeamMemLog := ctx.NewLogInfo("团队-新增成员").WithSave(true) - savePmP := ctx.NewPermission("team:member:save") + saveProjectTeamMemLog := req.NewLogInfo("团队-新增成员").WithSave(true) + savePmP := req.NewPermission("team:member:save") project.POST("/:id/members", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(saveProjectTeamMemLog). + req.NewCtxWithGin(c).WithLog(saveProjectTeamMemLog). WithRequiredPermission(savePmP). Handle(m.SaveTeamMember) }) // 删除团队成员 - delProjectTeamMemLog := ctx.NewLogInfo("团队-删除成员").WithSave(true) - savePmdP := ctx.NewPermission("team:member:del") + delProjectTeamMemLog := req.NewLogInfo("团队-删除成员").WithSave(true) + savePmdP := req.NewPermission("team:member:del") project.DELETE("/:id/members/:accountId", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).WithLog(delProjectTeamMemLog). + req.NewCtxWithGin(c).WithLog(delProjectTeamMemLog). WithRequiredPermission(savePmdP). Handle(m.DelTeamMember) }) // 获取团队关联的标签id列表 project.GET("/:id/tags", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c).Handle(m.GetTagIds) + req.NewCtxWithGin(c).Handle(m.GetTagIds) }) // 保存团队标签关联信息 - saveTeamTagLog := ctx.NewLogInfo("团队-保存标签关联信息").WithSave(true) - saveTeamTagP := ctx.NewPermission("team:tag:save") + saveTeamTagLog := req.NewLogInfo("团队-保存标签关联信息").WithSave(true) + saveTeamTagP := req.NewPermission("team:tag:save") project.POST("/:id/tags", func(c *gin.Context) { - ctx.NewReqCtxWithGin(c). + req.NewCtxWithGin(c). WithLog(saveTeamTagLog). WithRequiredPermission(saveTeamTagP). Handle(m.SaveTags) diff --git a/server/pkg/ctx/log_handler.go b/server/pkg/req/log_handler.go similarity index 92% rename from server/pkg/ctx/log_handler.go rename to server/pkg/req/log_handler.go index e4caf789..20e775c5 100644 --- a/server/pkg/ctx/log_handler.go +++ b/server/pkg/req/log_handler.go @@ -1,4 +1,4 @@ -package ctx +package req import ( "fmt" @@ -11,7 +11,7 @@ import ( "github.com/sirupsen/logrus" ) -type SaveLogFunc func(*ReqCtx) +type SaveLogFunc func(*Ctx) var saveLog SaveLogFunc @@ -43,7 +43,7 @@ func (i *LogInfo) WithSave(saveLog bool) *LogInfo { return i } -func LogHandler(rc *ReqCtx) error { +func LogHandler(rc *Ctx) error { li := rc.LogInfo if li == nil { return nil @@ -70,7 +70,7 @@ func LogHandler(rc *ReqCtx) error { return nil } -func getLogMsg(rc *ReqCtx) string { +func getLogMsg(rc *Ctx) string { msg := rc.LogInfo.Description + fmt.Sprintf(" ->%dms", rc.timed) if !utils.IsBlank(reflect.ValueOf(rc.ReqParam)) { msg = msg + fmt.Sprintf("\n--> %s", utils.ToString(rc.ReqParam)) @@ -83,7 +83,7 @@ func getLogMsg(rc *ReqCtx) string { return msg } -func getErrMsg(rc *ReqCtx, err interface{}) string { +func getErrMsg(rc *Ctx, err interface{}) string { msg := rc.LogInfo.Description if !utils.IsBlank(reflect.ValueOf(rc.ReqParam)) { msg = msg + fmt.Sprintf("\n--> %s", utils.ToString(rc.ReqParam)) diff --git a/server/pkg/ctx/permission_handler.go b/server/pkg/req/permission_handler.go similarity index 98% rename from server/pkg/ctx/permission_handler.go rename to server/pkg/req/permission_handler.go index 015c4097..72fd55dc 100644 --- a/server/pkg/ctx/permission_handler.go +++ b/server/pkg/req/permission_handler.go @@ -1,4 +1,4 @@ -package ctx +package req import ( "encoding/json" @@ -29,7 +29,7 @@ var ( permissionCodeRegistry PermissionCodeRegistry ) -func PermissionHandler(rc *ReqCtx) error { +func PermissionHandler(rc *Ctx) error { if permissionCodeRegistry == nil { if rediscli.GetCli() == nil { permissionCodeRegistry = new(DefaultPermissionCodeRegistry) diff --git a/server/pkg/ctx/req_ctx.go b/server/pkg/req/req_ctx.go similarity index 71% rename from server/pkg/ctx/req_ctx.go rename to server/pkg/req/req_ctx.go index d4ac3c65..9d209d02 100644 --- a/server/pkg/ctx/req_ctx.go +++ b/server/pkg/req/req_ctx.go @@ -1,4 +1,4 @@ -package ctx +package req import ( "io" @@ -11,24 +11,24 @@ import ( ) // 处理函数 -type HandlerFunc func(*ReqCtx) +type HandlerFunc func(*Ctx) -type ReqCtx struct { +type Ctx struct { GinCtx *gin.Context // gin context RequiredPermission *Permission // 需要的权限信息,默认为nil,需要校验token LoginAccount *model.LoginAccount // 登录账号信息,只有校验token后才会有值 - LogInfo *LogInfo // 日志相关信息 - ReqParam interface{} // 请求参数,主要用于记录日志 - ResData interface{} // 响应结果 - Err interface{} // 请求错误 + LogInfo *LogInfo // 日志相关信息 + ReqParam any // 请求参数,主要用于记录日志 + ResData any // 响应结果 + Err any // 请求错误 timed int64 // 执行时间 NoRes bool // 无需返回结果,即文件下载等 } -func (rc *ReqCtx) Handle(handler HandlerFunc) { +func (rc *Ctx) Handle(handler HandlerFunc) { ginCtx := rc.GinCtx defer func() { if err := recover(); err != nil { @@ -41,7 +41,7 @@ func (rc *ReqCtx) Handle(handler HandlerFunc) { assert.IsTrue(ginCtx != nil, "ginContext == nil") // 默认为不记录请求参数,可在handler回调函数中覆盖赋值 - rc.ReqParam = 0 + rc.ReqParam = nil // 默认响应结果为nil,可在handler中赋值 rc.ResData = nil @@ -59,40 +59,35 @@ func (rc *ReqCtx) Handle(handler HandlerFunc) { } } -func (rc *ReqCtx) Download(reader io.Reader, filename string) { +func (rc *Ctx) Download(reader io.Reader, filename string) { rc.NoRes = true ginx.Download(rc.GinCtx, reader, filename) } -// 新建请求上下文,默认需要校验token -func NewReqCtx() *ReqCtx { - return &ReqCtx{} -} - -func NewReqCtxWithGin(g *gin.Context) *ReqCtx { - return &ReqCtx{GinCtx: g} +func NewCtxWithGin(g *gin.Context) *Ctx { + return &Ctx{GinCtx: g} } // 调用该方法设置请求描述,则默认记录日志,并不记录响应结果 -func (r *ReqCtx) WithLog(li *LogInfo) *ReqCtx { +func (r *Ctx) WithLog(li *LogInfo) *Ctx { r.LogInfo = li return r } // 设置请求上下文需要的权限信息 -func (r *ReqCtx) WithRequiredPermission(permission *Permission) *ReqCtx { +func (r *Ctx) WithRequiredPermission(permission *Permission) *Ctx { r.RequiredPermission = permission return r } // 是否需要token -func (r *ReqCtx) WithNeedToken(needToken bool) *ReqCtx { +func (r *Ctx) WithNeedToken(needToken bool) *Ctx { r.RequiredPermission = &Permission{NeedToken: false} return r } // 处理器拦截器函数 -type HandlerInterceptorFunc func(*ReqCtx) error +type HandlerInterceptorFunc func(*Ctx) error type HandlerInterceptors []HandlerInterceptorFunc var ( @@ -111,7 +106,7 @@ func UseAfterHandlerInterceptor(b HandlerInterceptorFunc) { } // 应用指定处理器拦截器,如果有一个错误则直接返回错误 -func ApplyHandlerInterceptor(his HandlerInterceptors, rc *ReqCtx) interface{} { +func ApplyHandlerInterceptor(his HandlerInterceptors, rc *Ctx) interface{} { for _, handler := range his { if err := handler(rc); err != nil { return err diff --git a/server/pkg/ctx/token.go b/server/pkg/req/token.go similarity index 99% rename from server/pkg/ctx/token.go rename to server/pkg/req/token.go index 9d3a7a66..d865053e 100644 --- a/server/pkg/ctx/token.go +++ b/server/pkg/req/token.go @@ -1,4 +1,4 @@ -package ctx +package req import ( "errors" diff --git a/server/pkg/starter/run.go b/server/pkg/starter/run.go index 7717da23..9d26f16b 100644 --- a/server/pkg/starter/run.go +++ b/server/pkg/starter/run.go @@ -2,8 +2,8 @@ package starter import ( "mayfly-go/pkg/config" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/logger" + "mayfly-go/pkg/req" ) func RunWebServer() { @@ -12,7 +12,7 @@ func RunWebServer() { // 初始化日志配置信息 logger.Init() // 初始化jwt key与expire time等 - ctx.InitTokenConfig() + req.InitTokenConfig() // 打印banner printBanner() diff --git a/server/pkg/starter/web-server.go b/server/pkg/starter/web-server.go index 6c317abb..bc6cac19 100644 --- a/server/pkg/starter/web-server.go +++ b/server/pkg/starter/web-server.go @@ -4,17 +4,17 @@ import ( "mayfly-go/initialize" "mayfly-go/pkg/biz" "mayfly-go/pkg/config" - "mayfly-go/pkg/ctx" "mayfly-go/pkg/global" + "mayfly-go/pkg/req" ) func runWebServer() { // 权限处理器 - ctx.UseBeforeHandlerInterceptor(ctx.PermissionHandler) + req.UseBeforeHandlerInterceptor(req.PermissionHandler) // 日志处理器 - ctx.UseAfterHandlerInterceptor(ctx.LogHandler) + req.UseAfterHandlerInterceptor(req.LogHandler) // 设置日志保存函数 - ctx.SetSaveLogFunc(initialize.InitSaveLogFunc()) + req.SetSaveLogFunc(initialize.InitSaveLogFunc()) // 注册路由 web := initialize.InitRouter()