2022-10-20 18:45:47 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-11-02 18:30:22 +08:00
|
|
|
"bufio"
|
2022-10-25 18:34:33 +08:00
|
|
|
"errors"
|
2022-11-02 18:30:22 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-10-25 18:34:33 +08:00
|
|
|
|
2022-10-20 18:45:47 +08: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"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (b *BaseApi) CreateMysql(c *gin.Context) {
|
|
|
|
var req dto.MysqlDBCreate
|
|
|
|
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 := mysqlService.Create(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-10-24 18:46:19 +08:00
|
|
|
func (b *BaseApi) UpdateMysql(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 := mysqlService.ChangeInfo(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-10-25 11:41:19 +08:00
|
|
|
func (b *BaseApi) UpdateMysqlVariables(c *gin.Context) {
|
2022-11-04 19:02:15 +08:00
|
|
|
var req []dto.MysqlVariablesUpdate
|
2022-10-25 11:41:19 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-04 19:02:15 +08:00
|
|
|
|
|
|
|
mysqlName, ok := c.Params.Get("mysqlName")
|
|
|
|
if !ok {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error mysqlName in path"))
|
2022-10-25 11:41:19 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-04 19:02:15 +08:00
|
|
|
|
|
|
|
if err := mysqlService.UpdateVariables(mysqlName, req); err != nil {
|
2022-10-25 11:41:19 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-11-02 18:30:22 +08:00
|
|
|
func (b *BaseApi) UpdateMysqlConfByFile(c *gin.Context) {
|
|
|
|
var req dto.MysqlConfUpdateByFile
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mysqlInfo, err := mysqlService.LoadBaseInfo(req.MysqlName)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path := fmt.Sprintf("/opt/1Panel/data/apps/%s/%s/conf/my.cnf", mysqlInfo.MysqlKey, mysqlInfo.Name)
|
|
|
|
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()
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:45:47 +08:00
|
|
|
func (b *BaseApi) SearchMysql(c *gin.Context) {
|
2022-10-25 18:34:33 +08:00
|
|
|
var req dto.SearchDBWithPage
|
2022-10-20 18:45:47 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total, list, err := mysqlService.SearchWithPage(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-28 18:46:14 +08:00
|
|
|
func (b *BaseApi) ListDBNameByVersion(c *gin.Context) {
|
2022-10-31 23:52:39 +08:00
|
|
|
name, ok := c.Params.Get("name")
|
2022-10-28 18:46:14 +08:00
|
|
|
if !ok {
|
2022-10-31 23:52:39 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name in path"))
|
2022-10-28 18:46:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
list, err := mysqlService.ListDBByVersion(name)
|
2022-10-28 18:46:14 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, list)
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:09:39 +08:00
|
|
|
func (b *BaseApi) SearchDBBackups(c *gin.Context) {
|
|
|
|
var req dto.SearchBackupsWithPage
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 18:46:14 +08:00
|
|
|
total, list, err := mysqlService.SearchBackupsWithPage(req)
|
2022-10-27 23:09:39 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) BackupMysql(c *gin.Context) {
|
|
|
|
var req dto.BackupDB
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mysqlService.Backup(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) RecoverMysql(c *gin.Context) {
|
|
|
|
var req dto.RecoverDB
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mysqlService.Recover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:45:47 +08:00
|
|
|
func (b *BaseApi) DeleteMysql(c *gin.Context) {
|
|
|
|
var req dto.BatchDeleteReq
|
|
|
|
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-10-31 23:52:39 +08:00
|
|
|
name, ok := c.Params.Get("name")
|
2022-10-25 18:34:33 +08:00
|
|
|
if !ok {
|
2022-10-31 23:52:39 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name in path"))
|
2022-10-25 18:34:33 +08:00
|
|
|
return
|
|
|
|
}
|
2022-10-20 18:45:47 +08:00
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
if err := mysqlService.Delete(name, req.Ids); err != nil {
|
2022-10-20 18:45:47 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
2022-10-21 18:50:38 +08:00
|
|
|
|
2022-10-25 18:34:33 +08:00
|
|
|
func (b *BaseApi) LoadVersions(c *gin.Context) {
|
|
|
|
data, err := mysqlService.LoadRunningVersion()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) LoadBaseinfo(c *gin.Context) {
|
2022-10-31 23:52:39 +08:00
|
|
|
name, ok := c.Params.Get("name")
|
2022-10-25 18:34:33 +08:00
|
|
|
if !ok {
|
2022-10-31 23:52:39 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name in path"))
|
2022-10-25 18:34:33 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
data, err := mysqlService.LoadBaseInfo(name)
|
2022-10-25 18:34:33 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:50:38 +08:00
|
|
|
func (b *BaseApi) LoadStatus(c *gin.Context) {
|
2022-10-31 23:52:39 +08:00
|
|
|
name, ok := c.Params.Get("name")
|
2022-10-25 18:34:33 +08:00
|
|
|
if !ok {
|
2022-10-31 23:52:39 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name in path"))
|
2022-10-25 18:34:33 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
data, err := mysqlService.LoadStatus(name)
|
2022-10-21 18:50:38 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
|
|
|
|
2022-10-25 18:34:33 +08:00
|
|
|
func (b *BaseApi) LoadVariables(c *gin.Context) {
|
2022-10-31 23:52:39 +08:00
|
|
|
name, ok := c.Params.Get("name")
|
2022-10-25 18:34:33 +08:00
|
|
|
if !ok {
|
2022-10-31 23:52:39 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name in path"))
|
2022-10-25 18:34:33 +08:00
|
|
|
return
|
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
data, err := mysqlService.LoadVariables(name)
|
2022-10-21 18:50:38 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|