mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-02 23:40:24 +08:00
refactor: api层尽可能屏蔽gin框架相关代码
This commit is contained in:
@@ -10,7 +10,7 @@ RUN yarn config set registry 'https://registry.npmmirror.com' && \
|
||||
yarn build
|
||||
|
||||
# 构建后端资源
|
||||
FROM golang:1.21.5 as be-builder
|
||||
FROM golang:1.22 as be-builder
|
||||
|
||||
ENV GOPROXY https://goproxy.cn
|
||||
WORKDIR /mayfly
|
||||
|
||||
@@ -83,7 +83,7 @@
|
||||
</template>
|
||||
</page-table>
|
||||
|
||||
<el-dialog width="720px" :title="`${db} 数据库导出`" v-model="exportDialog.visible">
|
||||
<el-dialog width="750px" :title="`${db} 数据库导出`" v-model="exportDialog.visible">
|
||||
<el-row justify="space-between">
|
||||
<el-col :span="9">
|
||||
<el-form-item label="导出内容: ">
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"mayfly-go/pkg/cache"
|
||||
"mayfly-go/pkg/captcha"
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/otp"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
@@ -33,7 +32,7 @@ type AccountLogin struct {
|
||||
|
||||
// @router /auth/accounts/login [post]
|
||||
func (a *AccountLogin) Login(rc *req.Ctx) {
|
||||
loginForm := ginx.BindJsonAndValid(rc.GinCtx, new(form.LoginForm))
|
||||
loginForm := req.BindJsonAndValid(rc, new(form.LoginForm))
|
||||
|
||||
accountLoginSecurity := config.GetAccountLoginSecurity()
|
||||
// 判断是否有开启登录验证码校验
|
||||
@@ -81,7 +80,7 @@ type OtpVerifyInfo struct {
|
||||
// OTP双因素校验
|
||||
func (a *AccountLogin) OtpVerify(rc *req.Ctx) {
|
||||
otpVerify := new(form.OtpVerfiy)
|
||||
ginx.BindJsonAndValid(rc.GinCtx, otpVerify)
|
||||
req.BindJsonAndValid(rc, otpVerify)
|
||||
|
||||
tokenKey := fmt.Sprintf("otp:token:%s", otpVerify.OtpToken)
|
||||
otpInfoJson := cache.GetStr(tokenKey)
|
||||
|
||||
@@ -97,7 +97,7 @@ func useOtp(account *sysentity.Account, otpIssuer, accessToken string) (*OtpVeri
|
||||
|
||||
// 获取ip与归属地信息
|
||||
func getIpAndRegion(rc *req.Ctx) string {
|
||||
clientIp := rc.GinCtx.ClientIP()
|
||||
clientIp := rc.F.ClientIP()
|
||||
return fmt.Sprintf("%s %s", clientIp, netx.Ip2Region(clientIp))
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"mayfly-go/pkg/cache"
|
||||
"mayfly-go/pkg/captcha"
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
@@ -40,7 +39,7 @@ func (a *LdapLogin) GetLdapEnabled(rc *req.Ctx) {
|
||||
|
||||
// @router /auth/ldap/login [post]
|
||||
func (a *LdapLogin) Login(rc *req.Ctx) {
|
||||
loginForm := ginx.BindJsonAndValid(rc.GinCtx, new(form.LoginForm))
|
||||
loginForm := req.BindJsonAndValid(rc, new(form.LoginForm))
|
||||
|
||||
accountLoginSecurity := config.GetAccountLoginSecurity()
|
||||
// 判断是否有开启登录验证码校验
|
||||
|
||||
@@ -37,7 +37,7 @@ func (a *Oauth2Login) OAuth2Login(rc *req.Ctx) {
|
||||
client, _ := a.getOAuthClient()
|
||||
state := stringx.Rand(32)
|
||||
cache.SetStr("oauth2:state:"+state, "login", 5*time.Minute)
|
||||
rc.GinCtx.Redirect(http.StatusFound, client.AuthCodeURL(state))
|
||||
rc.F.Redirect(http.StatusFound, client.AuthCodeURL(state))
|
||||
}
|
||||
|
||||
func (a *Oauth2Login) OAuth2Bind(rc *req.Ctx) {
|
||||
@@ -45,26 +45,26 @@ func (a *Oauth2Login) OAuth2Bind(rc *req.Ctx) {
|
||||
state := stringx.Rand(32)
|
||||
cache.SetStr("oauth2:state:"+state, "bind:"+strconv.FormatUint(rc.GetLoginAccount().Id, 10),
|
||||
5*time.Minute)
|
||||
rc.GinCtx.Redirect(http.StatusFound, client.AuthCodeURL(state))
|
||||
rc.F.Redirect(http.StatusFound, client.AuthCodeURL(state))
|
||||
}
|
||||
|
||||
func (a *Oauth2Login) OAuth2Callback(rc *req.Ctx) {
|
||||
client, oauth := a.getOAuthClient()
|
||||
|
||||
code := rc.GinCtx.Query("code")
|
||||
code := rc.F.Query("code")
|
||||
biz.NotEmpty(code, "code不能为空")
|
||||
|
||||
state := rc.GinCtx.Query("state")
|
||||
state := rc.F.Query("state")
|
||||
biz.NotEmpty(state, "state不能为空")
|
||||
|
||||
stateAction := cache.GetStr("oauth2:state:" + state)
|
||||
biz.NotEmpty(stateAction, "state已过期, 请重新登录")
|
||||
|
||||
token, err := client.Exchange(rc.GinCtx, code)
|
||||
token, err := client.Exchange(rc, code)
|
||||
biz.ErrIsNilAppendErr(err, "获取OAuth2 accessToken失败: %s")
|
||||
|
||||
// 获取用户信息
|
||||
httpCli := client.Client(rc.GinCtx.Request.Context(), token)
|
||||
httpCli := client.Client(rc.F.GetRequest().Context(), token)
|
||||
resp, err := httpCli.Get(oauth.ResourceURL)
|
||||
biz.ErrIsNilAppendErr(err, "获取用户信息失败: %s")
|
||||
defer resp.Body.Close()
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
msgdto "mayfly-go/internal/msg/application/dto"
|
||||
tagapp "mayfly-go/internal/tag/application"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/logx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
@@ -27,7 +26,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/kanzihuang/vitess/go/vt/sqlparser"
|
||||
)
|
||||
|
||||
@@ -41,7 +39,7 @@ type Db struct {
|
||||
|
||||
// @router /api/dbs [get]
|
||||
func (d *Db) Dbs(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.DbQuery](rc.GinCtx, new(entity.DbQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.DbQuery](rc, new(entity.DbQuery))
|
||||
|
||||
// 不存在可访问标签id,即没有可操作数据
|
||||
codes := d.TagApp.GetAccountResourceCodes(rc.GetLoginAccount().Id, consts.TagResourceTypeDb, queryCond.TagPath)
|
||||
@@ -58,7 +56,7 @@ func (d *Db) Dbs(rc *req.Ctx) {
|
||||
|
||||
func (d *Db) Save(rc *req.Ctx) {
|
||||
form := &form.DbForm{}
|
||||
db := ginx.BindJsonAndCopyTo[*entity.Db](rc.GinCtx, form, new(entity.Db))
|
||||
db := req.BindJsonAndCopyTo[*entity.Db](rc, form, new(entity.Db))
|
||||
|
||||
rc.ReqParam = form
|
||||
|
||||
@@ -66,7 +64,7 @@ func (d *Db) Save(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (d *Db) DeleteDb(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "dbId")
|
||||
idsStr := rc.F.PathParam("dbId")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -84,11 +82,9 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
|
||||
/** 数据库操作相关、执行sql等 ***/
|
||||
|
||||
func (d *Db) ExecSql(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
form := &form.DbSqlExecForm{}
|
||||
ginx.BindJsonAndValid(g, form)
|
||||
form := req.BindJsonAndValid(rc, new(form.DbSqlExecForm))
|
||||
|
||||
dbId := getDbId(g)
|
||||
dbId := getDbId(rc)
|
||||
dbConn, err := d.DbApp.GetDbConn(dbId, form.Db)
|
||||
biz.ErrIsNil(err)
|
||||
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(rc.GetLoginAccount().Id, dbConn.Info.TagPath...), "%s")
|
||||
@@ -154,16 +150,15 @@ type progressMsg struct {
|
||||
|
||||
// 执行sql文件
|
||||
func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
multipart, err := g.Request.MultipartReader()
|
||||
multipart, err := rc.F.GetRequest().MultipartReader()
|
||||
biz.ErrIsNilAppendErr(err, "读取sql文件失败: %s")
|
||||
file, err := multipart.NextPart()
|
||||
biz.ErrIsNilAppendErr(err, "读取sql文件失败: %s")
|
||||
defer file.Close()
|
||||
filename := file.FileName()
|
||||
dbId := getDbId(g)
|
||||
dbName := getDbName(g)
|
||||
clientId := g.Query("clientId")
|
||||
dbId := getDbId(rc)
|
||||
dbName := getDbName(rc)
|
||||
clientId := rc.F.Query("clientId")
|
||||
|
||||
dbConn, err := d.DbApp.GetDbConn(dbId, dbName)
|
||||
biz.ErrIsNil(err)
|
||||
@@ -254,12 +249,11 @@ func (d *Db) ExecSqlFile(rc *req.Ctx) {
|
||||
|
||||
// 数据库dump
|
||||
func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
dbId := getDbId(g)
|
||||
dbNamesStr := g.Query("db")
|
||||
dumpType := g.Query("type")
|
||||
tablesStr := g.Query("tables")
|
||||
extName := g.Query("extName")
|
||||
dbId := getDbId(rc)
|
||||
dbNamesStr := rc.F.Query("db")
|
||||
dumpType := rc.F.Query("type")
|
||||
tablesStr := rc.F.Query("tables")
|
||||
extName := rc.F.Query("extName")
|
||||
switch extName {
|
||||
case ".gz", ".gzip", "gz", "gzip":
|
||||
extName = ".gz"
|
||||
@@ -279,10 +273,10 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
|
||||
now := time.Now()
|
||||
filename := fmt.Sprintf("%s.%s.sql%s", db.Name, now.Format("20060102150405"), extName)
|
||||
g.Header("Content-Type", "application/octet-stream")
|
||||
g.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
rc.F.Header("Content-Type", "application/octet-stream")
|
||||
rc.F.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
if extName != ".gz" {
|
||||
g.Header("Content-Encoding", "gzip")
|
||||
rc.F.Header("Content-Encoding", "gzip")
|
||||
}
|
||||
|
||||
var dbNames, tables []string
|
||||
@@ -293,7 +287,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
tables = strings.Split(tablesStr, ",")
|
||||
}
|
||||
|
||||
writer := newGzipWriter(g.Writer)
|
||||
writer := newGzipWriter(rc.F.GetWriter())
|
||||
defer func() {
|
||||
msg := anyx.ToString(recover())
|
||||
if len(msg) > 0 {
|
||||
@@ -305,13 +299,13 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
}()
|
||||
|
||||
for _, dbName := range dbNames {
|
||||
d.dumpDb(writer, dbId, dbName, tables, needStruct, needData)
|
||||
d.dumpDb(rc.MetaCtx, writer, dbId, dbName, tables, needStruct, needData)
|
||||
}
|
||||
|
||||
rc.ReqParam = collx.Kvs("db", db, "databases", dbNamesStr, "tables", tablesStr, "dumpType", dumpType)
|
||||
}
|
||||
|
||||
func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []string, needStruct bool, needData bool) {
|
||||
func (d *Db) dumpDb(ctx context.Context, writer *gzipWriter, dbId uint64, dbName string, tables []string, needStruct bool, needData bool) {
|
||||
dbConn, err := d.DbApp.GetDbConn(dbId, dbName)
|
||||
biz.ErrIsNil(err)
|
||||
writer.WriteString("\n-- ----------------------------")
|
||||
@@ -353,7 +347,7 @@ func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []str
|
||||
writer.WriteString("BEGIN;\n")
|
||||
}
|
||||
insertSql := "INSERT INTO %s VALUES (%s);\n"
|
||||
dbConn.WalkTableRows(context.TODO(), table, func(record map[string]any, columns []*dbi.QueryColumn) error {
|
||||
dbConn.WalkTableRows(ctx, table, func(record map[string]any, columns []*dbi.QueryColumn) error {
|
||||
var values []string
|
||||
writer.TryFlush()
|
||||
for _, column := range columns {
|
||||
@@ -379,26 +373,25 @@ func (d *Db) dumpDb(writer *gzipWriter, dbId uint64, dbName string, tables []str
|
||||
}
|
||||
|
||||
func (d *Db) TableInfos(rc *req.Ctx) {
|
||||
res, err := d.getDbConn(rc.GinCtx).GetDialect().GetTables()
|
||||
res, err := d.getDbConn(rc).GetDialect().GetTables()
|
||||
biz.ErrIsNilAppendErr(err, "获取表信息失败: %s")
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (d *Db) TableIndex(rc *req.Ctx) {
|
||||
tn := rc.GinCtx.Query("tableName")
|
||||
tn := rc.F.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
res, err := d.getDbConn(rc.GinCtx).GetDialect().GetTableIndex(tn)
|
||||
res, err := d.getDbConn(rc).GetDialect().GetTableIndex(tn)
|
||||
biz.ErrIsNilAppendErr(err, "获取表索引信息失败: %s")
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
// @router /api/db/:dbId/c-metadata [get]
|
||||
func (d *Db) ColumnMA(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
tn := g.Query("tableName")
|
||||
tn := rc.F.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
|
||||
dbi := d.getDbConn(rc.GinCtx)
|
||||
dbi := d.getDbConn(rc)
|
||||
res, err := dbi.GetDialect().GetColumns(tn)
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库列信息失败: %s")
|
||||
rc.ResData = res
|
||||
@@ -406,7 +399,7 @@ func (d *Db) ColumnMA(rc *req.Ctx) {
|
||||
|
||||
// @router /api/db/:dbId/hint-tables [get]
|
||||
func (d *Db) HintTables(rc *req.Ctx) {
|
||||
dbi := d.getDbConn(rc.GinCtx)
|
||||
dbi := d.getDbConn(rc)
|
||||
|
||||
dm := dbi.GetDialect()
|
||||
// 获取所有表
|
||||
@@ -447,22 +440,22 @@ func (d *Db) HintTables(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (d *Db) GetTableDDL(rc *req.Ctx) {
|
||||
tn := rc.GinCtx.Query("tableName")
|
||||
tn := rc.F.Query("tableName")
|
||||
biz.NotEmpty(tn, "tableName不能为空")
|
||||
res, err := d.getDbConn(rc.GinCtx).GetDialect().GetTableDDL(tn)
|
||||
res, err := d.getDbConn(rc).GetDialect().GetTableDDL(tn)
|
||||
biz.ErrIsNilAppendErr(err, "获取表ddl失败: %s")
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (d *Db) GetSchemas(rc *req.Ctx) {
|
||||
res, err := d.getDbConn(rc.GinCtx).GetDialect().GetSchemas()
|
||||
res, err := d.getDbConn(rc).GetDialect().GetSchemas()
|
||||
biz.ErrIsNilAppendErr(err, "获取schemas失败: %s")
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (d *Db) CopyTable(rc *req.Ctx) {
|
||||
form := &form.DbCopyTableForm{}
|
||||
copy := ginx.BindJsonAndCopyTo[*dbi.DbCopyTable](rc.GinCtx, form, new(dbi.DbCopyTable))
|
||||
copy := req.BindJsonAndCopyTo[*dbi.DbCopyTable](rc, form, new(dbi.DbCopyTable))
|
||||
|
||||
conn, err := d.DbApp.GetDbConn(form.Id, form.Db)
|
||||
biz.ErrIsNilAppendErr(err, "拷贝表失败: %s")
|
||||
@@ -474,41 +467,20 @@ func (d *Db) CopyTable(rc *req.Ctx) {
|
||||
biz.ErrIsNilAppendErr(err, "拷贝表失败: %s")
|
||||
}
|
||||
|
||||
func getDbId(g *gin.Context) uint64 {
|
||||
dbId, _ := strconv.Atoi(g.Param("dbId"))
|
||||
func getDbId(rc *req.Ctx) uint64 {
|
||||
dbId := rc.F.PathParamInt("dbId")
|
||||
biz.IsTrue(dbId > 0, "dbId错误")
|
||||
return uint64(dbId)
|
||||
}
|
||||
|
||||
func getDbName(g *gin.Context) string {
|
||||
db := g.Query("db")
|
||||
func getDbName(rc *req.Ctx) string {
|
||||
db := rc.F.Query("db")
|
||||
biz.NotEmpty(db, "db不能为空")
|
||||
return db
|
||||
}
|
||||
|
||||
func (d *Db) getDbConn(g *gin.Context) *dbi.DbConn {
|
||||
dc, err := d.DbApp.GetDbConn(getDbId(g), getDbName(g))
|
||||
func (d *Db) getDbConn(rc *req.Ctx) *dbi.DbConn {
|
||||
dc, err := d.DbApp.GetDbConn(getDbId(rc), getDbName(rc))
|
||||
biz.ErrIsNil(err)
|
||||
return dc
|
||||
}
|
||||
|
||||
// GetRestoreTask 获取数据库备份任务
|
||||
// @router /api/instances/:instance/restore-task [GET]
|
||||
func (d *Db) GetRestoreTask(rc *req.Ctx) {
|
||||
// todo get restore task
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// SaveRestoreTask 设置数据库备份任务
|
||||
// @router /api/instances/:instance/restore-task [POST]
|
||||
func (d *Db) SaveRestoreTask(rc *req.Ctx) {
|
||||
// todo set restore task
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
// GetRestoreHistories 获取数据库备份历史
|
||||
// @router /api/instances/:instance/restore-histories [GET]
|
||||
func (d *Db) GetRestoreHistories(rc *req.Ctx) {
|
||||
// todo get restore histories
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"mayfly-go/internal/db/application"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/timex"
|
||||
"strconv"
|
||||
@@ -26,12 +25,12 @@ type DbBackup struct {
|
||||
// GetPageList 获取数据库备份任务
|
||||
// @router /api/dbs/:dbId/backups [GET]
|
||||
func (d *DbBackup) GetPageList(rc *req.Ctx) {
|
||||
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
|
||||
dbId := uint64(rc.F.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "db_instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.DbBackupQuery](rc.GinCtx, new(entity.DbBackupQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.DbBackupQuery](rc, new(entity.DbBackupQuery))
|
||||
queryCond.DbInstanceId = db.InstanceId
|
||||
queryCond.InDbNames = strings.Fields(db.Database)
|
||||
res, err := d.backupApp.GetPageList(queryCond, page, new([]vo.DbBackup))
|
||||
@@ -42,14 +41,13 @@ func (d *DbBackup) GetPageList(rc *req.Ctx) {
|
||||
// Create 保存数据库备份任务
|
||||
// @router /api/dbs/:dbId/backups [POST]
|
||||
func (d *DbBackup) Create(rc *req.Ctx) {
|
||||
backupForm := &form.DbBackupForm{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, backupForm)
|
||||
backupForm := req.BindJsonAndValid(rc, &form.DbBackupForm{})
|
||||
rc.ReqParam = backupForm
|
||||
|
||||
dbNames := strings.Fields(backupForm.DbNames)
|
||||
biz.IsTrue(len(dbNames) > 0, "解析数据库备份任务失败:数据库名称未定义")
|
||||
|
||||
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
|
||||
dbId := uint64(rc.F.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instanceId")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
@@ -73,7 +71,7 @@ func (d *DbBackup) Create(rc *req.Ctx) {
|
||||
// @router /api/dbs/:dbId/backups/:backupId [PUT]
|
||||
func (d *DbBackup) Update(rc *req.Ctx) {
|
||||
backupForm := &form.DbBackupForm{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, backupForm)
|
||||
req.BindJsonAndValid(rc, backupForm)
|
||||
rc.ReqParam = backupForm
|
||||
|
||||
job := &entity.DbBackup{}
|
||||
@@ -86,7 +84,7 @@ func (d *DbBackup) Update(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (d *DbBackup) walk(rc *req.Ctx, paramName string, fn func(ctx context.Context, id uint64) error) error {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, paramName)
|
||||
idsStr := rc.F.PathParam(paramName)
|
||||
biz.NotEmpty(idsStr, paramName+" 为空")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Fields(idsStr)
|
||||
@@ -135,7 +133,7 @@ func (d *DbBackup) Start(rc *req.Ctx) {
|
||||
// GetDbNamesWithoutBackup 获取未配置定时备份的数据库名称
|
||||
// @router /api/dbs/:dbId/db-names-without-backup [GET]
|
||||
func (d *DbBackup) GetDbNamesWithoutBackup(rc *req.Ctx) {
|
||||
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
|
||||
dbId := uint64(rc.F.PathParamInt("dbId"))
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
dbNames := strings.Fields(db.Database)
|
||||
@@ -147,12 +145,12 @@ func (d *DbBackup) GetDbNamesWithoutBackup(rc *req.Ctx) {
|
||||
// GetHistoryPageList 获取数据库备份历史
|
||||
// @router /api/dbs/:dbId/backups/:backupId/histories [GET]
|
||||
func (d *DbBackup) GetHistoryPageList(rc *req.Ctx) {
|
||||
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
|
||||
dbId := uint64(rc.F.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "db_instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
|
||||
backupHistoryCond, page := ginx.BindQueryAndPage[*entity.DbBackupHistoryQuery](rc.GinCtx, new(entity.DbBackupHistoryQuery))
|
||||
backupHistoryCond, page := req.BindQueryAndPage[*entity.DbBackupHistoryQuery](rc, new(entity.DbBackupHistoryQuery))
|
||||
backupHistoryCond.DbInstanceId = db.InstanceId
|
||||
backupHistoryCond.InDbNames = strings.Fields(db.Database)
|
||||
backupHistories := make([]*vo.DbBackupHistory, 0, page.PageSize)
|
||||
@@ -182,7 +180,7 @@ func (d *DbBackup) GetHistoryPageList(rc *req.Ctx) {
|
||||
// RestoreHistories 从数据库备份历史中恢复数据库
|
||||
// @router /api/dbs/:dbId/backup-histories/:backupHistoryId/restore [POST]
|
||||
func (d *DbBackup) RestoreHistories(rc *req.Ctx) {
|
||||
pm := ginx.PathParam(rc.GinCtx, "backupHistoryId")
|
||||
pm := rc.F.PathParam("backupHistoryId")
|
||||
biz.NotEmpty(pm, "backupHistoryId 为空")
|
||||
idsStr := strings.Fields(pm)
|
||||
ids := make([]uint64, 0, len(idsStr))
|
||||
|
||||
@@ -7,13 +7,10 @@ import (
|
||||
"mayfly-go/internal/db/application"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/stringx"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DataSyncTask struct {
|
||||
@@ -21,14 +18,14 @@ type DataSyncTask struct {
|
||||
}
|
||||
|
||||
func (d *DataSyncTask) Tasks(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.DataSyncTaskQuery](rc.GinCtx, new(entity.DataSyncTaskQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.DataSyncTaskQuery](rc, new(entity.DataSyncTaskQuery))
|
||||
res, err := d.DataSyncTaskApp.GetPageList(queryCond, page, new([]vo.DataSyncTaskListVO))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (d *DataSyncTask) Logs(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.DataSyncLogQuery](rc.GinCtx, new(entity.DataSyncLogQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.DataSyncLogQuery](rc, new(entity.DataSyncLogQuery))
|
||||
res, err := d.DataSyncTaskApp.GetTaskLogList(queryCond, page, new([]vo.DataSyncLogListVO))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
@@ -36,7 +33,7 @@ func (d *DataSyncTask) Logs(rc *req.Ctx) {
|
||||
|
||||
func (d *DataSyncTask) SaveTask(rc *req.Ctx) {
|
||||
form := &form.DataSyncTaskForm{}
|
||||
task := ginx.BindJsonAndCopyTo[*entity.DataSyncTask](rc.GinCtx, form, new(entity.DataSyncTask))
|
||||
task := req.BindJsonAndCopyTo[*entity.DataSyncTask](rc, form, new(entity.DataSyncTask))
|
||||
|
||||
// 解码base64 sql
|
||||
sqlBytes, err := base64.StdEncoding.DecodeString(task.DataSql)
|
||||
@@ -50,7 +47,7 @@ func (d *DataSyncTask) SaveTask(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (d *DataSyncTask) DeleteTask(rc *req.Ctx) {
|
||||
taskId := ginx.PathParam(rc.GinCtx, "taskId")
|
||||
taskId := rc.F.PathParam("taskId")
|
||||
rc.ReqParam = taskId
|
||||
ids := strings.Split(taskId, ",")
|
||||
|
||||
@@ -63,7 +60,7 @@ func (d *DataSyncTask) DeleteTask(rc *req.Ctx) {
|
||||
|
||||
func (d *DataSyncTask) ChangeStatus(rc *req.Ctx) {
|
||||
form := &form.DataSyncTaskStatusForm{}
|
||||
task := ginx.BindJsonAndCopyTo[*entity.DataSyncTask](rc.GinCtx, form, new(entity.DataSyncTask))
|
||||
task := req.BindJsonAndCopyTo[*entity.DataSyncTask](rc, form, new(entity.DataSyncTask))
|
||||
_ = d.DataSyncTaskApp.UpdateById(rc.MetaCtx, task)
|
||||
|
||||
if task.Status == entity.DataSyncTaskStatusEnable {
|
||||
@@ -78,13 +75,13 @@ func (d *DataSyncTask) ChangeStatus(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (d *DataSyncTask) Run(rc *req.Ctx) {
|
||||
taskId := getTaskId(rc.GinCtx)
|
||||
taskId := getTaskId(rc)
|
||||
rc.ReqParam = taskId
|
||||
_ = d.DataSyncTaskApp.RunCronJob(taskId)
|
||||
}
|
||||
|
||||
func (d *DataSyncTask) Stop(rc *req.Ctx) {
|
||||
taskId := getTaskId(rc.GinCtx)
|
||||
taskId := getTaskId(rc)
|
||||
rc.ReqParam = taskId
|
||||
|
||||
task := new(entity.DataSyncTask)
|
||||
@@ -94,13 +91,13 @@ func (d *DataSyncTask) Stop(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (d *DataSyncTask) GetTask(rc *req.Ctx) {
|
||||
taskId := getTaskId(rc.GinCtx)
|
||||
taskId := getTaskId(rc)
|
||||
dbEntity, _ := d.DataSyncTaskApp.GetById(new(entity.DataSyncTask), taskId)
|
||||
rc.ResData = dbEntity
|
||||
}
|
||||
|
||||
func getTaskId(g *gin.Context) uint64 {
|
||||
instanceId, _ := strconv.Atoi(g.Param("taskId"))
|
||||
func getTaskId(rc *req.Ctx) uint64 {
|
||||
instanceId := rc.F.PathParamInt("taskId")
|
||||
biz.IsTrue(instanceId > 0, "instanceId 错误")
|
||||
return uint64(instanceId)
|
||||
}
|
||||
|
||||
@@ -6,13 +6,10 @@ import (
|
||||
"mayfly-go/internal/db/application"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/cryptox"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
@@ -23,7 +20,7 @@ type Instance struct {
|
||||
// Instances 获取数据库实例信息
|
||||
// @router /api/instances [get]
|
||||
func (d *Instance) Instances(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.InstanceQuery](rc.GinCtx, new(entity.InstanceQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.InstanceQuery](rc, new(entity.InstanceQuery))
|
||||
res, err := d.InstanceApp.GetPageList(queryCond, page, new([]vo.InstanceListVO))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
@@ -31,7 +28,7 @@ func (d *Instance) Instances(rc *req.Ctx) {
|
||||
|
||||
func (d *Instance) TestConn(rc *req.Ctx) {
|
||||
form := &form.InstanceForm{}
|
||||
instance := ginx.BindJsonAndCopyTo[*entity.DbInstance](rc.GinCtx, form, new(entity.DbInstance))
|
||||
instance := req.BindJsonAndCopyTo[*entity.DbInstance](rc, form, new(entity.DbInstance))
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
||||
@@ -45,7 +42,7 @@ func (d *Instance) TestConn(rc *req.Ctx) {
|
||||
// @router /api/instances [post]
|
||||
func (d *Instance) SaveInstance(rc *req.Ctx) {
|
||||
form := &form.InstanceForm{}
|
||||
instance := ginx.BindJsonAndCopyTo[*entity.DbInstance](rc.GinCtx, form, new(entity.DbInstance))
|
||||
instance := req.BindJsonAndCopyTo[*entity.DbInstance](rc, form, new(entity.DbInstance))
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(form.Password, true)
|
||||
@@ -61,7 +58,7 @@ func (d *Instance) SaveInstance(rc *req.Ctx) {
|
||||
// GetInstance 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
// @router /api/instances/:instance [GET]
|
||||
func (d *Instance) GetInstance(rc *req.Ctx) {
|
||||
dbId := getInstanceId(rc.GinCtx)
|
||||
dbId := getInstanceId(rc)
|
||||
dbEntity, err := d.InstanceApp.GetById(new(entity.DbInstance), dbId)
|
||||
biz.ErrIsNil(err, "获取数据库实例错误")
|
||||
dbEntity.Password = ""
|
||||
@@ -71,7 +68,7 @@ func (d *Instance) GetInstance(rc *req.Ctx) {
|
||||
// GetInstancePwd 获取数据库实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
// @router /api/instances/:instance/pwd [GET]
|
||||
func (d *Instance) GetInstancePwd(rc *req.Ctx) {
|
||||
instanceId := getInstanceId(rc.GinCtx)
|
||||
instanceId := getInstanceId(rc)
|
||||
instanceEntity, err := d.InstanceApp.GetById(new(entity.DbInstance), instanceId, "Password")
|
||||
biz.ErrIsNil(err, "获取数据库实例错误")
|
||||
biz.ErrIsNil(instanceEntity.PwdDecrypt())
|
||||
@@ -81,7 +78,7 @@ func (d *Instance) GetInstancePwd(rc *req.Ctx) {
|
||||
// DeleteInstance 删除数据库实例信息
|
||||
// @router /api/instances/:instance [DELETE]
|
||||
func (d *Instance) DeleteInstance(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "instanceId")
|
||||
idsStr := rc.F.PathParam("instanceId")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -96,7 +93,7 @@ func (d *Instance) DeleteInstance(rc *req.Ctx) {
|
||||
|
||||
// 获取数据库实例的所有数据库名
|
||||
func (d *Instance) GetDatabaseNames(rc *req.Ctx) {
|
||||
instanceId := getInstanceId(rc.GinCtx)
|
||||
instanceId := getInstanceId(rc)
|
||||
instance, err := d.InstanceApp.GetById(new(entity.DbInstance), instanceId, "Password")
|
||||
biz.ErrIsNil(err, "获取数据库实例错误")
|
||||
biz.ErrIsNil(instance.PwdDecrypt())
|
||||
@@ -107,7 +104,7 @@ func (d *Instance) GetDatabaseNames(rc *req.Ctx) {
|
||||
|
||||
// 获取数据库实例server信息
|
||||
func (d *Instance) GetDbServer(rc *req.Ctx) {
|
||||
instanceId := getInstanceId(rc.GinCtx)
|
||||
instanceId := getInstanceId(rc)
|
||||
conn, err := d.DbApp.GetDbConnByInstanceId(instanceId)
|
||||
biz.ErrIsNil(err)
|
||||
res, err := conn.GetDialect().GetDbServer()
|
||||
@@ -115,8 +112,8 @@ func (d *Instance) GetDbServer(rc *req.Ctx) {
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func getInstanceId(g *gin.Context) uint64 {
|
||||
instanceId, _ := strconv.Atoi(g.Param("instanceId"))
|
||||
func getInstanceId(rc *req.Ctx) uint64 {
|
||||
instanceId := rc.F.PathParamInt("instanceId")
|
||||
biz.IsTrue(instanceId > 0, "instanceId 错误")
|
||||
return uint64(instanceId)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"mayfly-go/internal/db/application"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -21,13 +20,13 @@ type DbRestore struct {
|
||||
// GetPageList 获取数据库恢复任务
|
||||
// @router /api/dbs/:dbId/restores [GET]
|
||||
func (d *DbRestore) GetPageList(rc *req.Ctx) {
|
||||
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
|
||||
dbId := uint64(rc.F.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "db_instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
|
||||
var restores []vo.DbRestore
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.DbRestoreQuery](rc.GinCtx, new(entity.DbRestoreQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.DbRestoreQuery](rc, new(entity.DbRestoreQuery))
|
||||
queryCond.DbInstanceId = db.InstanceId
|
||||
queryCond.InDbNames = strings.Fields(db.Database)
|
||||
res, err := d.restoreApp.GetPageList(queryCond, page, &restores)
|
||||
@@ -39,10 +38,10 @@ func (d *DbRestore) GetPageList(rc *req.Ctx) {
|
||||
// @router /api/dbs/:dbId/restores [POST]
|
||||
func (d *DbRestore) Create(rc *req.Ctx) {
|
||||
restoreForm := &form.DbRestoreForm{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, restoreForm)
|
||||
req.BindJsonAndValid(rc, restoreForm)
|
||||
rc.ReqParam = restoreForm
|
||||
|
||||
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
|
||||
dbId := uint64(rc.F.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instanceId")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
@@ -70,7 +69,7 @@ func (d *DbRestore) createWithBackupHistory(backupHistoryIds string) {
|
||||
// @router /api/dbs/:dbId/restores/:restoreId [PUT]
|
||||
func (d *DbRestore) Update(rc *req.Ctx) {
|
||||
restoreForm := &form.DbRestoreForm{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, restoreForm)
|
||||
req.BindJsonAndValid(rc, restoreForm)
|
||||
rc.ReqParam = restoreForm
|
||||
|
||||
job := &entity.DbRestore{}
|
||||
@@ -81,7 +80,7 @@ func (d *DbRestore) Update(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (d *DbRestore) walk(rc *req.Ctx, fn func(ctx context.Context, restoreId uint64) error) error {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "restoreId")
|
||||
idsStr := rc.F.PathParam("restoreId")
|
||||
biz.NotEmpty(idsStr, "restoreId 为空")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Fields(idsStr)
|
||||
@@ -123,7 +122,7 @@ func (d *DbRestore) Disable(rc *req.Ctx) {
|
||||
// GetDbNamesWithoutRestore 获取未配置定时恢复的数据库名称
|
||||
// @router /api/dbs/:dbId/db-names-without-backup [GET]
|
||||
func (d *DbRestore) GetDbNamesWithoutRestore(rc *req.Ctx) {
|
||||
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
|
||||
dbId := uint64(rc.F.PathParamInt("dbId"))
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
dbNames := strings.Fields(db.Database)
|
||||
@@ -136,9 +135,9 @@ func (d *DbRestore) GetDbNamesWithoutRestore(rc *req.Ctx) {
|
||||
// @router /api/dbs/:dbId/restores/:restoreId/histories [GET]
|
||||
func (d *DbRestore) GetHistoryPageList(rc *req.Ctx) {
|
||||
queryCond := &entity.DbRestoreHistoryQuery{
|
||||
DbRestoreId: uint64(ginx.PathParamInt(rc.GinCtx, "restoreId")),
|
||||
DbRestoreId: uint64(rc.F.PathParamInt("restoreId")),
|
||||
}
|
||||
res, err := d.restoreApp.GetHistoryPageList(queryCond, ginx.GetPageParam(rc.GinCtx), new([]vo.DbRestoreHistory))
|
||||
res, err := d.restoreApp.GetHistoryPageList(queryCond, rc.F.GetPageParam(), new([]vo.DbRestoreHistory))
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库备份历史失败: %v")
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"mayfly-go/internal/db/application"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
)
|
||||
|
||||
@@ -15,12 +14,11 @@ type DbSql struct {
|
||||
|
||||
// @router /api/db/:dbId/sql [post]
|
||||
func (d *DbSql) SaveSql(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
dbSqlForm := &form.DbSqlSaveForm{}
|
||||
ginx.BindJsonAndValid(g, dbSqlForm)
|
||||
req.BindJsonAndValid(rc, dbSqlForm)
|
||||
rc.ReqParam = dbSqlForm
|
||||
|
||||
dbId := getDbId(g)
|
||||
dbId := getDbId(rc)
|
||||
|
||||
account := rc.GetLoginAccount()
|
||||
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
||||
@@ -39,8 +37,8 @@ func (d *DbSql) SaveSql(rc *req.Ctx) {
|
||||
|
||||
// 获取所有保存的sql names
|
||||
func (d *DbSql) GetSqlNames(rc *req.Ctx) {
|
||||
dbId := getDbId(rc.GinCtx)
|
||||
dbName := getDbName(rc.GinCtx)
|
||||
dbId := getDbId(rc)
|
||||
dbName := getDbName(rc)
|
||||
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
|
||||
dbSql.CreatorId = rc.GetLoginAccount().Id
|
||||
@@ -52,22 +50,22 @@ func (d *DbSql) GetSqlNames(rc *req.Ctx) {
|
||||
|
||||
// 删除保存的sql
|
||||
func (d *DbSql) DeleteSql(rc *req.Ctx) {
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: getDbId(rc.GinCtx)}
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: getDbId(rc)}
|
||||
dbSql.CreatorId = rc.GetLoginAccount().Id
|
||||
dbSql.Name = rc.GinCtx.Query("name")
|
||||
dbSql.Db = rc.GinCtx.Query("db")
|
||||
dbSql.Name = rc.F.Query("name")
|
||||
dbSql.Db = rc.F.Query("db")
|
||||
|
||||
biz.ErrIsNil(d.DbSqlApp.DeleteByCond(rc.MetaCtx, dbSql))
|
||||
}
|
||||
|
||||
// @router /api/db/:dbId/sql [get]
|
||||
func (d *DbSql) GetSql(rc *req.Ctx) {
|
||||
dbId := getDbId(rc.GinCtx)
|
||||
dbName := getDbName(rc.GinCtx)
|
||||
dbId := getDbId(rc)
|
||||
dbName := getDbName(rc)
|
||||
// 根据创建者id, 数据库id,以及sql模板名称查询保存的sql信息
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
|
||||
dbSql.CreatorId = rc.GetLoginAccount().Id
|
||||
dbSql.Name = rc.GinCtx.Query("name")
|
||||
dbSql.Name = rc.F.Query("name")
|
||||
|
||||
e := d.DbSqlApp.GetBy(dbSql)
|
||||
if e != nil {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"mayfly-go/internal/db/application"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
)
|
||||
|
||||
@@ -13,7 +12,7 @@ type DbSqlExec struct {
|
||||
}
|
||||
|
||||
func (d *DbSqlExec) DbSqlExecs(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.DbSqlExecQuery))
|
||||
queryCond, page := req.BindQueryAndPage(rc, new(entity.DbSqlExecQuery))
|
||||
res, err := d.DbSqlExecApp.GetPageList(queryCond, page, new([]entity.DbSqlExec))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
|
||||
@@ -46,13 +46,6 @@ func InitDbRouter(router *gin.RouterGroup) {
|
||||
|
||||
req.NewGet(":dbId/hint-tables", d.HintTables),
|
||||
|
||||
req.NewGet(":dbId/restore-task", d.GetRestoreTask),
|
||||
|
||||
req.NewPost(":dbId/restore-task", d.SaveRestoreTask).
|
||||
Log(req.NewLogSave("db-保存数据库恢复任务")),
|
||||
|
||||
req.NewGet(":dbId/restore-histories", d.GetRestoreHistories),
|
||||
|
||||
req.NewPost(":dbId/copy-table", d.CopyTable),
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"mayfly-go/internal/machine/application"
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -17,14 +16,14 @@ type AuthCert struct {
|
||||
}
|
||||
|
||||
func (ac *AuthCert) BaseAuthCerts(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.AuthCertQuery))
|
||||
queryCond, page := req.BindQueryAndPage(rc, new(entity.AuthCertQuery))
|
||||
res, err := ac.AuthCertApp.GetPageList(queryCond, page, new([]vo.AuthCertBaseVO))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (ac *AuthCert) AuthCerts(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.AuthCertQuery))
|
||||
queryCond, page := req.BindQueryAndPage(rc, new(entity.AuthCertQuery))
|
||||
|
||||
res := new([]*entity.AuthCert)
|
||||
pageRes, err := ac.AuthCertApp.GetPageList(queryCond, page, res)
|
||||
@@ -37,7 +36,7 @@ func (ac *AuthCert) AuthCerts(rc *req.Ctx) {
|
||||
|
||||
func (c *AuthCert) SaveAuthCert(rc *req.Ctx) {
|
||||
acForm := &form.AuthCertForm{}
|
||||
ac := ginx.BindJsonAndCopyTo(rc.GinCtx, acForm, new(entity.AuthCert))
|
||||
ac := req.BindJsonAndCopyTo(rc, acForm, new(entity.AuthCert))
|
||||
|
||||
// 脱敏记录日志
|
||||
acForm.Passphrase = "***"
|
||||
@@ -48,7 +47,7 @@ func (c *AuthCert) SaveAuthCert(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (c *AuthCert) Delete(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "id")
|
||||
idsStr := rc.F.PathParam("id")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
tagapp "mayfly-go/internal/tag/application"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/anyx"
|
||||
@@ -34,7 +33,7 @@ type Machine struct {
|
||||
}
|
||||
|
||||
func (m *Machine) Machines(rc *req.Ctx) {
|
||||
condition, pageParam := ginx.BindQueryAndPage(rc.GinCtx, new(entity.MachineQuery))
|
||||
condition, pageParam := req.BindQueryAndPage(rc, new(entity.MachineQuery))
|
||||
|
||||
// 不存在可访问标签id,即没有可操作数据
|
||||
codes := m.TagApp.GetAccountResourceCodes(rc.GetLoginAccount().Id, consts.TagResourceTypeMachine, condition.TagPath)
|
||||
@@ -65,7 +64,7 @@ func (m *Machine) Machines(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Machine) MachineStats(rc *req.Ctx) {
|
||||
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
|
||||
cli, err := m.MachineApp.GetCli(GetMachineId(rc))
|
||||
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
||||
rc.ResData = cli.GetAllStats()
|
||||
}
|
||||
@@ -73,7 +72,7 @@ func (m *Machine) MachineStats(rc *req.Ctx) {
|
||||
// 保存机器信息
|
||||
func (m *Machine) SaveMachine(rc *req.Ctx) {
|
||||
machineForm := new(form.MachineForm)
|
||||
me := ginx.BindJsonAndCopyTo(rc.GinCtx, machineForm, new(entity.Machine))
|
||||
me := req.BindJsonAndCopyTo(rc, machineForm, new(entity.Machine))
|
||||
|
||||
machineForm.Password = "******"
|
||||
rc.ReqParam = machineForm
|
||||
@@ -82,21 +81,20 @@ func (m *Machine) SaveMachine(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Machine) TestConn(rc *req.Ctx) {
|
||||
me := ginx.BindJsonAndCopyTo(rc.GinCtx, new(form.MachineForm), new(entity.Machine))
|
||||
me := req.BindJsonAndCopyTo(rc, new(form.MachineForm), new(entity.Machine))
|
||||
// 测试连接
|
||||
biz.ErrIsNilAppendErr(m.MachineApp.TestConn(me), "该机器无法连接: %s")
|
||||
}
|
||||
|
||||
func (m *Machine) ChangeStatus(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
id := uint64(ginx.PathParamInt(g, "machineId"))
|
||||
status := int8(ginx.PathParamInt(g, "status"))
|
||||
id := uint64(rc.F.PathParamInt("machineId"))
|
||||
status := int8(rc.F.PathParamInt("status"))
|
||||
rc.ReqParam = collx.Kvs("id", id, "status", status)
|
||||
biz.ErrIsNil(m.MachineApp.ChangeStatus(rc.MetaCtx, id, status))
|
||||
}
|
||||
|
||||
func (m *Machine) DeleteMachine(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "machineId")
|
||||
idsStr := rc.F.PathParam("machineId")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -109,24 +107,23 @@ func (m *Machine) DeleteMachine(rc *req.Ctx) {
|
||||
|
||||
// 获取进程列表信息
|
||||
func (m *Machine) GetProcess(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
cmd := "ps -aux "
|
||||
sortType := g.Query("sortType")
|
||||
sortType := rc.F.Query("sortType")
|
||||
if sortType == "2" {
|
||||
cmd += "--sort -pmem "
|
||||
} else {
|
||||
cmd += "--sort -pcpu "
|
||||
}
|
||||
|
||||
pname := g.Query("name")
|
||||
pname := rc.F.Query("name")
|
||||
if pname != "" {
|
||||
cmd += fmt.Sprintf("| grep %s ", pname)
|
||||
}
|
||||
|
||||
count := ginx.QueryInt(g, "count", 10)
|
||||
count := rc.F.QueryIntDefault("count", 10)
|
||||
cmd += "| head -n " + fmt.Sprintf("%d", count)
|
||||
|
||||
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
|
||||
cli, err := m.MachineApp.GetCli(GetMachineId(rc))
|
||||
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
||||
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.GetLoginAccount().Id, cli.Info.TagPath...), "%s")
|
||||
|
||||
@@ -137,10 +134,10 @@ func (m *Machine) GetProcess(rc *req.Ctx) {
|
||||
|
||||
// 终止进程
|
||||
func (m *Machine) KillProcess(rc *req.Ctx) {
|
||||
pid := rc.GinCtx.Query("pid")
|
||||
pid := rc.F.Query("pid")
|
||||
biz.NotEmpty(pid, "进程id不能为空")
|
||||
|
||||
cli, err := m.MachineApp.GetCli(GetMachineId(rc.GinCtx))
|
||||
cli, err := m.MachineApp.GetCli(GetMachineId(rc))
|
||||
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
||||
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.GetLoginAccount().Id, cli.Info.TagPath...), "%s")
|
||||
|
||||
@@ -167,13 +164,13 @@ func (m *Machine) WsSSH(g *gin.Context) {
|
||||
panic(errorx.NewBiz("\033[1;31m您没有权限操作该机器终端,请重新登录后再试~\033[0m"))
|
||||
}
|
||||
|
||||
cli, err := m.MachineApp.NewCli(GetMachineId(g))
|
||||
cli, err := m.MachineApp.NewCli(GetMachineId(rc))
|
||||
biz.ErrIsNilAppendErr(err, "获取客户端连接失败: %s")
|
||||
defer cli.Close()
|
||||
biz.ErrIsNilAppendErr(m.TagApp.CanAccess(rc.GetLoginAccount().Id, cli.Info.TagPath...), "%s")
|
||||
|
||||
cols := ginx.QueryInt(g, "cols", 80)
|
||||
rows := ginx.QueryInt(g, "rows", 32)
|
||||
cols := rc.F.QueryIntDefault("cols", 80)
|
||||
rows := rc.F.QueryIntDefault("rows", 32)
|
||||
|
||||
// 记录系统操作日志
|
||||
rc.WithLog(req.NewLogSave("机器-终端操作"))
|
||||
@@ -185,15 +182,14 @@ func (m *Machine) WsSSH(g *gin.Context) {
|
||||
}
|
||||
|
||||
func (m *Machine) MachineTermOpRecords(rc *req.Ctx) {
|
||||
mid := GetMachineId(rc.GinCtx)
|
||||
res, err := m.MachineTermOpApp.GetPageList(&entity.MachineTermOp{MachineId: mid}, ginx.GetPageParam(rc.GinCtx), new([]entity.MachineTermOp))
|
||||
mid := GetMachineId(rc)
|
||||
res, err := m.MachineTermOpApp.GetPageList(&entity.MachineTermOp{MachineId: mid}, rc.F.GetPageParam(), new([]entity.MachineTermOp))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (m *Machine) MachineTermOpRecord(rc *req.Ctx) {
|
||||
recId, _ := strconv.Atoi(rc.GinCtx.Param("recId"))
|
||||
termOp, err := m.MachineTermOpApp.GetById(new(entity.MachineTermOp), uint64(recId))
|
||||
termOp, err := m.MachineTermOpApp.GetById(new(entity.MachineTermOp), uint64(rc.F.PathParamInt("recId")))
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
bytes, err := os.ReadFile(path.Join(config.GetMachine().TerminalRecPath, termOp.RecordFilePath))
|
||||
@@ -201,8 +197,8 @@ func (m *Machine) MachineTermOpRecord(rc *req.Ctx) {
|
||||
rc.ResData = base64.StdEncoding.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
func GetMachineId(g *gin.Context) uint64 {
|
||||
machineId, _ := strconv.Atoi(g.Param("machineId"))
|
||||
func GetMachineId(rc *req.Ctx) uint64 {
|
||||
machineId, _ := strconv.Atoi(rc.F.PathParam("machineId"))
|
||||
biz.IsTrue(machineId != 0, "machineId错误")
|
||||
return uint64(machineId)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/scheduler"
|
||||
)
|
||||
@@ -19,7 +18,7 @@ type MachineCronJob struct {
|
||||
}
|
||||
|
||||
func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
|
||||
cond, pageParam := ginx.BindQueryAndPage(rc.GinCtx, new(entity.MachineCronJob))
|
||||
cond, pageParam := req.BindQueryAndPage(rc, new(entity.MachineCronJob))
|
||||
|
||||
vos := new([]*vo.MachineCronJobVO)
|
||||
pageRes, err := m.MachineCronJobApp.GetPageList(cond, pageParam, vos)
|
||||
@@ -33,7 +32,7 @@ func (m *MachineCronJob) MachineCronJobs(rc *req.Ctx) {
|
||||
|
||||
func (m *MachineCronJob) Save(rc *req.Ctx) {
|
||||
jobForm := new(form.MachineCronJobForm)
|
||||
mcj := ginx.BindJsonAndCopyTo[*entity.MachineCronJob](rc.GinCtx, jobForm, new(entity.MachineCronJob))
|
||||
mcj := req.BindJsonAndCopyTo[*entity.MachineCronJob](rc, jobForm, new(entity.MachineCronJob))
|
||||
rc.ReqParam = jobForm
|
||||
|
||||
cronJobId, err := m.MachineCronJobApp.SaveMachineCronJob(rc.MetaCtx, mcj)
|
||||
@@ -44,7 +43,7 @@ func (m *MachineCronJob) Save(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineCronJob) Delete(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "ids")
|
||||
idsStr := rc.F.PathParam("ids")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -56,21 +55,21 @@ func (m *MachineCronJob) Delete(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineCronJob) GetRelateMachineIds(rc *req.Ctx) {
|
||||
rc.ResData = m.MachineCronJobApp.GetRelateMachineIds(uint64(ginx.QueryInt(rc.GinCtx, "cronJobId", -1)))
|
||||
rc.ResData = m.MachineCronJobApp.GetRelateMachineIds(uint64(rc.F.QueryIntDefault("cronJobId", -1)))
|
||||
}
|
||||
|
||||
func (m *MachineCronJob) GetRelateCronJobIds(rc *req.Ctx) {
|
||||
rc.ResData = m.MachineCronJobApp.GetRelateMachineIds(uint64(ginx.QueryInt(rc.GinCtx, "machineId", -1)))
|
||||
rc.ResData = m.MachineCronJobApp.GetRelateMachineIds(uint64(rc.F.QueryIntDefault("machineId", -1)))
|
||||
}
|
||||
|
||||
func (m *MachineCronJob) RunCronJob(rc *req.Ctx) {
|
||||
cronJobKey := ginx.PathParam(rc.GinCtx, "key")
|
||||
cronJobKey := rc.F.PathParam("key")
|
||||
biz.NotEmpty(cronJobKey, "cronJob key不能为空")
|
||||
m.MachineCronJobApp.RunCronJob(cronJobKey)
|
||||
}
|
||||
|
||||
func (m *MachineCronJob) CronJobExecs(rc *req.Ctx) {
|
||||
cond, pageParam := ginx.BindQueryAndPage[*entity.MachineCronJobExec](rc.GinCtx, new(entity.MachineCronJobExec))
|
||||
cond, pageParam := req.BindQueryAndPage[*entity.MachineCronJobExec](rc, new(entity.MachineCronJobExec))
|
||||
res, err := m.MachineCronJobApp.GetExecPageList(cond, pageParam, new([]entity.MachineCronJobExec))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
msgdto "mayfly-go/internal/msg/application/dto"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/logx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/anyx"
|
||||
@@ -23,11 +22,8 @@ import (
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MachineFile struct {
|
||||
@@ -43,32 +39,30 @@ const (
|
||||
)
|
||||
|
||||
func (m *MachineFile) MachineFiles(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
condition := &entity.MachineFile{MachineId: GetMachineId(g)}
|
||||
res, err := m.MachineFileApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineFileVO))
|
||||
condition := &entity.MachineFile{MachineId: GetMachineId(rc)}
|
||||
res, err := m.MachineFileApp.GetPageList(condition, rc.F.GetPageParam(), new([]vo.MachineFileVO))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (m *MachineFile) SaveMachineFiles(rc *req.Ctx) {
|
||||
fileForm := new(form.MachineFileForm)
|
||||
entity := ginx.BindJsonAndCopyTo[*entity.MachineFile](rc.GinCtx, fileForm, new(entity.MachineFile))
|
||||
entity := req.BindJsonAndCopyTo[*entity.MachineFile](rc, fileForm, new(entity.MachineFile))
|
||||
|
||||
rc.ReqParam = fileForm
|
||||
biz.ErrIsNil(m.MachineFileApp.Save(rc.MetaCtx, entity))
|
||||
}
|
||||
|
||||
func (m *MachineFile) DeleteFile(rc *req.Ctx) {
|
||||
biz.ErrIsNil(m.MachineFileApp.DeleteById(rc.MetaCtx, GetMachineFileId(rc.GinCtx)))
|
||||
biz.ErrIsNil(m.MachineFileApp.DeleteById(rc.MetaCtx, GetMachineFileId(rc)))
|
||||
}
|
||||
|
||||
/*** sftp相关操作 */
|
||||
|
||||
func (m *MachineFile) CreateFile(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
fid := GetMachineFileId(rc)
|
||||
|
||||
form := ginx.BindJsonAndValid(g, new(form.MachineCreateFileForm))
|
||||
form := req.BindJsonAndValid(rc, new(form.MachineCreateFileForm))
|
||||
path := form.Path
|
||||
|
||||
attrs := collx.Kvs("path", path)
|
||||
@@ -87,9 +81,8 @@ func (m *MachineFile) CreateFile(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
readPath := g.Query("path")
|
||||
fid := GetMachineFileId(rc)
|
||||
readPath := rc.F.Query("path")
|
||||
|
||||
sftpFile, mi, err := m.MachineFileApp.ReadFile(fid, readPath)
|
||||
rc.ReqParam = collx.Kvs("machine", mi, "path", readPath)
|
||||
@@ -107,9 +100,8 @@ func (m *MachineFile) ReadFileContent(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) DownloadFile(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
readPath := g.Query("path")
|
||||
fid := GetMachineFileId(rc)
|
||||
readPath := rc.F.Query("path")
|
||||
|
||||
sftpFile, mi, err := m.MachineFileApp.ReadFile(fid, readPath)
|
||||
rc.ReqParam = collx.Kvs("machine", mi, "path", readPath)
|
||||
@@ -118,13 +110,12 @@ func (m *MachineFile) DownloadFile(rc *req.Ctx) {
|
||||
|
||||
// 截取文件名,如/usr/local/test.java -》 test.java
|
||||
path := strings.Split(readPath, "/")
|
||||
rc.Download(sftpFile, path[len(path)-1])
|
||||
rc.F.Download(sftpFile, path[len(path)-1])
|
||||
}
|
||||
|
||||
func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
readPath := g.Query("path")
|
||||
fid := GetMachineFileId(rc)
|
||||
readPath := rc.F.Query("path")
|
||||
rc.ReqParam = fmt.Sprintf("path: %s", readPath)
|
||||
|
||||
if !strings.HasSuffix(readPath, "/") {
|
||||
@@ -150,9 +141,8 @@ func (m *MachineFile) GetDirEntry(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) GetDirSize(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
readPath := g.Query("path")
|
||||
fid := GetMachineFileId(rc)
|
||||
readPath := rc.F.Query("path")
|
||||
|
||||
size, err := m.MachineFileApp.GetDirSize(fid, readPath)
|
||||
biz.ErrIsNil(err)
|
||||
@@ -160,9 +150,8 @@ func (m *MachineFile) GetDirSize(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) GetFileStat(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
readPath := g.Query("path")
|
||||
fid := GetMachineFileId(rc)
|
||||
readPath := rc.F.Query("path")
|
||||
|
||||
res, err := m.MachineFileApp.FileStat(fid, readPath)
|
||||
biz.ErrIsNil(err, res)
|
||||
@@ -170,11 +159,9 @@ func (m *MachineFile) GetFileStat(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
fid := GetMachineFileId(rc)
|
||||
|
||||
form := new(form.MachineFileUpdateForm)
|
||||
ginx.BindJsonAndValid(g, form)
|
||||
form := req.BindJsonAndValid(rc, new(form.MachineFileUpdateForm))
|
||||
path := form.Path
|
||||
|
||||
mi, err := m.MachineFileApp.WriteFileContent(fid, path, []byte(form.Content))
|
||||
@@ -183,11 +170,10 @@ func (m *MachineFile) WriteFileContent(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) UploadFile(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
path := g.PostForm("path")
|
||||
fid := GetMachineFileId(rc)
|
||||
path := rc.F.PostForm("path")
|
||||
|
||||
fileheader, err := g.FormFile("file")
|
||||
fileheader, err := rc.F.FormFile("file")
|
||||
biz.ErrIsNilAppendErr(err, "读取文件失败: %s")
|
||||
|
||||
maxUploadFileSize := config.GetMachine().UploadMaxFileSize
|
||||
@@ -217,10 +203,9 @@ type FolderFile struct {
|
||||
}
|
||||
|
||||
func (m *MachineFile) UploadFolder(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
fid := GetMachineFileId(rc)
|
||||
|
||||
mf, err := g.MultipartForm()
|
||||
mf, err := rc.F.MultipartForm()
|
||||
biz.ErrIsNilAppendErr(err, "获取表单信息失败: %s")
|
||||
basePath := mf.Value["basePath"][0]
|
||||
biz.NotEmpty(basePath, "基础路径不能为空")
|
||||
@@ -311,11 +296,8 @@ func (m *MachineFile) UploadFolder(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) RemoveFile(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
|
||||
rmForm := new(form.MachineFileOpForm)
|
||||
ginx.BindJsonAndValid(g, rmForm)
|
||||
fid := GetMachineFileId(rc)
|
||||
rmForm := req.BindJsonAndValid(rc, new(form.MachineFileOpForm))
|
||||
|
||||
mi, err := m.MachineFileApp.RemoveFile(fid, rmForm.Path...)
|
||||
rc.ReqParam = collx.Kvs("machine", mi, "path", rmForm.Path)
|
||||
@@ -323,33 +305,27 @@ func (m *MachineFile) RemoveFile(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineFile) CopyFile(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
fid := GetMachineFileId(rc)
|
||||
cpForm := req.BindJsonAndValid(rc, new(form.MachineFileOpForm))
|
||||
|
||||
cpForm := new(form.MachineFileOpForm)
|
||||
ginx.BindJsonAndValid(g, cpForm)
|
||||
mi, err := m.MachineFileApp.Copy(fid, cpForm.ToPath, cpForm.Path...)
|
||||
biz.ErrIsNilAppendErr(err, "文件拷贝失败: %s")
|
||||
rc.ReqParam = collx.Kvs("machine", mi, "cp", cpForm)
|
||||
}
|
||||
|
||||
func (m *MachineFile) MvFile(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
fid := GetMachineFileId(rc)
|
||||
cpForm := req.BindJsonAndValid(rc, new(form.MachineFileOpForm))
|
||||
|
||||
cpForm := new(form.MachineFileOpForm)
|
||||
ginx.BindJsonAndValid(g, cpForm)
|
||||
mi, err := m.MachineFileApp.Mv(fid, cpForm.ToPath, cpForm.Path...)
|
||||
rc.ReqParam = collx.Kvs("machine", mi, "mv", cpForm)
|
||||
biz.ErrIsNilAppendErr(err, "文件移动失败: %s")
|
||||
}
|
||||
|
||||
func (m *MachineFile) Rename(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
fid := GetMachineFileId(g)
|
||||
fid := GetMachineFileId(rc)
|
||||
rename := req.BindJsonAndValid(rc, new(form.MachineFileRename))
|
||||
|
||||
rename := new(form.MachineFileRename)
|
||||
ginx.BindJsonAndValid(g, rename)
|
||||
mi, err := m.MachineFileApp.Rename(fid, rename.Oldname, rename.Newname)
|
||||
rc.ReqParam = collx.Kvs("machine", mi, "rename", rename)
|
||||
biz.ErrIsNilAppendErr(err, "文件重命名失败: %s")
|
||||
@@ -365,8 +341,8 @@ func getFileType(fm fs.FileMode) string {
|
||||
return dir
|
||||
}
|
||||
|
||||
func GetMachineFileId(g *gin.Context) uint64 {
|
||||
fileId, _ := strconv.Atoi(g.Param("fileId"))
|
||||
func GetMachineFileId(rc *req.Ctx) uint64 {
|
||||
fileId := rc.F.PathParamInt("fileId")
|
||||
biz.IsTrue(fileId != 0, "fileId错误")
|
||||
return uint64(fileId)
|
||||
}
|
||||
|
||||
@@ -7,15 +7,12 @@ import (
|
||||
"mayfly-go/internal/machine/domain/entity"
|
||||
tagapp "mayfly-go/internal/tag/application"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"mayfly-go/pkg/utils/jsonx"
|
||||
"mayfly-go/pkg/utils/stringx"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MachineScript struct {
|
||||
@@ -25,23 +22,22 @@ type MachineScript struct {
|
||||
}
|
||||
|
||||
func (m *MachineScript) MachineScripts(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
condition := &entity.MachineScript{MachineId: GetMachineId(g)}
|
||||
res, err := m.MachineScriptApp.GetPageList(condition, ginx.GetPageParam(g), new([]vo.MachineScriptVO))
|
||||
condition := &entity.MachineScript{MachineId: GetMachineId(rc)}
|
||||
res, err := m.MachineScriptApp.GetPageList(condition, rc.F.GetPageParam(), new([]vo.MachineScriptVO))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (m *MachineScript) SaveMachineScript(rc *req.Ctx) {
|
||||
form := new(form.MachineScriptForm)
|
||||
machineScript := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.MachineScript))
|
||||
machineScript := req.BindJsonAndCopyTo(rc, form, new(entity.MachineScript))
|
||||
|
||||
rc.ReqParam = form
|
||||
biz.ErrIsNil(m.MachineScriptApp.Save(rc.MetaCtx, machineScript))
|
||||
}
|
||||
|
||||
func (m *MachineScript) DeleteMachineScript(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "scriptId")
|
||||
idsStr := rc.F.PathParam("scriptId")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -53,17 +49,15 @@ func (m *MachineScript) DeleteMachineScript(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
|
||||
scriptId := GetMachineScriptId(g)
|
||||
machineId := GetMachineId(g)
|
||||
scriptId := GetMachineScriptId(rc)
|
||||
machineId := GetMachineId(rc)
|
||||
ms, err := m.MachineScriptApp.GetById(new(entity.MachineScript), scriptId, "MachineId", "Name", "Script")
|
||||
biz.ErrIsNil(err, "该脚本不存在")
|
||||
biz.IsTrue(ms.MachineId == application.Common_Script_Machine_Id || ms.MachineId == machineId, "该脚本不属于该机器")
|
||||
|
||||
script := ms.Script
|
||||
// 如果有脚本参数,则用脚本参数替换脚本中的模板占位符参数
|
||||
if params := g.Query("params"); params != "" {
|
||||
if params := rc.F.Query("params"); params != "" {
|
||||
script, err = stringx.TemplateParse(ms.Script, jsonx.ToMap(params))
|
||||
biz.ErrIsNilAppendErr(err, "脚本模板参数解析失败: %s")
|
||||
}
|
||||
@@ -80,8 +74,8 @@ func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func GetMachineScriptId(g *gin.Context) uint64 {
|
||||
scriptId, _ := strconv.Atoi(g.Param("scriptId"))
|
||||
func GetMachineScriptId(rc *req.Ctx) uint64 {
|
||||
scriptId := rc.F.PathParamInt("scriptId")
|
||||
biz.IsTrue(scriptId > 0, "scriptId错误")
|
||||
return uint64(scriptId)
|
||||
}
|
||||
|
||||
@@ -63,6 +63,9 @@ func checkClientAvailability(interval time.Duration) {
|
||||
// 遍历所有机器连接实例,若存在机器连接实例使用该ssh隧道机器,则返回true,表示还在使用中...
|
||||
items := cliCache.Items()
|
||||
for _, v := range items {
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
cli := v.Value.(*Cli)
|
||||
if _, _, err := cli.sshClient.Conn.SendRequest("ping", true, nil); err != nil {
|
||||
logx.Errorf("machine[%s] cache client is not available: %s", cli.Info.Name, err.Error())
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"mayfly-go/internal/mongo/domain/entity"
|
||||
tagapp "mayfly-go/internal/tag/application"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
@@ -16,7 +15,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
@@ -28,7 +26,7 @@ type Mongo struct {
|
||||
}
|
||||
|
||||
func (m *Mongo) Mongos(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.MongoQuery](rc.GinCtx, new(entity.MongoQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.MongoQuery](rc, new(entity.MongoQuery))
|
||||
|
||||
// 不存在可访问标签id,即没有可操作数据
|
||||
codes := m.TagApp.GetAccountResourceCodes(rc.GetLoginAccount().Id, consts.TagResourceTypeMongo, queryCond.TagPath)
|
||||
@@ -45,13 +43,13 @@ func (m *Mongo) Mongos(rc *req.Ctx) {
|
||||
|
||||
func (m *Mongo) TestConn(rc *req.Ctx) {
|
||||
form := &form.Mongo{}
|
||||
mongo := ginx.BindJsonAndCopyTo[*entity.Mongo](rc.GinCtx, form, new(entity.Mongo))
|
||||
mongo := req.BindJsonAndCopyTo[*entity.Mongo](rc, form, new(entity.Mongo))
|
||||
biz.ErrIsNilAppendErr(m.MongoApp.TestConn(mongo), "连接失败: %s")
|
||||
}
|
||||
|
||||
func (m *Mongo) Save(rc *req.Ctx) {
|
||||
form := &form.Mongo{}
|
||||
mongo := ginx.BindJsonAndCopyTo[*entity.Mongo](rc.GinCtx, form, new(entity.Mongo))
|
||||
mongo := req.BindJsonAndCopyTo[*entity.Mongo](rc, form, new(entity.Mongo))
|
||||
|
||||
// 密码脱敏记录日志
|
||||
form.Uri = func(str string) string {
|
||||
@@ -64,7 +62,7 @@ func (m *Mongo) Save(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Mongo) DeleteMongo(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "id")
|
||||
idsStr := rc.F.PathParam("id")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -76,7 +74,7 @@ func (m *Mongo) DeleteMongo(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Mongo) Databases(rc *req.Ctx) {
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc.GinCtx))
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
||||
biz.ErrIsNil(err)
|
||||
res, err := conn.Cli.ListDatabases(context.TODO(), bson.D{})
|
||||
biz.ErrIsNilAppendErr(err, "获取mongo所有库信息失败: %s")
|
||||
@@ -84,9 +82,9 @@ func (m *Mongo) Databases(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Mongo) Collections(rc *req.Ctx) {
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc.GinCtx))
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
||||
biz.ErrIsNil(err)
|
||||
db := rc.GinCtx.Query("database")
|
||||
db := rc.F.Query("database")
|
||||
biz.NotEmpty(db, "database不能为空")
|
||||
ctx := context.TODO()
|
||||
res, err := conn.Cli.Database(db).ListCollectionNames(ctx, bson.D{})
|
||||
@@ -96,9 +94,9 @@ func (m *Mongo) Collections(rc *req.Ctx) {
|
||||
|
||||
func (m *Mongo) RunCommand(rc *req.Ctx) {
|
||||
commandForm := new(form.MongoRunCommand)
|
||||
ginx.BindJsonAndValid(rc.GinCtx, commandForm)
|
||||
req.BindJsonAndValid(rc, commandForm)
|
||||
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc.GinCtx))
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
||||
|
||||
@@ -125,11 +123,9 @@ func (m *Mongo) RunCommand(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Mongo) FindCommand(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
commandForm := new(form.MongoFindCommand)
|
||||
ginx.BindJsonAndValid(g, commandForm)
|
||||
commandForm := req.BindJsonAndValid(rc, new(form.MongoFindCommand))
|
||||
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(g))
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
||||
biz.ErrIsNil(err)
|
||||
cli := conn.Cli
|
||||
|
||||
@@ -161,11 +157,9 @@ func (m *Mongo) FindCommand(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
commandForm := new(form.MongoUpdateByIdCommand)
|
||||
ginx.BindJsonAndValid(g, commandForm)
|
||||
commandForm := req.BindJsonAndValid(rc, new(form.MongoUpdateByIdCommand))
|
||||
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(g))
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
||||
|
||||
@@ -186,11 +180,9 @@ func (m *Mongo) UpdateByIdCommand(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
commandForm := new(form.MongoUpdateByIdCommand)
|
||||
ginx.BindJsonAndValid(g, commandForm)
|
||||
commandForm := req.BindJsonAndValid(rc, new(form.MongoUpdateByIdCommand))
|
||||
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(g))
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
||||
|
||||
@@ -210,11 +202,9 @@ func (m *Mongo) DeleteByIdCommand(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Mongo) InsertOneCommand(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
commandForm := new(form.MongoInsertCommand)
|
||||
ginx.BindJsonAndValid(g, commandForm)
|
||||
commandForm := req.BindJsonAndValid(rc, new(form.MongoInsertCommand))
|
||||
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(g))
|
||||
conn, err := m.MongoApp.GetMongoConn(m.GetMongoId(rc))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ReqParam = collx.Kvs("mongo", conn.Info, "cmd", commandForm)
|
||||
|
||||
@@ -224,8 +214,8 @@ func (m *Mongo) InsertOneCommand(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
// 获取请求路径上的mongo id
|
||||
func (m *Mongo) GetMongoId(g *gin.Context) uint64 {
|
||||
dbId, _ := strconv.Atoi(g.Param("id"))
|
||||
func (m *Mongo) GetMongoId(rc *req.Ctx) uint64 {
|
||||
dbId := rc.F.PathParamInt("id")
|
||||
biz.IsTrue(dbId > 0, "mongoId错误")
|
||||
return uint64(dbId)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"mayfly-go/internal/msg/application"
|
||||
"mayfly-go/internal/msg/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
)
|
||||
|
||||
@@ -17,7 +16,7 @@ func (m *Msg) GetMsgs(rc *req.Ctx) {
|
||||
condition := &entity.Msg{
|
||||
RecipientId: int64(rc.GetLoginAccount().Id),
|
||||
}
|
||||
res, err := m.MsgApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]entity.Msg))
|
||||
res, err := m.MsgApp.GetPageList(condition, rc.F.GetPageParam(), new([]entity.Msg))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"mayfly-go/internal/redis/api/form"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"time"
|
||||
@@ -12,10 +11,9 @@ import (
|
||||
|
||||
func (r *Redis) Hscan(rc *req.Ctx) {
|
||||
ri, key := r.checkKeyAndGetRedisConn(rc)
|
||||
g := rc.GinCtx
|
||||
count := ginx.QueryInt(g, "count", 10)
|
||||
match := g.Query("match")
|
||||
cursor := ginx.QueryInt(g, "cursor", 0)
|
||||
count := rc.F.QueryIntDefault("count", 10)
|
||||
match := rc.F.Query("match")
|
||||
cursor := rc.F.QueryIntDefault("cursor", 0)
|
||||
contextTodo := context.TODO()
|
||||
|
||||
cmdable := ri.GetCmdable()
|
||||
@@ -33,7 +31,7 @@ func (r *Redis) Hscan(rc *req.Ctx) {
|
||||
|
||||
func (r *Redis) Hdel(rc *req.Ctx) {
|
||||
ri, key := r.checkKeyAndGetRedisConn(rc)
|
||||
field := rc.GinCtx.Query("field")
|
||||
field := rc.F.Query("field")
|
||||
|
||||
rc.ReqParam = collx.Kvs("redis", ri.Info, "key", key, "field", field)
|
||||
delRes, err := ri.GetCmdable().HDel(context.TODO(), key, field).Result()
|
||||
@@ -43,7 +41,7 @@ func (r *Redis) Hdel(rc *req.Ctx) {
|
||||
|
||||
func (r *Redis) Hget(rc *req.Ctx) {
|
||||
ri, key := r.checkKeyAndGetRedisConn(rc)
|
||||
field := rc.GinCtx.Query("field")
|
||||
field := rc.F.Query("field")
|
||||
|
||||
res, err := ri.GetCmdable().HGet(context.TODO(), key, field).Result()
|
||||
biz.ErrIsNilAppendErr(err, "hget err: %s")
|
||||
@@ -51,9 +49,7 @@ func (r *Redis) Hget(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) Hset(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
hashValue := new(form.HashValue)
|
||||
ginx.BindJsonAndValid(g, hashValue)
|
||||
hashValue := req.BindJsonAndValid(rc, new(form.HashValue))
|
||||
|
||||
hv := hashValue.Value[0]
|
||||
ri := r.getRedisConn(rc)
|
||||
@@ -65,9 +61,7 @@ func (r *Redis) Hset(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) SaveHashValue(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
hashValue := new(form.HashValue)
|
||||
ginx.BindJsonAndValid(g, hashValue)
|
||||
hashValue := req.BindJsonAndValid(rc, new(form.HashValue))
|
||||
|
||||
ri := r.getRedisConn(rc)
|
||||
rc.ReqParam = collx.Kvs("redis", ri.Info, "hash", hashValue)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"mayfly-go/internal/redis/api/vo"
|
||||
"mayfly-go/internal/redis/rdm"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"strings"
|
||||
@@ -20,8 +19,7 @@ import (
|
||||
func (r *Redis) ScanKeys(rc *req.Ctx) {
|
||||
ri := r.getRedisConn(rc)
|
||||
|
||||
form := &form.RedisScanForm{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, form)
|
||||
form := req.BindJsonAndValid(rc, new(form.RedisScanForm))
|
||||
|
||||
cmd := ri.GetCmdable()
|
||||
ctx := context.Background()
|
||||
@@ -143,8 +141,7 @@ func (r *Redis) DeleteKey(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) RenameKey(rc *req.Ctx) {
|
||||
form := &form.Rename{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, form)
|
||||
form := req.BindJsonAndValid(rc, new(form.Rename))
|
||||
|
||||
ri := r.getRedisConn(rc)
|
||||
rc.ReqParam = collx.Kvs("redis", ri.Info, "rename", form)
|
||||
@@ -152,8 +149,7 @@ func (r *Redis) RenameKey(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) ExpireKey(rc *req.Ctx) {
|
||||
form := &form.Expire{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, form)
|
||||
form := req.BindJsonAndValid(rc, new(form.Expire))
|
||||
|
||||
ri := r.getRedisConn(rc)
|
||||
rc.ReqParam = collx.Kvs("redis", ri.Info, "expire", form)
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"mayfly-go/internal/redis/api/form"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
)
|
||||
@@ -17,9 +16,8 @@ func (r *Redis) GetListValue(rc *req.Ctx) {
|
||||
len, err := cmdable.LLen(ctx, key).Result()
|
||||
biz.ErrIsNilAppendErr(err, "获取list长度失败: %s")
|
||||
|
||||
g := rc.GinCtx
|
||||
start := ginx.QueryInt(g, "start", 0)
|
||||
stop := ginx.QueryInt(g, "stop", 10)
|
||||
start := rc.F.QueryIntDefault("start", 0)
|
||||
stop := rc.F.QueryIntDefault("stop", 10)
|
||||
res, err := cmdable.LRange(ctx, key, int64(start), int64(stop)).Result()
|
||||
biz.ErrIsNilAppendErr(err, "获取list值失败: %s")
|
||||
|
||||
@@ -30,9 +28,7 @@ func (r *Redis) GetListValue(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) Lrem(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
option := new(form.LRemOption)
|
||||
ginx.BindJsonAndValid(g, option)
|
||||
option := req.BindJsonAndValid(rc, new(form.LRemOption))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
res, err := cmd.LRem(context.TODO(), option.Key, int64(option.Count), option.Member).Result()
|
||||
@@ -41,9 +37,7 @@ func (r *Redis) Lrem(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) SaveListValue(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
listValue := new(form.ListValue)
|
||||
ginx.BindJsonAndValid(g, listValue)
|
||||
listValue := req.BindJsonAndValid(rc, new(form.ListValue))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
|
||||
@@ -55,9 +49,7 @@ func (r *Redis) SaveListValue(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) Lset(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
listSetValue := new(form.ListSetValue)
|
||||
ginx.BindJsonAndValid(g, listSetValue)
|
||||
listSetValue := req.BindJsonAndValid(rc, new(form.ListSetValue))
|
||||
|
||||
ri := r.getRedisConn(rc)
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"mayfly-go/internal/redis/rdm"
|
||||
tagapp "mayfly-go/internal/tag/application"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
@@ -19,7 +18,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
@@ -29,7 +27,7 @@ type Redis struct {
|
||||
}
|
||||
|
||||
func (r *Redis) RedisList(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.RedisQuery](rc.GinCtx, new(entity.RedisQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.RedisQuery](rc, new(entity.RedisQuery))
|
||||
|
||||
// 不存在可访问标签id,即没有可操作数据
|
||||
codes := r.TagApp.GetAccountResourceCodes(rc.GetLoginAccount().Id, consts.TagResourceTypeRedis, queryCond.TagPath)
|
||||
@@ -46,7 +44,7 @@ func (r *Redis) RedisList(rc *req.Ctx) {
|
||||
|
||||
func (r *Redis) TestConn(rc *req.Ctx) {
|
||||
form := &form.Redis{}
|
||||
redis := ginx.BindJsonAndCopyTo[*entity.Redis](rc.GinCtx, form, new(entity.Redis))
|
||||
redis := req.BindJsonAndCopyTo[*entity.Redis](rc, form, new(entity.Redis))
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(redis.Password, true)
|
||||
@@ -58,7 +56,7 @@ func (r *Redis) TestConn(rc *req.Ctx) {
|
||||
|
||||
func (r *Redis) Save(rc *req.Ctx) {
|
||||
form := &form.Redis{}
|
||||
redis := ginx.BindJsonAndCopyTo[*entity.Redis](rc.GinCtx, form, new(entity.Redis))
|
||||
redis := req.BindJsonAndCopyTo[*entity.Redis](rc, form, new(entity.Redis))
|
||||
|
||||
// 密码解密,并使用解密后的赋值
|
||||
originPwd, err := cryptox.DefaultRsaDecrypt(redis.Password, true)
|
||||
@@ -74,7 +72,7 @@ func (r *Redis) Save(rc *req.Ctx) {
|
||||
|
||||
// 获取redis实例密码,由于数据库是加密存储,故提供该接口展示原文密码
|
||||
func (r *Redis) GetRedisPwd(rc *req.Ctx) {
|
||||
rid := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
|
||||
rid := uint64(rc.F.PathParamInt("id"))
|
||||
re, err := r.RedisApp.GetById(new(entity.Redis), rid, "Password")
|
||||
biz.ErrIsNil(err, "redis信息不存在")
|
||||
if err := re.PwdDecrypt(); err != nil {
|
||||
@@ -84,7 +82,7 @@ func (r *Redis) GetRedisPwd(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) DeleteRedis(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "id")
|
||||
idsStr := rc.F.PathParam("id")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -96,11 +94,10 @@ func (r *Redis) DeleteRedis(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) RedisInfo(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
ri, err := r.RedisApp.GetRedisConn(uint64(ginx.PathParamInt(g, "id")), 0)
|
||||
ri, err := r.RedisApp.GetRedisConn(uint64(rc.F.PathParamInt("id")), 0)
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
section := rc.GinCtx.Query("section")
|
||||
section := rc.F.Query("section")
|
||||
mode := ri.Info.Mode
|
||||
ctx := context.Background()
|
||||
var redisCli *redis.Client
|
||||
@@ -108,7 +105,7 @@ func (r *Redis) RedisInfo(rc *req.Ctx) {
|
||||
if mode == "" || mode == rdm.StandaloneMode || mode == rdm.SentinelMode {
|
||||
redisCli = ri.Cli
|
||||
} else if mode == rdm.ClusterMode {
|
||||
host := rc.GinCtx.Query("host")
|
||||
host := rc.F.Query("host")
|
||||
biz.NotEmpty(host, "集群模式host信息不能为空")
|
||||
clusterClient := ri.ClusterCli
|
||||
// 遍历集群的master节点找到该redis client
|
||||
@@ -174,8 +171,7 @@ func (r *Redis) RedisInfo(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) ClusterInfo(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
ri, err := r.RedisApp.GetRedisConn(uint64(ginx.PathParamInt(g, "id")), 0)
|
||||
ri, err := r.RedisApp.GetRedisConn(uint64(rc.F.PathParamInt("id")), 0)
|
||||
biz.ErrIsNil(err)
|
||||
biz.IsEquals(ri.Info.Mode, rdm.ClusterMode, "非集群模式")
|
||||
info, _ := ri.ClusterCli.ClusterInfo(context.Background()).Result()
|
||||
@@ -220,19 +216,19 @@ func (r *Redis) ClusterInfo(rc *req.Ctx) {
|
||||
|
||||
// 校验查询参数中的key为必填项,并返回redis实例
|
||||
func (r *Redis) checkKeyAndGetRedisConn(rc *req.Ctx) (*rdm.RedisConn, string) {
|
||||
key := rc.GinCtx.Query("key")
|
||||
key := rc.F.Query("key")
|
||||
biz.NotEmpty(key, "key不能为空")
|
||||
return r.getRedisConn(rc), key
|
||||
}
|
||||
|
||||
func (r *Redis) getRedisConn(rc *req.Ctx) *rdm.RedisConn {
|
||||
ri, err := r.RedisApp.GetRedisConn(getIdAndDbNum(rc.GinCtx))
|
||||
ri, err := r.RedisApp.GetRedisConn(getIdAndDbNum(rc))
|
||||
biz.ErrIsNil(err)
|
||||
biz.ErrIsNilAppendErr(r.TagApp.CanAccess(rc.GetLoginAccount().Id, ri.Info.TagPath...), "%s")
|
||||
return ri
|
||||
}
|
||||
|
||||
// 获取redis id与要操作的库号(统一路径)
|
||||
func getIdAndDbNum(g *gin.Context) (uint64, int) {
|
||||
return uint64(ginx.PathParamInt(g, "id")), ginx.PathParamInt(g, "db")
|
||||
func getIdAndDbNum(rc *req.Ctx) (uint64, int) {
|
||||
return uint64(rc.F.PathParamInt("id")), rc.F.PathParamInt("db")
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"mayfly-go/internal/redis/api/form"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"time"
|
||||
@@ -18,9 +17,7 @@ func (r *Redis) GetSetValue(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) SaveSetValue(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
keyvalue := new(form.SetValue)
|
||||
ginx.BindJsonAndValid(g, keyvalue)
|
||||
keyvalue := req.BindJsonAndValid(rc, new(form.SetValue))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
|
||||
@@ -43,9 +40,7 @@ func (r *Redis) Scard(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) Sscan(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
scan := new(form.ScanForm)
|
||||
ginx.BindJsonAndValid(g, scan)
|
||||
scan := req.BindJsonAndValid(rc, new(form.ScanForm))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
keys, cursor, err := cmd.SScan(context.TODO(), scan.Key, scan.Cursor, scan.Match, scan.Count).Result()
|
||||
@@ -57,9 +52,8 @@ func (r *Redis) Sscan(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) Sadd(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
option := new(form.SmemberOption)
|
||||
ginx.BindJsonAndValid(g, option)
|
||||
option := req.BindJsonAndValid(rc, new(form.SmemberOption))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
|
||||
res, err := cmd.SAdd(context.TODO(), option.Key, option.Member).Result()
|
||||
@@ -68,9 +62,7 @@ func (r *Redis) Sadd(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) Srem(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
option := new(form.SmemberOption)
|
||||
ginx.BindJsonAndValid(g, option)
|
||||
option := req.BindJsonAndValid(rc, new(form.SmemberOption))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
res, err := cmd.SRem(context.TODO(), option.Key, option.Member).Result()
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"mayfly-go/internal/redis/api/form"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"time"
|
||||
@@ -18,9 +17,7 @@ func (r *Redis) GetStringValue(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) SaveStringValue(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
keyValue := new(form.StringValue)
|
||||
ginx.BindJsonAndValid(g, keyValue)
|
||||
keyValue := req.BindJsonAndValid(rc, new(form.StringValue))
|
||||
|
||||
ri := r.getRedisConn(rc)
|
||||
cmd := ri.GetCmdable()
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"mayfly-go/internal/redis/api/form"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
|
||||
@@ -20,12 +19,11 @@ func (r *Redis) ZCard(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) ZScan(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
ri, key := r.checkKeyAndGetRedisConn(rc)
|
||||
|
||||
cursor := uint64(ginx.QueryInt(g, "cursor", 0))
|
||||
match := ginx.Query(g, "match", "*")
|
||||
count := ginx.QueryInt(g, "count", 50)
|
||||
cursor := uint64(rc.F.QueryIntDefault("cursor", 0))
|
||||
match := rc.F.QueryDefault("match", "*")
|
||||
count := rc.F.QueryIntDefault("count", 50)
|
||||
|
||||
keys, cursor, err := ri.GetCmdable().ZScan(context.TODO(), key, cursor, match, int64(count)).Result()
|
||||
biz.ErrIsNilAppendErr(err, "sscan失败: %s")
|
||||
@@ -36,10 +34,9 @@ func (r *Redis) ZScan(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) ZRevRange(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
ri, key := r.checkKeyAndGetRedisConn(rc)
|
||||
start := ginx.QueryInt(g, "start", 0)
|
||||
stop := ginx.QueryInt(g, "stop", 50)
|
||||
start := rc.F.QueryIntDefault("start", 0)
|
||||
stop := rc.F.QueryIntDefault("stop", 50)
|
||||
|
||||
res, err := ri.GetCmdable().ZRevRangeWithScores(context.TODO(), key, int64(start), int64(stop)).Result()
|
||||
biz.ErrIsNilAppendErr(err, "ZRevRange失败: %s")
|
||||
@@ -47,9 +44,7 @@ func (r *Redis) ZRevRange(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) ZRem(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
option := new(form.SmemberOption)
|
||||
ginx.BindJsonAndValid(g, option)
|
||||
option := req.BindJsonAndValid(rc, new(form.SmemberOption))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
res, err := cmd.ZRem(context.TODO(), option.Key, option.Member).Result()
|
||||
@@ -58,9 +53,7 @@ func (r *Redis) ZRem(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Redis) ZAdd(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
option := new(form.ZAddOption)
|
||||
ginx.BindJsonAndValid(g, option)
|
||||
option := req.BindJsonAndValid(rc, new(form.ZAddOption))
|
||||
|
||||
cmd := r.getRedisConn(rc).GetCmdable()
|
||||
zm := redis.Z{
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/contextx"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
@@ -60,8 +59,7 @@ func (a *Account) GetPermissions(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (a *Account) ChangePassword(rc *req.Ctx) {
|
||||
form := new(form.AccountChangePasswordForm)
|
||||
ginx.BindJsonAndValid(rc.GinCtx, form)
|
||||
form := req.BindJsonAndValid(rc, new(form.AccountChangePasswordForm))
|
||||
|
||||
originOldPwd, err := cryptox.DefaultRsaDecrypt(form.OldPassword, true)
|
||||
biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
|
||||
@@ -98,7 +96,7 @@ func (a *Account) AccountInfo(rc *req.Ctx) {
|
||||
|
||||
// 更新个人账号信息
|
||||
func (a *Account) UpdateAccount(rc *req.Ctx) {
|
||||
updateAccount := ginx.BindJsonAndCopyTo[*entity.Account](rc.GinCtx, new(form.AccountUpdateForm), new(entity.Account))
|
||||
updateAccount := req.BindJsonAndCopyTo[*entity.Account](rc, new(form.AccountUpdateForm), new(entity.Account))
|
||||
// 账号id为登录者账号
|
||||
updateAccount.Id = rc.GetLoginAccount().Id
|
||||
|
||||
@@ -122,8 +120,8 @@ func (a *Account) UpdateAccount(rc *req.Ctx) {
|
||||
// @router /accounts [get]
|
||||
func (a *Account) Accounts(rc *req.Ctx) {
|
||||
condition := &entity.Account{}
|
||||
condition.Username = rc.GinCtx.Query("username")
|
||||
res, err := a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
|
||||
condition.Username = rc.F.Query("username")
|
||||
res, err := a.AccountApp.GetPageList(condition, rc.F.GetPageParam(), new([]vo.AccountManageVO))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
@@ -131,7 +129,7 @@ func (a *Account) Accounts(rc *req.Ctx) {
|
||||
// @router /accounts
|
||||
func (a *Account) SaveAccount(rc *req.Ctx) {
|
||||
form := &form.AccountCreateForm{}
|
||||
account := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Account))
|
||||
account := req.BindJsonAndCopyTo(rc, form, new(entity.Account))
|
||||
|
||||
form.Password = "*****"
|
||||
rc.ReqParam = form
|
||||
@@ -150,12 +148,10 @@ func (a *Account) SaveAccount(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (a *Account) ChangeStatus(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
|
||||
account := &entity.Account{}
|
||||
account.Id = uint64(ginx.PathParamInt(g, "id"))
|
||||
account.Id = uint64(rc.F.PathParamInt("id"))
|
||||
|
||||
status := entity.AccountStatus(int8(ginx.PathParamInt(g, "status")))
|
||||
status := entity.AccountStatus(int8(rc.F.PathParamInt("status")))
|
||||
biz.ErrIsNil(entity.AccountStatusEnum.Valid(status))
|
||||
account.Status = status
|
||||
|
||||
@@ -164,7 +160,7 @@ func (a *Account) ChangeStatus(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (a *Account) DeleteAccount(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "id")
|
||||
idsStr := rc.F.PathParam("id")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -177,7 +173,7 @@ func (a *Account) DeleteAccount(rc *req.Ctx) {
|
||||
|
||||
// 获取账号角色信息列表
|
||||
func (a *Account) AccountRoles(rc *req.Ctx) {
|
||||
rc.ResData = a.getAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
|
||||
rc.ResData = a.getAccountRoles(uint64(rc.F.PathParamInt("id")))
|
||||
}
|
||||
|
||||
func (a *Account) getAccountRoles(accountId uint64) []*vo.AccountRoleVO {
|
||||
@@ -221,23 +217,21 @@ func (a *Account) getAccountRoles(accountId uint64) []*vo.AccountRoleVO {
|
||||
func (a *Account) AccountResources(rc *req.Ctx) {
|
||||
var resources vo.ResourceManageVOList
|
||||
// 获取账号菜单资源
|
||||
biz.ErrIsNil(a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources))
|
||||
biz.ErrIsNil(a.ResourceApp.GetAccountResources(uint64(rc.F.PathParamInt("id")), &resources))
|
||||
rc.ResData = resources.ToTrees(0)
|
||||
}
|
||||
|
||||
// 关联账号角色
|
||||
func (a *Account) RelateRole(rc *req.Ctx) {
|
||||
var form form.AccountRoleForm
|
||||
ginx.BindJsonAndValid(rc.GinCtx, &form)
|
||||
form := req.BindJsonAndValid(rc, new(form.AccountRoleForm))
|
||||
rc.ReqParam = form
|
||||
|
||||
biz.ErrIsNil(a.RoleApp.RelateAccountRole(rc.MetaCtx, form.Id, form.RoleId, consts.AccountRoleRelateType(form.RelateType)))
|
||||
}
|
||||
|
||||
// 重置otp秘钥
|
||||
func (a *Account) ResetOtpSecret(rc *req.Ctx) {
|
||||
account := &entity.Account{OtpSecret: "-"}
|
||||
accountId := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
|
||||
accountId := uint64(rc.F.PathParamInt("id"))
|
||||
account.Id = accountId
|
||||
rc.ReqParam = collx.Kvs("accountId", accountId)
|
||||
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"mayfly-go/internal/sys/application"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
)
|
||||
|
||||
@@ -14,16 +13,15 @@ type Config struct {
|
||||
}
|
||||
|
||||
func (c *Config) Configs(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
condition := &entity.Config{Key: g.Query("key")}
|
||||
condition := &entity.Config{Key: rc.F.Query("key")}
|
||||
condition.Permission = rc.GetLoginAccount().Username
|
||||
res, err := c.ConfigApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Config))
|
||||
res, err := c.ConfigApp.GetPageList(condition, rc.F.GetPageParam(), new([]entity.Config))
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (c *Config) GetConfigValueByKey(rc *req.Ctx) {
|
||||
key := rc.GinCtx.Query("key")
|
||||
key := rc.F.Query("key")
|
||||
biz.NotEmpty(key, "key不能为空")
|
||||
|
||||
config := c.ConfigApp.GetConfig(key)
|
||||
@@ -38,7 +36,7 @@ func (c *Config) GetConfigValueByKey(rc *req.Ctx) {
|
||||
|
||||
func (c *Config) SaveConfig(rc *req.Ctx) {
|
||||
form := &form.ConfigForm{}
|
||||
config := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Config))
|
||||
config := req.BindJsonAndCopyTo(rc, form, new(entity.Config))
|
||||
rc.ReqParam = form
|
||||
biz.ErrIsNil(c.ConfigApp.Save(rc.MetaCtx, config))
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"mayfly-go/internal/sys/application"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
)
|
||||
@@ -23,15 +22,14 @@ func (r *Resource) GetAllResourceTree(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Resource) GetById(rc *req.Ctx) {
|
||||
res, err := r.ResourceApp.GetById(new(entity.Resource), uint64(ginx.PathParamInt(rc.GinCtx, "id")))
|
||||
res, err := r.ResourceApp.GetById(new(entity.Resource), uint64(rc.F.PathParamInt("id")))
|
||||
biz.ErrIsNil(err, "该资源不存在")
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
func (r *Resource) SaveResource(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
form := new(form.ResourceForm)
|
||||
entity := ginx.BindJsonAndCopyTo(g, form, new(entity.Resource))
|
||||
entity := req.BindJsonAndCopyTo(rc, form, new(entity.Resource))
|
||||
|
||||
rc.ReqParam = form
|
||||
|
||||
@@ -43,19 +41,19 @@ func (r *Resource) SaveResource(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (r *Resource) DelResource(rc *req.Ctx) {
|
||||
biz.ErrIsNil(r.ResourceApp.Delete(rc.MetaCtx, uint64(ginx.PathParamInt(rc.GinCtx, "id"))))
|
||||
biz.ErrIsNil(r.ResourceApp.Delete(rc.MetaCtx, uint64(rc.F.PathParamInt("id"))))
|
||||
}
|
||||
|
||||
func (r *Resource) ChangeStatus(rc *req.Ctx) {
|
||||
rid := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
|
||||
status := int8(ginx.PathParamInt(rc.GinCtx, "status"))
|
||||
rid := uint64(rc.F.PathParamInt("id"))
|
||||
status := int8(rc.F.PathParamInt("status"))
|
||||
rc.ReqParam = collx.Kvs("id", rid, "status", status)
|
||||
biz.ErrIsNil(r.ResourceApp.ChangeStatus(rc.MetaCtx, rid, status))
|
||||
}
|
||||
|
||||
func (r *Resource) Sort(rc *req.Ctx) {
|
||||
var rs []form.ResourceForm
|
||||
rc.GinCtx.ShouldBindJSON(&rs)
|
||||
rc.F.BindJSON(&rs)
|
||||
rc.ReqParam = rs
|
||||
|
||||
for _, v := range rs {
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"mayfly-go/internal/sys/application"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/anyx"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
@@ -20,10 +19,9 @@ type Role struct {
|
||||
}
|
||||
|
||||
func (r *Role) Roles(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
cond, pageParam := ginx.BindQueryAndPage(g, new(entity.RoleQuery))
|
||||
cond, pageParam := req.BindQueryAndPage(rc, new(entity.RoleQuery))
|
||||
|
||||
notIdsStr := g.Query("notIds")
|
||||
notIdsStr := rc.F.Query("notIds")
|
||||
if notIdsStr != "" {
|
||||
cond.NotIds = collx.ArrayMap[string, uint64](strings.Split(notIdsStr, ","), func(val string) uint64 {
|
||||
return uint64(anyx.ConvInt(val))
|
||||
@@ -38,7 +36,7 @@ func (r *Role) Roles(rc *req.Ctx) {
|
||||
// 保存角色信息
|
||||
func (r *Role) SaveRole(rc *req.Ctx) {
|
||||
form := &form.RoleForm{}
|
||||
role := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Role))
|
||||
role := req.BindJsonAndCopyTo(rc, form, new(entity.Role))
|
||||
rc.ReqParam = form
|
||||
|
||||
r.RoleApp.SaveRole(rc.MetaCtx, role)
|
||||
@@ -46,7 +44,7 @@ func (r *Role) SaveRole(rc *req.Ctx) {
|
||||
|
||||
// 删除角色及其资源关联关系
|
||||
func (r *Role) DelRole(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "id")
|
||||
idsStr := rc.F.PathParam("id")
|
||||
rc.ReqParam = collx.Kvs("ids", idsStr)
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -59,23 +57,20 @@ func (r *Role) DelRole(rc *req.Ctx) {
|
||||
|
||||
// 获取角色关联的资源id数组,用于分配资源时回显已拥有的资源
|
||||
func (r *Role) RoleResourceIds(rc *req.Ctx) {
|
||||
rc.ResData = r.RoleApp.GetRoleResourceIds(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
|
||||
rc.ResData = r.RoleApp.GetRoleResourceIds(uint64(rc.F.PathParamInt("id")))
|
||||
}
|
||||
|
||||
// 查看角色关联的资源树信息
|
||||
func (r *Role) RoleResource(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
|
||||
var resources vo.ResourceManageVOList
|
||||
r.RoleApp.GetRoleResources(uint64(ginx.PathParamInt(g, "id")), &resources)
|
||||
|
||||
r.RoleApp.GetRoleResources(uint64(rc.F.PathParamInt("id")), &resources)
|
||||
rc.ResData = resources.ToTrees(0)
|
||||
}
|
||||
|
||||
// 保存角色资源
|
||||
func (r *Role) SaveResource(rc *req.Ctx) {
|
||||
var form form.RoleResourceForm
|
||||
ginx.BindJsonAndValid(rc.GinCtx, &form)
|
||||
req.BindJsonAndValid(rc, &form)
|
||||
rc.ReqParam = form
|
||||
|
||||
// 将,拼接的字符串进行切割并转换
|
||||
@@ -89,9 +84,8 @@ func (r *Role) SaveResource(rc *req.Ctx) {
|
||||
|
||||
// 查看角色关联的用户
|
||||
func (r *Role) RoleAccount(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
cond, pageParam := ginx.BindQueryAndPage[*entity.RoleAccountQuery](g, new(entity.RoleAccountQuery))
|
||||
cond.RoleId = uint64(ginx.PathParamInt(g, "id"))
|
||||
cond, pageParam := req.BindQueryAndPage[*entity.RoleAccountQuery](rc, new(entity.RoleAccountQuery))
|
||||
cond.RoleId = uint64(rc.F.PathParamInt("id"))
|
||||
var accounts []*vo.AccountRoleVO
|
||||
res, err := r.RoleApp.GetRoleAccountPage(cond, pageParam, &accounts)
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"mayfly-go/internal/sys/application"
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
)
|
||||
|
||||
@@ -13,7 +12,7 @@ type Syslog struct {
|
||||
}
|
||||
|
||||
func (r *Syslog) Syslogs(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage[*entity.SysLogQuery](rc.GinCtx, new(entity.SysLogQuery))
|
||||
queryCond, page := req.BindQueryAndPage[*entity.SysLogQuery](rc, new(entity.SysLogQuery))
|
||||
res, err := r.SyslogApp.GetPageList(queryCond, page, new([]entity.SysLog), "create_time DESC")
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
|
||||
@@ -56,7 +56,7 @@ func (m *syslogAppImpl) SaveFromReq(req *req.Ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := req.Err; err != nil {
|
||||
if err := req.Error; err != nil {
|
||||
syslog.Type = entity.SyslogTypeError
|
||||
var errMsg string
|
||||
switch t := err.(type) {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"mayfly-go/internal/tag/application"
|
||||
"mayfly-go/internal/tag/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"sort"
|
||||
@@ -61,7 +60,7 @@ func (p *TagTree) GetTagTree(rc *req.Ctx) {
|
||||
|
||||
func (p *TagTree) ListByQuery(rc *req.Ctx) {
|
||||
cond := new(entity.TagTreeQuery)
|
||||
tagPaths := rc.GinCtx.Query("tagPaths")
|
||||
tagPaths := rc.F.Query("tagPaths")
|
||||
cond.CodePaths = strings.Split(tagPaths, ",")
|
||||
var tagTrees vo.TagTreeVOS
|
||||
p.TagTreeApp.ListByQuery(cond, &tagTrees)
|
||||
@@ -70,7 +69,7 @@ func (p *TagTree) ListByQuery(rc *req.Ctx) {
|
||||
|
||||
func (p *TagTree) SaveTagTree(rc *req.Ctx) {
|
||||
tagTree := &entity.TagTree{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, tagTree)
|
||||
req.BindJsonAndValid(rc, tagTree)
|
||||
|
||||
rc.ReqParam = fmt.Sprintf("tagTreeId: %d, tagName: %s, code: %s", tagTree.Id, tagTree.Name, tagTree.Code)
|
||||
|
||||
@@ -78,12 +77,12 @@ func (p *TagTree) SaveTagTree(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (p *TagTree) DelTagTree(rc *req.Ctx) {
|
||||
biz.ErrIsNil(p.TagTreeApp.Delete(rc.MetaCtx, uint64(ginx.PathParamInt(rc.GinCtx, "id"))))
|
||||
biz.ErrIsNil(p.TagTreeApp.Delete(rc.MetaCtx, uint64(rc.F.PathParamInt("id"))))
|
||||
}
|
||||
|
||||
// 获取用户可操作的资源标签路径
|
||||
func (p *TagTree) TagResources(rc *req.Ctx) {
|
||||
resourceType := int8(ginx.PathParamInt(rc.GinCtx, "rtype"))
|
||||
resourceType := int8(rc.F.PathParamInt("rtype"))
|
||||
tagResources := p.TagTreeApp.GetAccountTagResources(rc.GetLoginAccount().Id, resourceType, "")
|
||||
tagPath2Resource := collx.ArrayToMap[entity.TagResource, string](tagResources, func(tagResource entity.TagResource) string {
|
||||
return tagResource.TagPath
|
||||
@@ -97,6 +96,6 @@ func (p *TagTree) TagResources(rc *req.Ctx) {
|
||||
// 资源标签关联信息查询
|
||||
func (p *TagTree) QueryTagResources(rc *req.Ctx) {
|
||||
var trs []*entity.TagResource
|
||||
p.TagResourceApp.ListByQuery(ginx.BindQuery(rc.GinCtx, new(entity.TagResourceQuery)), &trs)
|
||||
p.TagResourceApp.ListByQuery(req.BindQuery(rc, new(entity.TagResourceQuery)), &trs)
|
||||
rc.ResData = trs
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"mayfly-go/internal/tag/application"
|
||||
"mayfly-go/internal/tag/domain/entity"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/req"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"strconv"
|
||||
@@ -23,7 +22,7 @@ type Team struct {
|
||||
}
|
||||
|
||||
func (p *Team) GetTeams(rc *req.Ctx) {
|
||||
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.TeamQuery))
|
||||
queryCond, page := req.BindQueryAndPage(rc, new(entity.TeamQuery))
|
||||
teams := &[]entity.Team{}
|
||||
res, err := p.TeamApp.GetPageList(queryCond, page, teams)
|
||||
biz.ErrIsNil(err)
|
||||
@@ -31,8 +30,7 @@ func (p *Team) GetTeams(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (p *Team) SaveTeam(rc *req.Ctx) {
|
||||
team := &entity.Team{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, team)
|
||||
team := req.BindJsonAndValid(rc, new(entity.Team))
|
||||
rc.ReqParam = team
|
||||
isAdd := team.Id == 0
|
||||
|
||||
@@ -51,7 +49,7 @@ func (p *Team) SaveTeam(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (p *Team) DelTeam(rc *req.Ctx) {
|
||||
idsStr := ginx.PathParam(rc.GinCtx, "id")
|
||||
idsStr := rc.F.PathParam("id")
|
||||
rc.ReqParam = idsStr
|
||||
ids := strings.Split(idsStr, ",")
|
||||
|
||||
@@ -64,18 +62,17 @@ func (p *Team) DelTeam(rc *req.Ctx) {
|
||||
|
||||
// 获取团队的成员信息
|
||||
func (p *Team) GetTeamMembers(rc *req.Ctx) {
|
||||
condition := &entity.TeamMember{TeamId: uint64(ginx.PathParamInt(rc.GinCtx, "id"))}
|
||||
condition.Username = rc.GinCtx.Query("username")
|
||||
condition := &entity.TeamMember{TeamId: uint64(rc.F.PathParamInt("id"))}
|
||||
condition.Username = rc.F.Query("username")
|
||||
|
||||
res, err := p.TeamApp.GetMemberPage(condition, ginx.GetPageParam(rc.GinCtx), &[]vo.TeamMember{})
|
||||
res, err := p.TeamApp.GetMemberPage(condition, rc.F.GetPageParam(), &[]vo.TeamMember{})
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
// 保存团队信息
|
||||
func (p *Team) SaveTeamMember(rc *req.Ctx) {
|
||||
teamMems := &form.TeamMember{}
|
||||
ginx.BindJsonAndValid(rc.GinCtx, teamMems)
|
||||
teamMems := req.BindJsonAndValid(rc, new(form.TeamMember))
|
||||
|
||||
teamId := teamMems.TeamId
|
||||
|
||||
@@ -101,9 +98,8 @@ func (p *Team) SaveTeamMember(rc *req.Ctx) {
|
||||
|
||||
// 删除团队成员
|
||||
func (p *Team) DelTeamMember(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
tid := ginx.PathParamInt(g, "id")
|
||||
aid := ginx.PathParamInt(g, "accountId")
|
||||
tid := rc.F.PathParamInt("id")
|
||||
aid := rc.F.PathParamInt("accountId")
|
||||
rc.ReqParam = fmt.Sprintf("teamId: %d, accountId: %d", tid, aid)
|
||||
|
||||
p.TeamApp.DeleteMember(rc.MetaCtx, uint64(tid), uint64(aid))
|
||||
@@ -111,21 +107,16 @@ func (p *Team) DelTeamMember(rc *req.Ctx) {
|
||||
|
||||
// 获取团队关联的标签id
|
||||
func (p *Team) GetTagIds(rc *req.Ctx) {
|
||||
rc.ResData = p.TeamApp.ListTagIds(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
|
||||
rc.ResData = p.TeamApp.ListTagIds(uint64(rc.F.PathParamInt("id")))
|
||||
}
|
||||
|
||||
// 保存团队关联标签信息
|
||||
func (p *Team) SaveTags(rc *req.Ctx) {
|
||||
g := rc.GinCtx
|
||||
|
||||
var form form.TagTreeTeam
|
||||
ginx.BindJsonAndValid(g, &form)
|
||||
|
||||
form := req.BindJsonAndValid(rc, new(form.TagTreeTeam))
|
||||
teamId := form.TeamId
|
||||
|
||||
// 将[]uint64转为[]any
|
||||
oIds := p.TeamApp.ListTagIds(teamId)
|
||||
|
||||
// 比较新旧两合集
|
||||
addIds, delIds, _ := collx.ArrayCompare(form.TagIds, oIds)
|
||||
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
package ginx
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/logx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/structx"
|
||||
"mayfly-go/pkg/validatorx"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
// 绑定并校验请求结构体参数
|
||||
func BindJsonAndValid[T any](g *gin.Context, data T) T {
|
||||
if err := g.ShouldBindJSON(data); err != nil {
|
||||
panic(ConvBindValidationError(data, err))
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定请求体中的json至form结构体,并拷贝至另一结构体
|
||||
func BindJsonAndCopyTo[T any](g *gin.Context, form any, toStruct T) T {
|
||||
BindJsonAndValid(g, form)
|
||||
structx.Copy(toStruct, form)
|
||||
return toStruct
|
||||
}
|
||||
|
||||
// 绑定查询字符串到指定结构体
|
||||
func BindQuery[T any](g *gin.Context, data T) T {
|
||||
if err := g.BindQuery(data); err != nil {
|
||||
panic(ConvBindValidationError(data, err))
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定查询字符串到指定结构体,并将分页信息也返回
|
||||
func BindQueryAndPage[T any](g *gin.Context, data T) (T, *model.PageParam) {
|
||||
if err := g.BindQuery(data); err != nil {
|
||||
panic(ConvBindValidationError(data, err))
|
||||
} else {
|
||||
return data, GetPageParam(g)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取分页参数
|
||||
func GetPageParam(g *gin.Context) *model.PageParam {
|
||||
return &model.PageParam{PageNum: QueryInt(g, "pageNum", 1), PageSize: QueryInt(g, "pageSize", 10)}
|
||||
}
|
||||
|
||||
// 获取查询参数,不存在则返回默认值
|
||||
func Query(g *gin.Context, qm string, defaultStr string) string {
|
||||
qv := g.Query(qm)
|
||||
if qv == "" {
|
||||
return defaultStr
|
||||
}
|
||||
return qv
|
||||
}
|
||||
|
||||
// 获取查询参数中指定参数值,并转为int
|
||||
func QueryInt(g *gin.Context, qm string, defaultInt int) int {
|
||||
qv := g.Query(qm)
|
||||
if qv == "" {
|
||||
return defaultInt
|
||||
}
|
||||
qvi, err := strconv.Atoi(qv)
|
||||
biz.ErrIsNil(err, "query param not int")
|
||||
return qvi
|
||||
}
|
||||
|
||||
// 获取路径参数
|
||||
func PathParamInt(g *gin.Context, pm string) int {
|
||||
value, err := strconv.Atoi(g.Param(pm))
|
||||
biz.ErrIsNilAppendErr(err, "string类型转换int异常: %s")
|
||||
return value
|
||||
}
|
||||
|
||||
// 获取路径参数
|
||||
func PathParam(g *gin.Context, pm string) string {
|
||||
return g.Param(pm)
|
||||
}
|
||||
|
||||
// 文件下载
|
||||
func Download(g *gin.Context, reader io.Reader, filename string) {
|
||||
g.Header("Content-Type", "application/octet-stream")
|
||||
g.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
io.Copy(g.Writer, reader)
|
||||
}
|
||||
|
||||
// 返回统一成功结果
|
||||
func SuccessRes(g *gin.Context, data any) {
|
||||
g.JSON(http.StatusOK, model.Success(data))
|
||||
}
|
||||
|
||||
// 返回失败结果集
|
||||
func ErrorRes(g *gin.Context, err any) {
|
||||
switch t := err.(type) {
|
||||
case errorx.BizError:
|
||||
g.JSON(http.StatusOK, model.Error(t))
|
||||
default:
|
||||
logx.ErrorTrace("服务器错误", t)
|
||||
g.JSON(http.StatusOK, model.ServerError())
|
||||
}
|
||||
}
|
||||
|
||||
// 转换参数校验错误为业务异常错误
|
||||
func ConvBindValidationError(data any, err error) error {
|
||||
if e, ok := err.(validator.ValidationErrors); ok {
|
||||
return errorx.NewBizCode(403, validatorx.Translate2Str(data, e))
|
||||
}
|
||||
return err
|
||||
}
|
||||
157
server/pkg/req/f.go
Normal file
157
server/pkg/req/f.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package req
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/model"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// http请求常用的通用方法(目前系统使用了以下那些方法),F算是framework简称
|
||||
type F interface {
|
||||
GetRequest() *http.Request
|
||||
|
||||
GetWriter() http.ResponseWriter
|
||||
|
||||
Redirect(code int, location string)
|
||||
|
||||
ClientIP() string
|
||||
|
||||
BindJSON(obj any) error
|
||||
|
||||
BindQuery(obj any) error
|
||||
|
||||
Query(qm string) string
|
||||
|
||||
PathParam(pm string) string
|
||||
|
||||
PostForm(key string) string
|
||||
|
||||
FormFile(name string) (*multipart.FileHeader, error)
|
||||
|
||||
MultipartForm() (*multipart.Form, error)
|
||||
|
||||
JSONRes(code int, data any)
|
||||
}
|
||||
|
||||
// wrapper F,提供更多基于F接口方法的封装方法
|
||||
type WrapperF struct {
|
||||
F F
|
||||
}
|
||||
|
||||
func NewWrapperF(f F) *WrapperF {
|
||||
return &WrapperF{F: f}
|
||||
}
|
||||
|
||||
// Header is an intelligent shortcut for c.Writer.Header().Set(key, value).
|
||||
// It writes a header in the response.
|
||||
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
||||
func (wf *WrapperF) Header(key, value string) {
|
||||
if value == "" {
|
||||
wf.GetWriter().Header().Del(key)
|
||||
return
|
||||
}
|
||||
wf.GetWriter().Header().Set(key, value)
|
||||
}
|
||||
|
||||
// get request header value
|
||||
func (wf *WrapperF) GetHeader(key string) string {
|
||||
return wf.GetRequest().Header.Get(key)
|
||||
}
|
||||
|
||||
// 获取查询参数,不存在则返回默认值
|
||||
func (wf *WrapperF) QueryDefault(qm string, defaultStr string) string {
|
||||
qv := wf.Query(qm)
|
||||
if qv == "" {
|
||||
return defaultStr
|
||||
}
|
||||
return qv
|
||||
}
|
||||
|
||||
// 获取查询参数中指定参数值,并转为int
|
||||
func (wf *WrapperF) QueryInt(qm string) int {
|
||||
return wf.QueryIntDefault(qm, 0)
|
||||
}
|
||||
|
||||
// 获取查询参数中指定参数值,并转为int, 不存在则返回默认值
|
||||
func (wf *WrapperF) QueryIntDefault(qm string, defaultInt int) int {
|
||||
qv := wf.Query(qm)
|
||||
if qv == "" {
|
||||
return defaultInt
|
||||
}
|
||||
qvi, err := strconv.Atoi(qv)
|
||||
biz.ErrIsNil(err, "query param not int")
|
||||
return qvi
|
||||
}
|
||||
|
||||
// 获取分页参数
|
||||
func (wf *WrapperF) GetPageParam() *model.PageParam {
|
||||
return &model.PageParam{PageNum: wf.QueryIntDefault("pageNum", 1), PageSize: wf.QueryIntDefault("pageSize", 10)}
|
||||
}
|
||||
|
||||
// 获取路径参数
|
||||
func (wf *WrapperF) PathParamInt(pm string) int {
|
||||
value, err := strconv.Atoi(wf.PathParam(pm))
|
||||
biz.ErrIsNilAppendErr(err, "string类型转换int异常: %s")
|
||||
return value
|
||||
}
|
||||
|
||||
func (wf *WrapperF) Download(reader io.Reader, filename string) {
|
||||
wf.Header("Content-Type", "application/octet-stream")
|
||||
wf.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
io.Copy(wf.GetWriter(), reader)
|
||||
}
|
||||
|
||||
/************************************/
|
||||
/************ wrapper F ************/
|
||||
/************************************/
|
||||
|
||||
func (wf *WrapperF) GetRequest() *http.Request {
|
||||
return wf.F.GetRequest()
|
||||
}
|
||||
|
||||
func (wf *WrapperF) GetWriter() http.ResponseWriter {
|
||||
return wf.F.GetWriter()
|
||||
}
|
||||
|
||||
func (wf *WrapperF) Redirect(code int, location string) {
|
||||
wf.F.Redirect(code, location)
|
||||
}
|
||||
|
||||
func (wf *WrapperF) ClientIP() string {
|
||||
return wf.F.ClientIP()
|
||||
}
|
||||
|
||||
func (wf *WrapperF) BindJSON(data any) error {
|
||||
return wf.F.BindJSON(data)
|
||||
}
|
||||
|
||||
func (wf *WrapperF) BindQuery(data any) error {
|
||||
return wf.F.BindQuery(data)
|
||||
}
|
||||
|
||||
func (wf *WrapperF) Query(qm string) string {
|
||||
return wf.F.Query(qm)
|
||||
}
|
||||
|
||||
func (wf *WrapperF) PathParam(pm string) string {
|
||||
return wf.F.PathParam(pm)
|
||||
}
|
||||
|
||||
func (wf *WrapperF) PostForm(key string) string {
|
||||
return wf.F.PostForm(key)
|
||||
}
|
||||
|
||||
func (wf *WrapperF) FormFile(name string) (*multipart.FileHeader, error) {
|
||||
return wf.F.FormFile(name)
|
||||
}
|
||||
|
||||
func (wf *WrapperF) MultipartForm() (*multipart.Form, error) {
|
||||
return wf.F.MultipartForm()
|
||||
}
|
||||
|
||||
func (wf *WrapperF) JSONRes(code int, data any) {
|
||||
wf.F.JSONRes(200, data)
|
||||
}
|
||||
65
server/pkg/req/ginf.go
Normal file
65
server/pkg/req/ginf.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package req
|
||||
|
||||
import (
|
||||
"mayfly-go/pkg/contextx"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func NewCtxWithGin(g *gin.Context) *Ctx {
|
||||
return &Ctx{F: NewWrapperF(&GinF{ginCtx: g}), MetaCtx: contextx.WithTraceId(g.Request.Context())}
|
||||
}
|
||||
|
||||
type GinF struct {
|
||||
ginCtx *gin.Context
|
||||
}
|
||||
|
||||
func (gf *GinF) GetRequest() *http.Request {
|
||||
return gf.ginCtx.Request
|
||||
}
|
||||
|
||||
func (gf *GinF) GetWriter() http.ResponseWriter {
|
||||
return gf.ginCtx.Writer
|
||||
}
|
||||
|
||||
func (gf GinF) Redirect(code int, location string) {
|
||||
gf.ginCtx.Redirect(code, location)
|
||||
}
|
||||
|
||||
func (gf *GinF) ClientIP() string {
|
||||
return gf.ginCtx.ClientIP()
|
||||
}
|
||||
|
||||
func (gf *GinF) BindJSON(data any) error {
|
||||
return gf.ginCtx.ShouldBindJSON(data)
|
||||
}
|
||||
|
||||
func (gf *GinF) BindQuery(data any) error {
|
||||
return gf.ginCtx.BindQuery(data)
|
||||
}
|
||||
|
||||
func (gf *GinF) Query(qm string) string {
|
||||
return gf.ginCtx.Query(qm)
|
||||
}
|
||||
|
||||
func (gf *GinF) PathParam(pm string) string {
|
||||
return gf.ginCtx.Param(pm)
|
||||
}
|
||||
|
||||
func (gf *GinF) PostForm(key string) string {
|
||||
return gf.ginCtx.PostForm(key)
|
||||
}
|
||||
|
||||
func (gf *GinF) FormFile(name string) (*multipart.FileHeader, error) {
|
||||
return gf.ginCtx.FormFile(name)
|
||||
}
|
||||
|
||||
func (gf *GinF) MultipartForm() (*multipart.Form, error) {
|
||||
return gf.ginCtx.MultipartForm()
|
||||
}
|
||||
|
||||
func (gf *GinF) JSONRes(code int, data any) {
|
||||
gf.ginCtx.JSON(200, data)
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func LogHandler(rc *Ctx) error {
|
||||
|
||||
attrMap := make(map[string]any, 0)
|
||||
|
||||
req := rc.GinCtx.Request
|
||||
req := rc.F.GetRequest()
|
||||
attrMap[req.Method] = req.URL.Path
|
||||
|
||||
if la := contextx.GetLoginAccount(rc.MetaCtx); la != nil {
|
||||
@@ -66,6 +66,7 @@ func LogHandler(rc *Ctx) error {
|
||||
}
|
||||
|
||||
logMsg := li.Description
|
||||
rcErr := rc.Error
|
||||
|
||||
if logx.GetConfig().IsJsonType() {
|
||||
// json格式日志处理
|
||||
@@ -75,9 +76,9 @@ func LogHandler(rc *Ctx) error {
|
||||
}
|
||||
attrMap["exeTime"] = rc.timed
|
||||
|
||||
if rc.Err != nil {
|
||||
if rcErr != nil {
|
||||
nFrames := DefaultLogFrames
|
||||
if _, ok := rc.Err.(errorx.BizError); ok {
|
||||
if _, ok := rc.Error.(errorx.BizError); ok {
|
||||
nFrames = nFrames / 2
|
||||
}
|
||||
attrMap["error"] = rc.Err
|
||||
@@ -86,14 +87,14 @@ func LogHandler(rc *Ctx) error {
|
||||
}
|
||||
} else {
|
||||
// 处理文本格式日志信息
|
||||
if err := rc.Err; err != nil {
|
||||
logMsg = getErrMsg(rc, err)
|
||||
if rcErr != nil {
|
||||
logMsg = getErrMsg(rc, rcErr)
|
||||
} else {
|
||||
logMsg = getLogMsg(rc)
|
||||
}
|
||||
}
|
||||
|
||||
if err := rc.Err; err != nil {
|
||||
if rcErr != nil {
|
||||
logx.ErrorWithFields(rc.MetaCtx, logMsg, attrMap)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -44,12 +44,12 @@ func PermissionHandler(rc *Ctx) error {
|
||||
if permission != nil && !permission.NeedToken {
|
||||
return nil
|
||||
}
|
||||
tokenStr := rc.GinCtx.Request.Header.Get("Authorization")
|
||||
tokenStr := rc.F.GetHeader("Authorization")
|
||||
// 删除前缀 Bearer, 以支持 Bearer Token
|
||||
tokenStr, _ = strings.CutPrefix(tokenStr, "Bearer ")
|
||||
// header不存在则从查询参数token中获取
|
||||
if tokenStr == "" {
|
||||
tokenStr = rc.GinCtx.Query("token")
|
||||
tokenStr = rc.F.Query("token")
|
||||
}
|
||||
if tokenStr == "" {
|
||||
return errorx.PermissionErr
|
||||
|
||||
@@ -2,46 +2,41 @@ package req
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/contextx"
|
||||
"mayfly-go/pkg/ginx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/assert"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 处理函数
|
||||
type HandlerFunc func(*Ctx)
|
||||
|
||||
type Ctx struct {
|
||||
Conf *Conf // 请求配置
|
||||
Conf *Conf // 请求配置
|
||||
F *WrapperF // http framework处理接口
|
||||
|
||||
GinCtx *gin.Context // gin context
|
||||
ReqParam any // 请求参数,主要用于记录日志
|
||||
LogExtra any // 日志额外参数,主要用于系统日志定制化展示
|
||||
ResData any // 响应结果
|
||||
Err any // 请求错误
|
||||
timed int64 // 执行时间
|
||||
ReqParam any // 请求参数,主要用于记录日志
|
||||
ResData any // 响应结果
|
||||
Error any // 请求错误
|
||||
timed int64 // 执行时间
|
||||
|
||||
MetaCtx context.Context // 元数据上下文信息,如登录账号(只有校验token后才会有值),traceId等
|
||||
}
|
||||
|
||||
func (rc *Ctx) Handle(handler HandlerFunc) {
|
||||
ginCtx := rc.GinCtx
|
||||
begin := time.Now()
|
||||
defer func() {
|
||||
rc.timed = time.Since(begin).Milliseconds()
|
||||
if err := recover(); err != nil {
|
||||
rc.Err = err
|
||||
ginx.ErrorRes(ginCtx, err)
|
||||
rc.Error = err
|
||||
ErrorRes(rc, err)
|
||||
}
|
||||
// 应用所有请求后置处理器
|
||||
ApplyHandlerInterceptor(afterHandlers, rc)
|
||||
}()
|
||||
assert.IsTrue(ginCtx != nil, "ginContext == nil")
|
||||
assert.IsTrue(rc.F != nil, "F == nil")
|
||||
|
||||
// 默认为不记录请求参数,可在handler回调函数中覆盖赋值
|
||||
rc.ReqParam = nil
|
||||
@@ -56,14 +51,10 @@ func (rc *Ctx) Handle(handler HandlerFunc) {
|
||||
|
||||
handler(rc)
|
||||
if rc.Conf == nil || !rc.Conf.noRes {
|
||||
ginx.SuccessRes(ginCtx, rc.ResData)
|
||||
rc.F.JSONRes(http.StatusOK, model.Success(rc.ResData))
|
||||
}
|
||||
}
|
||||
|
||||
func (rc *Ctx) Download(reader io.Reader, filename string) {
|
||||
ginx.Download(rc.GinCtx, reader, filename)
|
||||
}
|
||||
|
||||
// 获取当前登录账号信息,不存在则会报错。
|
||||
//
|
||||
// 若不需要报错,则使用contextx.GetLoginAccount方法
|
||||
@@ -100,8 +91,51 @@ func (rc *Ctx) GetLogInfo() *LogInfo {
|
||||
return rc.Conf.logInfo
|
||||
}
|
||||
|
||||
func NewCtxWithGin(g *gin.Context) *Ctx {
|
||||
return &Ctx{GinCtx: g, MetaCtx: contextx.WithTraceId(g.Request.Context())}
|
||||
/************************************/
|
||||
/***** GOLANG.ORG/X/NET/CONTEXT -> copy gin.Context *****/
|
||||
/************************************/
|
||||
|
||||
// hasRequestContext returns whether c.Request has Context and fallback.
|
||||
func (c *Ctx) hasRequestContext() bool {
|
||||
request := c.F.GetRequest()
|
||||
return request != nil && request.Context() != nil
|
||||
}
|
||||
|
||||
// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
|
||||
func (c *Ctx) Deadline() (deadline time.Time, ok bool) {
|
||||
if !c.hasRequestContext() {
|
||||
return
|
||||
}
|
||||
return c.F.GetRequest().Context().Deadline()
|
||||
}
|
||||
|
||||
// Done returns nil (chan which will wait forever) when c.Request has no Context.
|
||||
func (c *Ctx) Done() <-chan struct{} {
|
||||
if !c.hasRequestContext() {
|
||||
return nil
|
||||
}
|
||||
return c.F.GetRequest().Context().Done()
|
||||
}
|
||||
|
||||
// Err returns nil when c.Request has no Context.
|
||||
func (c *Ctx) Err() error {
|
||||
if !c.hasRequestContext() {
|
||||
return nil
|
||||
}
|
||||
return c.F.GetRequest().Context().Err()
|
||||
}
|
||||
|
||||
// Value returns the value associated with this context for key, or nil
|
||||
// if no value is associated with key. Successive calls to Value with
|
||||
// the same key returns the same result.
|
||||
func (c *Ctx) Value(key any) any {
|
||||
if key == 0 {
|
||||
return c.F.GetRequest()
|
||||
}
|
||||
if !c.hasRequestContext() {
|
||||
return nil
|
||||
}
|
||||
return c.F.GetRequest().Context().Value(key)
|
||||
}
|
||||
|
||||
// 处理器拦截器函数
|
||||
|
||||
65
server/pkg/req/util.go
Normal file
65
server/pkg/req/util.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package req
|
||||
|
||||
import (
|
||||
"mayfly-go/pkg/errorx"
|
||||
"mayfly-go/pkg/logx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/structx"
|
||||
"mayfly-go/pkg/validatorx"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
// 绑定并校验请求结构体参数
|
||||
func BindJsonAndValid[T any](rc *Ctx, data T) T {
|
||||
if err := rc.F.BindJSON(data); err != nil {
|
||||
panic(ConvBindValidationError(data, err))
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定请求体中的json至form结构体,并拷贝至另一结构体
|
||||
func BindJsonAndCopyTo[T any](rc *Ctx, form any, toStruct T) T {
|
||||
BindJsonAndValid(rc, form)
|
||||
structx.Copy(toStruct, form)
|
||||
return toStruct
|
||||
}
|
||||
|
||||
// 绑定查询字符串到指定结构体
|
||||
func BindQuery[T any](rc *Ctx, data T) T {
|
||||
if err := rc.F.BindQuery(data); err != nil {
|
||||
panic(ConvBindValidationError(data, err))
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
// 绑定查询字符串到指定结构体,并将分页信息也返回
|
||||
func BindQueryAndPage[T any](rc *Ctx, data T) (T, *model.PageParam) {
|
||||
if err := rc.F.BindQuery(data); err != nil {
|
||||
panic(ConvBindValidationError(data, err))
|
||||
} else {
|
||||
return data, rc.F.GetPageParam()
|
||||
}
|
||||
}
|
||||
|
||||
// 返回失败结果集
|
||||
func ErrorRes(rc *Ctx, err any) {
|
||||
switch t := err.(type) {
|
||||
case errorx.BizError:
|
||||
rc.F.JSONRes(http.StatusOK, model.Error(t))
|
||||
default:
|
||||
logx.ErrorTrace("服务器错误", t)
|
||||
rc.F.JSONRes(http.StatusOK, model.ServerError())
|
||||
}
|
||||
}
|
||||
|
||||
// 转换参数校验错误为业务异常错误
|
||||
func ConvBindValidationError(data any, err error) error {
|
||||
if e, ok := err.(validator.ValidationErrors); ok {
|
||||
return errorx.NewBizCode(403, validatorx.Translate2Str(data, e))
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user