2022-10-31 09:26:15 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-11-02 10:30:22 +00:00
|
|
|
"bufio"
|
2022-11-02 08:28:54 +00:00
|
|
|
"fmt"
|
2022-11-02 10:30:22 +00:00
|
|
|
"os"
|
2022-11-02 08:28:54 +00:00
|
|
|
|
2022-10-31 09:26:15 +00:00
|
|
|
"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"
|
2022-11-03 15:42:42 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
2022-10-31 09:26:15 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
|
|
|
// @Summary Load redis status info
|
|
|
|
// @Description 获取 redis 状态信息
|
|
|
|
// @Success 200 {object} dto.RedisStatus
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/status [get]
|
2022-11-02 08:28:54 +00:00
|
|
|
func (b *BaseApi) LoadRedisStatus(c *gin.Context) {
|
|
|
|
data, err := redisService.LoadStatus()
|
|
|
|
if err != nil {
|
2022-10-31 09:26:15 +00:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-02 08:28:54 +00:00
|
|
|
|
|
|
|
helper.SuccessWithData(c, data)
|
2022-10-31 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
|
|
|
// @Summary Load redis conf
|
|
|
|
// @Description 获取 redis 配置信息
|
|
|
|
// @Success 200 {object} dto.RedisConf
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/conf [get]
|
2022-11-02 08:28:54 +00:00
|
|
|
func (b *BaseApi) LoadRedisConf(c *gin.Context) {
|
|
|
|
data, err := redisService.LoadConf()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
2022-10-31 09:26:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-02 08:28:54 +00:00
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
|
|
|
// @Summary Load redis persistence conf
|
|
|
|
// @Description 获取 redis 持久化配置
|
|
|
|
// @Success 200 {object} dto.RedisPersistence
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/persistence/conf [get]
|
2022-11-02 08:28:54 +00:00
|
|
|
func (b *BaseApi) LoadPersistenceConf(c *gin.Context) {
|
|
|
|
data, err := redisService.LoadPersistenceConf()
|
2022-10-31 09:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-02 08:28:54 +00:00
|
|
|
helper.SuccessWithData(c, data)
|
2022-10-31 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
|
|
|
// @Summary Update redis conf
|
|
|
|
// @Description 更新 redis 配置信息
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.RedisConfUpdate true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/conf/update [post]
|
|
|
|
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFuntions":[],"formatZH":"更新 redis 数据库配置信息","formatEN":"update the redis database configuration information"}
|
2022-11-02 08:28:54 +00:00
|
|
|
func (b *BaseApi) UpdateRedisConf(c *gin.Context) {
|
|
|
|
var req dto.RedisConfUpdate
|
2022-10-31 09:26:15 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-02 08:28:54 +00:00
|
|
|
if err := redisService.UpdateConf(req); err != nil {
|
2022-10-31 09:26:15 +00:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
|
|
|
// @Summary Change redis password
|
|
|
|
// @Description 更新 redis 密码
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.ChangeDBInfo true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/password [post]
|
|
|
|
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFuntions":[],"formatZH":"修改 redis 数据库密码","formatEN":"change the password of the redis database"}
|
2022-12-12 06:06:39 +00:00
|
|
|
func (b *BaseApi) ChangeRedisPassword(c *gin.Context) {
|
|
|
|
var req dto.ChangeDBInfo
|
|
|
|
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.ChangePassword(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
|
|
|
// @Summary Update redis persistence conf
|
|
|
|
// @Description 更新 redis 持久化配置
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.RedisConfPersistenceUpdate true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/persistence/update [post]
|
|
|
|
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFuntions":[],"formatZH":"redis 数据库持久化配置更新","formatEN":"redis database persistence configuration update"}
|
2022-11-03 15:42:42 +00:00
|
|
|
func (b *BaseApi) UpdateRedisPersistenceConf(c *gin.Context) {
|
|
|
|
var req dto.RedisConfPersistenceUpdate
|
|
|
|
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.UpdatePersistenceConf(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
2023-01-05 03:57:03 +00:00
|
|
|
// @Summary Page redis backups
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Description 获取 redis 备份记录分页
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.PageInfo true "request"
|
|
|
|
// @Success 200 {object} dto.PageResult
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/backup/search [post]
|
2022-11-03 15:42:42 +00:00
|
|
|
func (b *BaseApi) RedisBackupList(c *gin.Context) {
|
|
|
|
var req dto.PageInfo
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total, list, err := redisService.SearchBackupListWithPage(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:31:51 +00:00
|
|
|
// @Tags Database Redis
|
|
|
|
// @Summary Update redis conf by file
|
|
|
|
// @Description 上传更新 redis 配置信息
|
|
|
|
// @Accept json
|
|
|
|
// @Param request body dto.RedisConfUpdateByFile true "request"
|
|
|
|
// @Success 200
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /databases/redis/conffile/update [post]
|
|
|
|
// @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFuntions":[],"formatZH":"更新 redis 数据库配置信息","formatEN":"update the redis database configuration information"}
|
2022-11-02 10:30:22 +00:00
|
|
|
func (b *BaseApi) UpdateRedisConfByFile(c *gin.Context) {
|
|
|
|
var req dto.RedisConfUpdateByFile
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
redisInfo, err := redisService.LoadConf()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-03 15:42:42 +00:00
|
|
|
path := fmt.Sprintf("%s/redis/%s/conf/redis.conf", constant.AppInstallDir, redisInfo.Name)
|
2022-11-02 10:30:22 +00:00
|
|
|
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0640)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
write := bufio.NewWriter(file)
|
|
|
|
_, _ = write.WriteString(req.File)
|
|
|
|
write.Flush()
|
|
|
|
|
2022-11-03 15:42:42 +00:00
|
|
|
if req.RestartNow {
|
|
|
|
composeDir := fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)
|
|
|
|
if _, err := compose.Restart(composeDir); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 10:30:22 +00:00
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|