mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-12-29 10:56:35 +08:00
refactor: api层尽可能屏蔽gin框架相关代码
This commit is contained in:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user