mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2026-10-28 02:57:28 +08:00
feat: 完成 redis 增删改查
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||
"github.com/1Panel-dev/1Panel/backend/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (b *BaseApi) SetRedis(c *gin.Context) {
|
||||
var req dto.RedisDataSet
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||
return
|
||||
}
|
||||
if err := global.VALID.Struct(req); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||
return
|
||||
}
|
||||
if err := redisService.Set(req); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||
return
|
||||
}
|
||||
helper.SuccessWithData(c, nil)
|
||||
}
|
||||
|
||||
func (b *BaseApi) SearchRedis(c *gin.Context) {
|
||||
var req dto.SearchRedisWithPage
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||
return
|
||||
}
|
||||
|
||||
total, list, err := redisService.SearchWithPage(req)
|
||||
if err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||
return
|
||||
}
|
||||
|
||||
helper.SuccessWithData(c, dto.PageResult{
|
||||
Items: list,
|
||||
Total: total,
|
||||
})
|
||||
}
|
||||
|
||||
func (b *BaseApi) DeleteRedis(c *gin.Context) {
|
||||
var req dto.RedisDelBatch
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||
return
|
||||
}
|
||||
if err := global.VALID.Struct(req); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := redisService.Delete(req); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||
return
|
||||
}
|
||||
helper.SuccessWithData(c, nil)
|
||||
}
|
||||
|
||||
func (b *BaseApi) CleanRedis(c *gin.Context) {
|
||||
db, ok := c.Params.Get("db")
|
||||
if !ok {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error db in path"))
|
||||
return
|
||||
}
|
||||
dbNum, err := strconv.Atoi(db)
|
||||
if err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error db in path"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := redisService.CleanAll(dbNum); err != nil {
|
||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||
return
|
||||
}
|
||||
helper.SuccessWithData(c, nil)
|
||||
}
|
||||
@@ -19,6 +19,7 @@ var (
|
||||
imageService = service.ServiceGroupApp.ImageService
|
||||
|
||||
mysqlService = service.ServiceGroupApp.MysqlService
|
||||
redisService = service.ServiceGroupApp.RedisService
|
||||
|
||||
cronjobService = service.ServiceGroupApp.CronjobService
|
||||
|
||||
|
||||
@@ -25,11 +25,12 @@ type BackupSearch struct {
|
||||
}
|
||||
|
||||
type BackupRecords struct {
|
||||
ID uint `json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Source string `json:"source"`
|
||||
FileDir string `json:"fileDir"`
|
||||
FileName string `json:"fileName"`
|
||||
ID uint `json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Source string `json:"source"`
|
||||
BackupType string `json:"backupType"`
|
||||
FileDir string `json:"fileDir"`
|
||||
FileName string `json:"fileName"`
|
||||
}
|
||||
|
||||
type DownloadRecord struct {
|
||||
|
||||
@@ -135,3 +135,29 @@ type RecoverDB struct {
|
||||
DBName string `json:"dbName" validate:"required"`
|
||||
BackupName string `json:"backupName" validate:"required"`
|
||||
}
|
||||
|
||||
// redis
|
||||
type SearchRedisWithPage struct {
|
||||
PageInfo
|
||||
DB int `json:"db" validate:"required"`
|
||||
}
|
||||
|
||||
type RedisData struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Type string `json:"type"`
|
||||
Length int64 `json:"length"`
|
||||
Expiration int64 `json:"expiration"`
|
||||
}
|
||||
|
||||
type RedisDataSet struct {
|
||||
DB int `json:"db"`
|
||||
Key string `json:"key" validate:"required"`
|
||||
Value string `json:"value" validate:"required"`
|
||||
Expiration int64 `json:"expiration"`
|
||||
}
|
||||
|
||||
type RedisDelBatch struct {
|
||||
DB int `json:"db" validate:"required"`
|
||||
Names []string `json:"names" validate:"required"`
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ type BackupRecord struct {
|
||||
Name string `gorm:"type:varchar(64);not null" json:"name"`
|
||||
DetailName string `gorm:"type:varchar(256)" json:"detailName"`
|
||||
Source string `gorm:"type:varchar(256)" json:"source"`
|
||||
BackupType string `gorm:"type:varchar(256)" json:"backupType"`
|
||||
FileDir string `gorm:"type:varchar(256)" json:"fileDir"`
|
||||
FileName string `gorm:"type:varchar(256)" json:"fileName"`
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ type IMysqlRepo interface {
|
||||
Delete(opts ...DBOption) error
|
||||
Update(id uint, vars map[string]interface{}) error
|
||||
LoadRunningVersion() ([]string, error)
|
||||
LoadBaseInfoByVersion(key string) (*RootInfo, error)
|
||||
LoadBaseInfoByKey(key string) (*RootInfo, error)
|
||||
UpdateMysqlConf(id uint, vars map[string]interface{}) error
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ type RootInfo struct {
|
||||
Env string `json:"env"`
|
||||
}
|
||||
|
||||
func (u *MysqlRepo) LoadBaseInfoByVersion(key string) (*RootInfo, error) {
|
||||
func (u *MysqlRepo) LoadBaseInfoByKey(key string) (*RootInfo, error) {
|
||||
var (
|
||||
app model.App
|
||||
appInstall model.AppInstall
|
||||
|
||||
@@ -66,7 +66,7 @@ func (u *MysqlService) ListDBByVersion(version string) ([]string, error) {
|
||||
}
|
||||
|
||||
func (u *MysqlService) SearchBackupsWithPage(search dto.SearchBackupsWithPage) (int64, interface{}, error) {
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(search.Version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(search.Version)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func (u *MysqlService) Create(mysqlDto dto.MysqlDBCreate) error {
|
||||
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
||||
}
|
||||
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(mysqlDto.Version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(mysqlDto.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func (u *MysqlService) Backup(db dto.BackupDB) error {
|
||||
}
|
||||
|
||||
func (u *MysqlService) Recover(db dto.RecoverDB) error {
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(db.Version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(db.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -163,7 +163,7 @@ func (u *MysqlService) Recover(db dto.RecoverDB) error {
|
||||
}
|
||||
|
||||
func (u *MysqlService) Delete(version string, ids []uint) error {
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -198,7 +198,7 @@ func (u *MysqlService) ChangeInfo(info dto.ChangeDBInfo) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(info.Version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(info.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -266,7 +266,7 @@ func (u *MysqlService) ChangeInfo(info dto.ChangeDBInfo) error {
|
||||
}
|
||||
|
||||
func (u *MysqlService) UpdateVariables(variables dto.MysqlVariablesUpdate) error {
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(variables.Version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(variables.Version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -320,7 +320,7 @@ func (u *MysqlService) UpdateVariables(variables dto.MysqlVariablesUpdate) error
|
||||
|
||||
func (u *MysqlService) LoadBaseInfo(version string) (*dto.DBBaseInfo, error) {
|
||||
var data dto.DBBaseInfo
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -342,7 +342,7 @@ func (u *MysqlService) LoadBaseInfo(version string) (*dto.DBBaseInfo, error) {
|
||||
}
|
||||
|
||||
func (u *MysqlService) LoadVariables(version string) (*dto.MysqlVariables, error) {
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -360,7 +360,7 @@ func (u *MysqlService) LoadVariables(version string) (*dto.MysqlVariables, error
|
||||
}
|
||||
|
||||
func (u *MysqlService) LoadStatus(version string) (*dto.MysqlStatus, error) {
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -444,7 +444,7 @@ func excuteSql(containerName, password, command string) error {
|
||||
}
|
||||
|
||||
func backupMysql(backupType, baseDir, backupDir, version, dbName, fileName string) error {
|
||||
app, err := mysqlRepo.LoadBaseInfoByVersion(version)
|
||||
app, err := mysqlRepo.LoadBaseInfoByKey(version)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -471,6 +471,7 @@ func backupMysql(backupType, baseDir, backupDir, version, dbName, fileName strin
|
||||
Name: app.Name,
|
||||
DetailName: dbName,
|
||||
Source: backupType,
|
||||
BackupType: backupType,
|
||||
FileDir: backupDir,
|
||||
FileName: fileName,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||
"github.com/go-redis/redis"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
type RedisService struct{}
|
||||
|
||||
type IRedisService interface {
|
||||
SearchWithPage(search dto.SearchRedisWithPage) (int64, interface{}, error)
|
||||
Set(setData dto.RedisDataSet) error
|
||||
Delete(info dto.RedisDelBatch) error
|
||||
CleanAll(db int) error
|
||||
|
||||
// Backup(db dto.BackupDB) error
|
||||
// Recover(db dto.RecoverDB) error
|
||||
}
|
||||
|
||||
func NewIRedisService() IRedisService {
|
||||
return &RedisService{}
|
||||
}
|
||||
|
||||
func (u *RedisService) SearchWithPage(search dto.SearchRedisWithPage) (int64, interface{}, error) {
|
||||
redisInfo, err := mysqlRepo.LoadBaseInfoByKey("redis")
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("localhost:%v", redisInfo.Port),
|
||||
Password: "eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81",
|
||||
DB: search.DB,
|
||||
})
|
||||
total, err := client.DbSize().Result()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
keys, _, err := client.Scan(uint64((search.Page-1)*search.PageSize), "*", int64(search.PageSize)).Result()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
var data []dto.RedisData
|
||||
for _, key := range keys {
|
||||
var dataItem dto.RedisData
|
||||
dataItem.Key = key
|
||||
value, err := client.Get(key).Result()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
dataItem.Value = value
|
||||
typeVal, err := client.Type(key).Result()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
dataItem.Type = typeVal
|
||||
length, err := client.StrLen(key).Result()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
dataItem.Length = length
|
||||
ttl, err := client.TTL(key).Result()
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
dataItem.Expiration = int64(ttl / 1000000000)
|
||||
data = append(data, dataItem)
|
||||
}
|
||||
return total, data, nil
|
||||
}
|
||||
|
||||
func (u *RedisService) Set(setData dto.RedisDataSet) error {
|
||||
redisInfo, err := mysqlRepo.LoadBaseInfoByKey("redis")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("localhost:%v", redisInfo.Port),
|
||||
Password: "eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81",
|
||||
DB: setData.DB,
|
||||
})
|
||||
value, _ := client.Get(setData.Key).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(value) != 0 {
|
||||
if _, err := client.Del(setData.Key).Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := client.Set(setData.Key, setData.Value, time.Duration(setData.Expiration*int64(time.Second))).Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *RedisService) Delete(req dto.RedisDelBatch) error {
|
||||
redisInfo, err := mysqlRepo.LoadBaseInfoByKey("redis")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("localhost:%v", redisInfo.Port),
|
||||
Password: "eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81",
|
||||
DB: req.DB,
|
||||
})
|
||||
if _, err := client.Del(req.Names...).Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *RedisService) CleanAll(db int) error {
|
||||
redisInfo, err := mysqlRepo.LoadBaseInfoByKey("redis")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: fmt.Sprintf("localhost:%v", redisInfo.Port),
|
||||
Password: "eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81",
|
||||
DB: db,
|
||||
})
|
||||
if _, err := client.FlushAll().Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,30 +1,62 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
func TestMysql(t *testing.T) {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
Password: "eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81",
|
||||
DB: 0,
|
||||
})
|
||||
// fmt.Println(rdb.Get("dqwas"))
|
||||
|
||||
gzipFile, err := os.Open("/tmp/ko.sql.gz")
|
||||
client.Set("omg", "111", 10*time.Minute)
|
||||
client.Set("omg1", "111", 10*time.Minute)
|
||||
client.Set("omg2", "111", 10*time.Minute)
|
||||
client.Set("omg3", "111", 10*time.Minute)
|
||||
client.Set("omg4", "111", 10*time.Minute)
|
||||
client.Set("omg5", "111", 10*time.Minute)
|
||||
client.Set("omg6", "111", 10*time.Minute)
|
||||
client.Set("omg7", "111", 10*time.Minute)
|
||||
client.Set("omg8", "111", 10*time.Minute)
|
||||
client.Set("omg9", "111", 10*time.Minute)
|
||||
keys, _, err := client.Scan(0, "*", 5).Result()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
panic(err)
|
||||
}
|
||||
defer gzipFile.Close()
|
||||
gzipReader, err := gzip.NewReader(gzipFile)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
|
||||
cmd := exec.Command("docker", "exec", "-i", "365", "mysql", "-uroot", "-pCalong@2012", "kubeoperator")
|
||||
cmd.Stdin = gzipReader
|
||||
stdout, err := cmd.CombinedOutput()
|
||||
fmt.Println(string(stdout), err)
|
||||
var data []dto.RedisData
|
||||
for _, key := range keys {
|
||||
var dataItem dto.RedisData
|
||||
dataItem.Key = key
|
||||
value, err := client.Get(key).Result()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
dataItem.Value = value
|
||||
typeVal, err := client.Type(key).Result()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
dataItem.Type = typeVal
|
||||
length, err := client.StrLen(key).Result()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
dataItem.Length = length
|
||||
ttl, err := client.TTL(key).Result()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
dataItem.Expiration = int64(ttl / 1000000000)
|
||||
data = append(data, dataItem)
|
||||
}
|
||||
fmt.Println(data)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ type ServiceGroup struct {
|
||||
ComposeTemplateService
|
||||
|
||||
MysqlService
|
||||
RedisService
|
||||
|
||||
CronjobService
|
||||
|
||||
|
||||
@@ -34,5 +34,10 @@ func (s *DatabaseRouter) InitDatabaseRouter(Router *gin.RouterGroup) {
|
||||
cmdRouter.GET("/baseinfo/:version", baseApi.LoadBaseinfo)
|
||||
cmdRouter.GET("/versions", baseApi.LoadVersions)
|
||||
cmdRouter.GET("/dbs/:version", baseApi.ListDBNameByVersion)
|
||||
|
||||
cmdRouter.POST("/redis/search", baseApi.SearchRedis)
|
||||
withRecordRouter.POST("/redis", baseApi.SetRedis)
|
||||
withRecordRouter.POST("/redis/del", baseApi.DeleteRedis)
|
||||
withRecordRouter.POST("/redis/clean/:db", baseApi.CleanRedis)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user