2022-10-20 18:45:47 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-12-21 15:54:34 +08:00
|
|
|
"context"
|
2022-12-24 13:31:30 +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
|
|
|
|
}
|
2022-12-21 15:54:34 +08:00
|
|
|
if _, err := mysqlService.Create(context.Background(), req); err != nil {
|
2022-10-20 18:45:47 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-12-24 13:31:30 +08:00
|
|
|
func (b *BaseApi) UpdateMysqlDescription(c *gin.Context) {
|
|
|
|
var req dto.MysqlDescription
|
|
|
|
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.UpdateDescription(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-12-08 19:07:32 +08:00
|
|
|
func (b *BaseApi) ChangeMysqlPassword(c *gin.Context) {
|
2022-10-24 18:46:19 +08:00
|
|
|
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
|
|
|
|
}
|
2022-12-08 19:07:32 +08:00
|
|
|
if err := mysqlService.ChangePassword(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) ChangeMysqlAccess(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.ChangeAccess(req); err != nil {
|
2022-10-24 18:46:19 +08:00
|
|
|
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
|
|
|
|
2022-11-18 16:14:23 +08:00
|
|
|
if err := mysqlService.UpdateVariables(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
|
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-02 18:30:22 +08:00
|
|
|
|
2022-11-09 15:08:38 +08:00
|
|
|
if err := mysqlService.UpdateConfByFile(req); err != nil {
|
2022-11-02 18:30:22 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-10-20 18:45:47 +08:00
|
|
|
func (b *BaseApi) SearchMysql(c *gin.Context) {
|
2022-11-18 16:14:23 +08:00
|
|
|
var req dto.PageInfo
|
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-11-18 16:14:23 +08:00
|
|
|
func (b *BaseApi) ListDBName(c *gin.Context) {
|
|
|
|
list, err := mysqlService.ListDBName()
|
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) BackupMysql(c *gin.Context) {
|
|
|
|
var req dto.BackupDB
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-27 23:09:39 +08:00
|
|
|
|
|
|
|
if err := mysqlService.Backup(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:08:38 +08:00
|
|
|
func (b *BaseApi) RecoverMysqlByUpload(c *gin.Context) {
|
|
|
|
var req dto.UploadRecover
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-09 15:08:38 +08:00
|
|
|
|
|
|
|
if err := mysqlService.RecoverByUpload(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:09:39 +08:00
|
|
|
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
|
|
|
|
}
|
2022-12-13 18:54:28 +08:00
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-10-27 23:09:39 +08:00
|
|
|
|
|
|
|
if err := mysqlService.Recover(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-12-04 17:28:03 +08:00
|
|
|
func (b *BaseApi) DeleteCheckMysql(c *gin.Context) {
|
2022-12-13 18:54:28 +08:00
|
|
|
var req dto.OperateByID
|
|
|
|
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)
|
2022-10-20 18:45:47 +08:00
|
|
|
return
|
|
|
|
}
|
2022-12-04 17:28:03 +08:00
|
|
|
|
2022-12-13 18:54:28 +08:00
|
|
|
apps, err := mysqlService.DeleteCheck(req.ID)
|
2022-12-04 17:28:03 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, apps)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) DeleteMysql(c *gin.Context) {
|
2022-12-26 14:47:08 +08:00
|
|
|
var req dto.MysqlDBDelete
|
2022-12-13 18:54:28 +08: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)
|
2022-10-20 18:45:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-27 16:30:25 +08:00
|
|
|
tx, ctx := helper.GetTxAndContext()
|
|
|
|
if err := mysqlService.Delete(ctx, req); err != nil {
|
2022-10-20 18:45:47 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
2022-12-27 16:30:25 +08:00
|
|
|
tx.Rollback()
|
2022-10-20 18:45:47 +08:00
|
|
|
return
|
|
|
|
}
|
2022-12-27 16:30:25 +08:00
|
|
|
tx.Commit()
|
2022-10-20 18:45:47 +08:00
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
2022-10-21 18:50:38 +08:00
|
|
|
|
2022-10-25 18:34:33 +08:00
|
|
|
func (b *BaseApi) LoadBaseinfo(c *gin.Context) {
|
2022-11-18 16:14:23 +08:00
|
|
|
data, err := mysqlService.LoadBaseInfo()
|
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-12-09 14:07:18 +08:00
|
|
|
func (b *BaseApi) LoadRemoteAccess(c *gin.Context) {
|
|
|
|
isRemote, err := mysqlService.LoadRemoteAccess()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, isRemote)
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:50:38 +08:00
|
|
|
func (b *BaseApi) LoadStatus(c *gin.Context) {
|
2022-11-18 16:14:23 +08:00
|
|
|
data, err := mysqlService.LoadStatus()
|
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-11-18 16:14:23 +08:00
|
|
|
data, err := mysqlService.LoadVariables()
|
2022-10-21 18:50:38 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|