From 12dcb4d6469f144756aace25bdced1351881b884 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:19:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AE=80=E5=8C=96=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E4=BB=A3=E7=A0=81=20(#2704)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/auth.go | 12 +- backend/app/api/v1/backup.go | 76 ++------ backend/app/api/v1/command.go | 26 +-- backend/app/api/v1/compose_template.go | 26 +-- backend/app/api/v1/container.go | 163 ++++-------------- backend/app/api/v1/cronjob.go | 57 ++---- backend/app/api/v1/database.go | 41 +---- backend/app/api/v1/database_mysql.go | 88 +++------- backend/app/api/v1/database_redis.go | 32 +--- backend/app/api/v1/docker.go | 21 +-- backend/app/api/v1/firewall.go | 65 ++----- backend/app/api/v1/group.go | 31 +--- backend/app/api/v1/host.go | 43 +---- backend/app/api/v1/image.go | 57 +----- backend/app/api/v1/image_repo.go | 38 +--- backend/app/api/v1/logs.go | 18 +- backend/app/api/v1/monitor.go | 8 +- backend/app/api/v1/setting.go | 57 ++---- backend/app/api/v1/snapshot.go | 58 ++----- backend/app/api/v1/ssh.go | 50 +----- backend/app/api/v1/upgrade.go | 8 +- backend/app/dto/auth.go | 20 +-- backend/app/dto/container.go | 18 +- backend/app/dto/cronjob.go | 2 +- backend/app/dto/database.go | 2 +- backend/app/dto/setting.go | 4 +- cmd/server/docs/docs.go | 98 ++++++++++- cmd/server/docs/swagger.json | 98 ++++++++++- cmd/server/docs/swagger.yaml | 68 +++++++- .../container/container/operate/index.vue | 1 + .../container/container/rename/index.vue | 6 +- 31 files changed, 494 insertions(+), 798 deletions(-) diff --git a/backend/app/api/v1/auth.go b/backend/app/api/v1/auth.go index d23096028..4437ae0ea 100644 --- a/backend/app/api/v1/auth.go +++ b/backend/app/api/v1/auth.go @@ -25,10 +25,10 @@ type BaseApi struct{} // @Router /auth/login [post] func (b *BaseApi) Login(c *gin.Context) { var req dto.Login - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if req.AuthMethod != "jwt" && !req.IgnoreCaptcha { if err := captcha.VerifyCode(req.CaptchaID, req.Captcha); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -60,14 +60,10 @@ func (b *BaseApi) Login(c *gin.Context) { // @Header 200 {string} EntranceCode "安全入口" func (b *BaseApi) MFALogin(c *gin.Context) { var req dto.MFALogin - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + entranceItem := c.Request.Header.Get("EntranceCode") var entrance []byte if len(entranceItem) != 0 { diff --git a/backend/app/api/v1/backup.go b/backend/app/api/v1/backup.go index d8ca85af9..328dd642d 100644 --- a/backend/app/api/v1/backup.go +++ b/backend/app/api/v1/backup.go @@ -8,7 +8,6 @@ import ( "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" ) @@ -23,12 +22,7 @@ import ( // @x-panel-log {"bodyKeys":["type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建备份账号 [type]","formatEN":"create backup account [type]"} func (b *BaseApi) CreateBackup(c *gin.Context) { var req dto.BackupOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } if len(req.Credential) != 0 { @@ -65,12 +59,7 @@ func (b *BaseApi) CreateBackup(c *gin.Context) { // @Router /settings/backup/search [post] func (b *BaseApi) ListBuckets(c *gin.Context) { var req dto.ForBuckets - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } if len(req.Credential) != 0 { @@ -125,12 +114,7 @@ func (b *BaseApi) LoadOneDriveInfo(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":true,"db":"backup_accounts","output_column":"type","output_value":"types"}],"formatZH":"删除备份账号 [types]","formatEN":"delete backup account [types]"} func (b *BaseApi) DeleteBackup(c *gin.Context) { 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -151,8 +135,7 @@ func (b *BaseApi) DeleteBackup(c *gin.Context) { // @Router /settings/backup/record/search [post] func (b *BaseApi) SearchBackupRecords(c *gin.Context) { var req dto.RecordSearch - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -179,12 +162,7 @@ func (b *BaseApi) SearchBackupRecords(c *gin.Context) { // @x-panel-log {"bodyKeys":["source","fileName"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"下载备份记录 [source][fileName]","formatEN":"download backup records [source][fileName]"} func (b *BaseApi) DownloadRecord(c *gin.Context) { var req dto.DownloadRecord - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -207,12 +185,7 @@ func (b *BaseApi) DownloadRecord(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"backup_records","output_column":"file_name","output_value":"files"}],"formatZH":"删除备份记录 [files]","formatEN":"delete backup records [files]"} func (b *BaseApi) DeleteBackupRecord(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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -234,14 +207,10 @@ func (b *BaseApi) DeleteBackupRecord(c *gin.Context) { // @x-panel-log {"bodyKeys":["type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新备份账号 [types]","formatEN":"update backup account [types]"} func (b *BaseApi) UpdateBackup(c *gin.Context) { var req dto.BackupOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if len(req.Credential) != 0 { credential, err := base64.StdEncoding.DecodeString(req.Credential) if err != nil { @@ -292,14 +261,10 @@ func (b *BaseApi) ListBackup(c *gin.Context) { // @Router /settings/backup/search/files [post] func (b *BaseApi) LoadFilesFromBackup(c *gin.Context) { var req dto.BackupSearchFile - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + data, err := backupService.ListFiles(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -320,12 +285,7 @@ func (b *BaseApi) LoadFilesFromBackup(c *gin.Context) { // @x-panel-log {"bodyKeys":["type","name","detailName"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"备份 [type] 数据 [name][detailName]","formatEN":"backup [type] data [name][detailName]"} func (b *BaseApi) Backup(c *gin.Context) { var req dto.CommonBackup - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -365,12 +325,7 @@ func (b *BaseApi) Backup(c *gin.Context) { // @x-panel-log {"bodyKeys":["type","name","detailName","file"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [file] 恢复 [type] 数据 [name][detailName]","formatEN":"recover [type] data [name][detailName] from [file]"} func (b *BaseApi) Recover(c *gin.Context) { var req dto.CommonRecover - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -418,12 +373,7 @@ func (b *BaseApi) Recover(c *gin.Context) { // @x-panel-log {"bodyKeys":["type","name","detailName","file"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [file] 恢复 [type] 数据 [name][detailName]","formatEN":"recover [type] data [name][detailName] from [file]"} func (b *BaseApi) RecoverByUpload(c *gin.Context) { var req dto.CommonRecover - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/command.go b/backend/app/api/v1/command.go index 34224afbf..dc2e0f474 100644 --- a/backend/app/api/v1/command.go +++ b/backend/app/api/v1/command.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -19,14 +18,10 @@ import ( // @x-panel-log {"bodyKeys":["name","command"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建快捷命令 [name][command]","formatEN":"create quick command [name][command]"} func (b *BaseApi) CreateCommand(c *gin.Context) { var req dto.CommandOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := commandService.Create(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -44,8 +39,7 @@ func (b *BaseApi) CreateCommand(c *gin.Context) { // @Router /hosts/command/search [post] func (b *BaseApi) SearchCommand(c *gin.Context) { var req dto.SearchWithPage - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -88,12 +82,7 @@ func (b *BaseApi) ListCommand(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"commands","output_column":"name","output_value":"names"}],"formatZH":"删除快捷命令 [names]","formatEN":"delete quick command [names]"} func (b *BaseApi) DeleteCommand(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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -115,12 +104,7 @@ func (b *BaseApi) DeleteCommand(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新快捷命令 [name]","formatEN":"update quick command [name]"} func (b *BaseApi) UpdateCommand(c *gin.Context) { var req dto.CommandOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/compose_template.go b/backend/app/api/v1/compose_template.go index 5aaf05ef9..5fcba846d 100644 --- a/backend/app/api/v1/compose_template.go +++ b/backend/app/api/v1/compose_template.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -19,14 +18,10 @@ import ( // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建 compose 模版 [name]","formatEN":"create compose template [name]"} func (b *BaseApi) CreateComposeTemplate(c *gin.Context) { var req dto.ComposeTemplateCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := composeTemplateService.Create(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -45,8 +40,7 @@ func (b *BaseApi) CreateComposeTemplate(c *gin.Context) { // @Router /containers/template/search [post] func (b *BaseApi) SearchComposeTemplate(c *gin.Context) { var req dto.SearchWithPage - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -90,12 +84,7 @@ func (b *BaseApi) ListComposeTemplate(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"compose_templates","output_column":"name","output_value":"names"}],"formatZH":"删除 compose 模版 [names]","formatEN":"delete compose template [names]"} func (b *BaseApi) DeleteComposeTemplate(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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -117,12 +106,7 @@ func (b *BaseApi) DeleteComposeTemplate(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"compose_templates","output_column":"name","output_value":"name"}],"formatZH":"更新 compose 模版 [name]","formatEN":"update compose template information [name]"} func (b *BaseApi) UpdateComposeTemplate(c *gin.Context) { var req dto.ComposeTemplateUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/container.go b/backend/app/api/v1/container.go index 73d20ece9..ddffe07ba 100644 --- a/backend/app/api/v1/container.go +++ b/backend/app/api/v1/container.go @@ -20,12 +20,7 @@ import ( // @Router /containers/search [post] func (b *BaseApi) SearchContainer(c *gin.Context) { var req dto.PageContainer - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -67,12 +62,7 @@ func (b *BaseApi) ListContainer(c *gin.Context) { // @Router /containers/compose/search [post] func (b *BaseApi) SearchCompose(c *gin.Context) { var req dto.SearchWithPage - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -98,12 +88,7 @@ func (b *BaseApi) SearchCompose(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"检测 compose [name] 格式","formatEN":"check compose [name]"} func (b *BaseApi) TestCompose(c *gin.Context) { var req dto.ComposeCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -126,12 +111,7 @@ func (b *BaseApi) TestCompose(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建 compose [name]","formatEN":"create compose [name]"} func (b *BaseApi) CreateCompose(c *gin.Context) { var req dto.ComposeCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -154,12 +134,7 @@ func (b *BaseApi) CreateCompose(c *gin.Context) { // @x-panel-log {"bodyKeys":["name","operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"compose [operation] [name]","formatEN":"compose [operation] [name]"} func (b *BaseApi) OperatorCompose(c *gin.Context) { var req dto.ComposeOperation - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -181,14 +156,10 @@ func (b *BaseApi) OperatorCompose(c *gin.Context) { // @x-panel-log {"bodyKeys":["name","image"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新容器 [name][image]","formatEN":"update container [name][image]"} func (b *BaseApi) ContainerUpdate(c *gin.Context) { var req dto.ContainerOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := containerService.ContainerUpdate(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -206,14 +177,10 @@ func (b *BaseApi) ContainerUpdate(c *gin.Context) { // @Router /containers/info [post] func (b *BaseApi) ContainerInfo(c *gin.Context) { var req dto.OperationWithName - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + data, err := containerService.ContainerInfo(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -242,12 +209,12 @@ func (b *BaseApi) LoadResourceLimit(c *gin.Context) { // @Security ApiKeyAuth // @Router /containers/list/stats [get] func (b *BaseApi) ContainerListStats(c *gin.Context) { - datas, err := containerService.ContainerListStats() + data, err := containerService.ContainerListStats() if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } - helper.SuccessWithData(c, datas) + helper.SuccessWithData(c, data) } // @Tags Container @@ -261,14 +228,10 @@ func (b *BaseApi) ContainerListStats(c *gin.Context) { // @x-panel-log {"bodyKeys":["name","image"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建容器 [name][image]","formatEN":"create container [name][image]"} func (b *BaseApi) ContainerCreate(c *gin.Context) { var req dto.ContainerOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := containerService.ContainerCreate(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -287,14 +250,10 @@ func (b *BaseApi) ContainerCreate(c *gin.Context) { // @x-panel-log {"bodyKeys":["name","image"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新容器镜像 [name][image]","formatEN":"upgrade container image [name][image]"} func (b *BaseApi) ContainerUpgrade(c *gin.Context) { var req dto.ContainerUpgrade - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := containerService.ContainerUpgrade(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -313,14 +272,10 @@ func (b *BaseApi) ContainerUpgrade(c *gin.Context) { // @x-panel-log {"bodyKeys":["pruneType"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理容器 [pruneType]","formatEN":"clean container [pruneType]"} func (b *BaseApi) ContainerPrune(c *gin.Context) { var req dto.ContainerPrune - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + report, err := containerService.Prune(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -340,14 +295,10 @@ func (b *BaseApi) ContainerPrune(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理容器 [name] 日志","formatEN":"clean container [name] logs"} func (b *BaseApi) CleanContainerLog(c *gin.Context) { var req dto.OperationWithName - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := containerService.ContainerLogClean(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -365,10 +316,10 @@ func (b *BaseApi) CleanContainerLog(c *gin.Context) { // @Router /containers/load/log [post] func (b *BaseApi) LoadContainerLog(c *gin.Context) { var req dto.OperationWithNameAndType - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + content := containerService.LoadContainerLogs(req) helper.SuccessWithData(c, content) } @@ -384,14 +335,10 @@ func (b *BaseApi) LoadContainerLog(c *gin.Context) { // @x-panel-log {"bodyKeys":["name","newName"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"容器重命名 [name] => [newName]","formatEN":"rename container [name] => [newName]"} func (b *BaseApi) ContainerRename(c *gin.Context) { var req dto.ContainerRename - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := containerService.ContainerRename(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -410,14 +357,10 @@ func (b *BaseApi) ContainerRename(c *gin.Context) { // @x-panel-log {"bodyKeys":["names","operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"容器 [names] 执行 [operation]","formatEN":"container [operation] [names]"} func (b *BaseApi) ContainerOperation(c *gin.Context) { var req dto.ContainerOperation - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := containerService.ContainerOperation(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -457,12 +400,7 @@ func (b *BaseApi) ContainerStats(c *gin.Context) { // @Router /containers/inspect [post] func (b *BaseApi) Inspect(c *gin.Context) { var req dto.InspectReq - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -513,12 +451,7 @@ func (b *BaseApi) ContainerLogs(c *gin.Context) { // @Router /containers/network/search [post] func (b *BaseApi) SearchNetwork(c *gin.Context) { var req dto.SearchWithPage - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -561,12 +494,7 @@ func (b *BaseApi) ListNetwork(c *gin.Context) { // @x-panel-log {"bodyKeys":["names"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"删除容器网络 [names]","formatEN":"delete container network [names]"} func (b *BaseApi) DeleteNetwork(c *gin.Context) { var req dto.BatchDelete - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -588,12 +516,7 @@ func (b *BaseApi) DeleteNetwork(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建容器网络 name","formatEN":"create container network [name]"} func (b *BaseApi) CreateNetwork(c *gin.Context) { var req dto.NetworkCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -615,12 +538,7 @@ func (b *BaseApi) CreateNetwork(c *gin.Context) { // @Router /containers/volume/search [post] func (b *BaseApi) SearchVolume(c *gin.Context) { var req dto.SearchWithPage - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -663,12 +581,7 @@ func (b *BaseApi) ListVolume(c *gin.Context) { // @x-panel-log {"bodyKeys":["names"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"删除容器存储卷 [names]","formatEN":"delete container volume [names]"} func (b *BaseApi) DeleteVolume(c *gin.Context) { var req dto.BatchDelete - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -690,12 +603,7 @@ func (b *BaseApi) DeleteVolume(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建容器存储卷 [name]","formatEN":"create container volume [name]"} func (b *BaseApi) CreateVolume(c *gin.Context) { var req dto.VolumeCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -717,12 +625,7 @@ func (b *BaseApi) CreateVolume(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 compose [name]","formatEN":"update compose information [name]"} func (b *BaseApi) ComposeUpdate(c *gin.Context) { var req dto.ComposeUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/cronjob.go b/backend/app/api/v1/cronjob.go index 18f67dca8..8faee68b0 100644 --- a/backend/app/api/v1/cronjob.go +++ b/backend/app/api/v1/cronjob.go @@ -6,7 +6,6 @@ import ( "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/1Panel-dev/1Panel/backend/utils/common" "github.com/gin-gonic/gin" ) @@ -22,14 +21,10 @@ import ( // @x-panel-log {"bodyKeys":["type","name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建计划任务 [type][name]","formatEN":"create cronjob [type][name]"} func (b *BaseApi) CreateCronjob(c *gin.Context) { var req dto.CronjobCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := cronjobService.Create(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -47,8 +42,7 @@ func (b *BaseApi) CreateCronjob(c *gin.Context) { // @Router /cronjobs/search [post] func (b *BaseApi) SearchCronjob(c *gin.Context) { var req dto.SearchWithPage - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -74,10 +68,10 @@ func (b *BaseApi) SearchCronjob(c *gin.Context) { // @Router /cronjobs/search/records [post] func (b *BaseApi) SearchJobRecords(c *gin.Context) { var req dto.SearchRecord - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + loc, _ := time.LoadLocation(common.LoadTimeZone()) req.StartTime = req.StartTime.In(loc) req.EndTime = req.EndTime.In(loc) @@ -104,8 +98,7 @@ func (b *BaseApi) SearchJobRecords(c *gin.Context) { // @Router /cronjob/record/log [post] func (b *BaseApi) LoadRecordLog(c *gin.Context) { var req dto.OperateByID - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } content, err := cronjobService.LoadRecordLog(req) @@ -127,8 +120,7 @@ func (b *BaseApi) LoadRecordLog(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"清空计划任务记录 [name]","formatEN":"clean cronjob [name] records"} func (b *BaseApi) CleanRecord(c *gin.Context) { var req dto.CronjobClean - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -151,12 +143,7 @@ func (b *BaseApi) CleanRecord(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"cronjobs","output_column":"name","output_value":"names"}],"formatZH":"删除计划任务 [names]","formatEN":"delete cronjob [names]"} func (b *BaseApi) DeleteCronjob(c *gin.Context) { var req dto.CronjobBatchDelete - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -178,12 +165,7 @@ func (b *BaseApi) DeleteCronjob(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"更新计划任务 [name]","formatEN":"update cronjob [name]"} func (b *BaseApi) UpdateCronjob(c *gin.Context) { var req dto.CronjobUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -205,12 +187,7 @@ func (b *BaseApi) UpdateCronjob(c *gin.Context) { // @x-panel-log {"bodyKeys":["id","status"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"修改计划任务 [name] 状态为 [status]","formatEN":"change the status of cronjob [name] to [status]."} func (b *BaseApi) UpdateCronjobStatus(c *gin.Context) { var req dto.CronjobUpdateStatus - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -232,12 +209,7 @@ func (b *BaseApi) UpdateCronjobStatus(c *gin.Context) { // @x-panel-log {"bodyKeys":["recordID"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"recordID","isList":false,"db":"job_records","output_column":"file","output_value":"file"}],"formatZH":"下载计划任务记录 [file]","formatEN":"download the cronjob record [file]"} func (b *BaseApi) TargetDownload(c *gin.Context) { var req dto.CronjobDownload - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -261,12 +233,7 @@ func (b *BaseApi) TargetDownload(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"cronjobs","output_column":"name","output_value":"name"}],"formatZH":"手动执行计划任务 [name]","formatEN":"manually execute the cronjob [name]"} func (b *BaseApi) HandleOnce(c *gin.Context) { 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/database.go b/backend/app/api/v1/database.go index 5cc30f96d..5431411d6 100644 --- a/backend/app/api/v1/database.go +++ b/backend/app/api/v1/database.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -19,14 +18,10 @@ import ( // @x-panel-log {"bodyKeys":["name", "type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建远程数据库 [name][type]","formatEN":"create database [name][type]"} func (b *BaseApi) CreateDatabase(c *gin.Context) { var req dto.DatabaseCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := databaseService.Create(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -45,14 +40,10 @@ func (b *BaseApi) CreateDatabase(c *gin.Context) { // @x-panel-log {"bodyKeys":["name", "type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"检测远程数据库 [name][type] 连接性","formatEN":"check if database [name][type] is connectable"} func (b *BaseApi) CheckDatabase(c *gin.Context) { var req dto.DatabaseCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + helper.SuccessWithData(c, databaseService.CheckDatabase(req)) } @@ -66,8 +57,7 @@ func (b *BaseApi) CheckDatabase(c *gin.Context) { // @Router /databases/db/search [post] func (b *BaseApi) SearchDatabase(c *gin.Context) { var req dto.DatabaseSearch - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -135,12 +125,7 @@ func (b *BaseApi) GetDatabase(c *gin.Context) { // @Router /db/remote/del/check [post] func (b *BaseApi) DeleteCheckDatabase(c *gin.Context) { 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -163,12 +148,7 @@ func (b *BaseApi) DeleteCheckDatabase(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"databases","output_column":"name","output_value":"names"}],"formatZH":"删除远程数据库 [names]","formatEN":"delete database [names]"} func (b *BaseApi) DeleteDatabase(c *gin.Context) { var req dto.DatabaseDelete - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -190,12 +170,7 @@ func (b *BaseApi) DeleteDatabase(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新远程数据库 [name]","formatEN":"update database [name]"} func (b *BaseApi) UpdateDatabase(c *gin.Context) { var req dto.DatabaseUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/database_mysql.go b/backend/app/api/v1/database_mysql.go index fec2b8a9d..a3aec9b16 100644 --- a/backend/app/api/v1/database_mysql.go +++ b/backend/app/api/v1/database_mysql.go @@ -7,7 +7,6 @@ import ( "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" ) @@ -22,14 +21,10 @@ import ( // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建 mysql 数据库 [name]","formatEN":"create mysql database [name]"} 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if len(req.Password) != 0 { password, err := base64.StdEncoding.DecodeString(req.Password) if err != nil { @@ -57,14 +52,10 @@ func (b *BaseApi) CreateMysql(c *gin.Context) { // @x-panel-log {"bodyKeys":["id","description"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"mysql 数据库 [name] 描述信息修改 [description]","formatEN":"The description of the mysql database [name] is modified => [description]"} func (b *BaseApi) UpdateMysqlDescription(c *gin.Context) { var req dto.UpdateDescription - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := mysqlService.UpdateDescription(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -83,14 +74,10 @@ func (b *BaseApi) UpdateMysqlDescription(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"更新数据库 [name] 密码","formatEN":"Update database [name] password"} func (b *BaseApi) ChangeMysqlPassword(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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if len(req.Value) != 0 { value, err := base64.StdEncoding.DecodeString(req.Value) if err != nil { @@ -118,14 +105,10 @@ func (b *BaseApi) ChangeMysqlPassword(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"更新数据库 [name] 访问权限","formatEN":"Update database [name] access"} 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := mysqlService.ChangeAccess(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -144,8 +127,7 @@ func (b *BaseApi) ChangeMysqlAccess(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"调整 mysql 数据库性能参数","formatEN":"adjust mysql database performance parameters"} func (b *BaseApi) UpdateMysqlVariables(c *gin.Context) { var req dto.MysqlVariablesUpdate - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -167,12 +149,7 @@ func (b *BaseApi) UpdateMysqlVariables(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 mysql 数据库配置信息","formatEN":"update the mysql database configuration information"} 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 - } - if err := global.VALID.Struct(req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -194,8 +171,7 @@ func (b *BaseApi) UpdateMysqlConfByFile(c *gin.Context) { // @Router /databases/search [post] func (b *BaseApi) SearchMysql(c *gin.Context) { var req dto.MysqlDBSearch - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -238,14 +214,10 @@ func (b *BaseApi) ListDBName(c *gin.Context) { // @Router /databases/load [post] func (b *BaseApi) LoadDBFromRemote(c *gin.Context) { var req dto.MysqlLoadDB - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := mysqlService.LoadFromRemote(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -264,12 +236,7 @@ func (b *BaseApi) LoadDBFromRemote(c *gin.Context) { // @Router /databases/del/check [post] func (b *BaseApi) DeleteCheckMysql(c *gin.Context) { var req dto.MysqlDBDeleteCheck - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -292,12 +259,7 @@ func (b *BaseApi) DeleteCheckMysql(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"database_mysqls","output_column":"name","output_value":"name"}],"formatZH":"删除 mysql 数据库 [name]","formatEN":"delete mysql database [name]"} func (b *BaseApi) DeleteMysql(c *gin.Context) { var req dto.MysqlDBDelete - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -321,10 +283,10 @@ func (b *BaseApi) DeleteMysql(c *gin.Context) { // @Router /databases/baseinfo [post] func (b *BaseApi) LoadBaseinfo(c *gin.Context) { var req dto.OperationWithNameAndType - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + data, err := mysqlService.LoadBaseInfo(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -344,10 +306,10 @@ func (b *BaseApi) LoadBaseinfo(c *gin.Context) { // @Router /databases/load/file [post] func (b *BaseApi) LoadDatabaseFile(c *gin.Context) { var req dto.OperationWithNameAndType - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + content, err := mysqlService.LoadDatabaseFile(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -366,10 +328,10 @@ func (b *BaseApi) LoadDatabaseFile(c *gin.Context) { // @Router /databases/remote [post] func (b *BaseApi) LoadRemoteAccess(c *gin.Context) { var req dto.OperationWithNameAndType - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + isRemote, err := mysqlService.LoadRemoteAccess(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -389,10 +351,10 @@ func (b *BaseApi) LoadRemoteAccess(c *gin.Context) { // @Router /databases/status [post] func (b *BaseApi) LoadStatus(c *gin.Context) { var req dto.OperationWithNameAndType - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + data, err := mysqlService.LoadStatus(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -412,10 +374,10 @@ func (b *BaseApi) LoadStatus(c *gin.Context) { // @Router /databases/variables [post] func (b *BaseApi) LoadVariables(c *gin.Context) { var req dto.OperationWithNameAndType - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + data, err := mysqlService.LoadVariables(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) diff --git a/backend/app/api/v1/database_redis.go b/backend/app/api/v1/database_redis.go index bd9c90fbe..306f4beab 100644 --- a/backend/app/api/v1/database_redis.go +++ b/backend/app/api/v1/database_redis.go @@ -9,7 +9,6 @@ import ( "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/1Panel-dev/1Panel/backend/utils/compose" "github.com/gin-gonic/gin" ) @@ -73,14 +72,10 @@ func (b *BaseApi) LoadPersistenceConf(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 redis 数据库配置信息","formatEN":"update the redis database configuration information"} func (b *BaseApi) UpdateRedisConf(c *gin.Context) { var req dto.RedisConfUpdate - 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) + if err := helper.CheckBind(&req, c); err != nil { return } + if err := redisService.UpdateConf(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -99,14 +94,10 @@ func (b *BaseApi) UpdateRedisConf(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 redis 数据库密码","formatEN":"change the password of the redis database"} func (b *BaseApi) ChangeRedisPassword(c *gin.Context) { var req dto.ChangeRedisPass - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if len(req.Value) != 0 { value, err := base64.StdEncoding.DecodeString(req.Value) if err != nil { @@ -134,14 +125,10 @@ func (b *BaseApi) ChangeRedisPassword(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"redis 数据库持久化配置更新","formatEN":"redis database persistence configuration update"} 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := redisService.UpdatePersistenceConf(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -159,8 +146,7 @@ func (b *BaseApi) UpdateRedisPersistenceConf(c *gin.Context) { // @Router /databases/redis/backup/search [post] 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -187,10 +173,10 @@ func (b *BaseApi) RedisBackupList(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 redis 数据库配置信息","formatEN":"update the redis database configuration information"} 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + redisInfo, err := redisService.LoadConf() if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) diff --git a/backend/app/api/v1/docker.go b/backend/app/api/v1/docker.go index 5d5cfdf89..9ae5b9e2d 100644 --- a/backend/app/api/v1/docker.go +++ b/backend/app/api/v1/docker.go @@ -6,7 +6,6 @@ import ( "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" ) @@ -65,8 +64,7 @@ func (b *BaseApi) LoadDaemonJson(c *gin.Context) { // @x-panel-log {"bodyKeys":["key", "value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 docker daemon.json 配置 [key]=>[value]","formatEN":"Updated the docker daemon.json configuration [key]=>[value]"} func (b *BaseApi) UpdateDaemonJson(c *gin.Context) { var req dto.SettingUpdate - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -89,8 +87,7 @@ func (b *BaseApi) UpdateDaemonJson(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 docker daemon.json 日志配置","formatEN":"Updated the docker daemon.json log option"} func (b *BaseApi) UpdateLogOption(c *gin.Context) { var req dto.LogOption - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBind(&req, c); err != nil { return } @@ -113,12 +110,7 @@ func (b *BaseApi) UpdateLogOption(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新 docker daemon.json 配置","formatEN":"Updated the docker daemon.json configuration"} func (b *BaseApi) UpdateDaemonJsonByFile(c *gin.Context) { var req dto.DaemonJsonUpdateByFile - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -141,12 +133,7 @@ func (b *BaseApi) UpdateDaemonJsonByFile(c *gin.Context) { // @x-panel-log {"bodyKeys":["operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"docker 服务 [operation]","formatEN":"[operation] docker service"} func (b *BaseApi) OperateDocker(c *gin.Context) { var req dto.DockerOperation - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/firewall.go b/backend/app/api/v1/firewall.go index d455bae89..ab59ceed2 100644 --- a/backend/app/api/v1/firewall.go +++ b/backend/app/api/v1/firewall.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -28,18 +27,13 @@ func (b *BaseApi) LoadFirewallBaseInfo(c *gin.Context) { // @Summary Page firewall rules // @Description 获取防火墙规则列表分页 // @Accept json -// @Param request body dto.SearchWithPage true "request" +// @Param request body dto.RuleSearch true "request" // @Success 200 {object} dto.PageResult // @Security ApiKeyAuth // @Router /hosts/firewall/search [post] func (b *BaseApi) SearchFirewallRule(c *gin.Context) { var req dto.RuleSearch - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -66,12 +60,7 @@ func (b *BaseApi) SearchFirewallRule(c *gin.Context) { // @x-panel-log {"bodyKeys":["operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"[operation] 防火墙","formatEN":"[operation] firewall"} func (b *BaseApi) OperateFirewall(c *gin.Context) { var req dto.FirewallOperation - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -94,14 +83,10 @@ func (b *BaseApi) OperateFirewall(c *gin.Context) { // @x-panel-log {"bodyKeys":["port","strategy"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"添加端口规则 [strategy] [port]","formatEN":"create port rules [strategy][port]"} func (b *BaseApi) OperatePortRule(c *gin.Context) { var req dto.PortRuleOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := firewallService.OperatePortRule(req, true); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -120,14 +105,10 @@ func (b *BaseApi) OperatePortRule(c *gin.Context) { // @x-panel-log {"bodyKeys":["strategy","address"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"添加 ip 规则 [strategy] [address]","formatEN":"create address rules [strategy][address]"} func (b *BaseApi) OperateIPRule(c *gin.Context) { var req dto.AddrRuleOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := firewallService.OperateAddressRule(req, true); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -145,14 +126,10 @@ func (b *BaseApi) OperateIPRule(c *gin.Context) { // @Router /hosts/firewall/batch [post] func (b *BaseApi) BatchOperateRule(c *gin.Context) { var req dto.BatchRuleOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := firewallService.BatchOperateRule(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -170,14 +147,10 @@ func (b *BaseApi) BatchOperateRule(c *gin.Context) { // @Router /hosts/firewall/update/description [post] func (b *BaseApi) UpdateFirewallDescription(c *gin.Context) { var req dto.UpdateFirewallDescription - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := firewallService.UpdateDescription(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -195,14 +168,10 @@ func (b *BaseApi) UpdateFirewallDescription(c *gin.Context) { // @Router /hosts/firewall/update/port [post] func (b *BaseApi) UpdatePortRule(c *gin.Context) { var req dto.PortRuleUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := firewallService.UpdatePortRule(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -220,14 +189,10 @@ func (b *BaseApi) UpdatePortRule(c *gin.Context) { // @Router /hosts/firewall/update/addr [post] func (b *BaseApi) UpdateAddrRule(c *gin.Context) { var req dto.AddrRuleUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := firewallService.UpdateAddrRule(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/api/v1/group.go b/backend/app/api/v1/group.go index 36ab9f4a1..95ce11deb 100644 --- a/backend/app/api/v1/group.go +++ b/backend/app/api/v1/group.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -19,14 +18,10 @@ import ( // @x-panel-log {"bodyKeys":["name","type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建组 [name][type]","formatEN":"create group [name][type]"} func (b *BaseApi) CreateGroup(c *gin.Context) { var req dto.GroupCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := groupService.Create(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -45,12 +40,7 @@ func (b *BaseApi) CreateGroup(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"groups","output_column":"name","output_value":"name"},{"input_column":"id","input_value":"id","isList":false,"db":"groups","output_column":"type","output_value":"type"}],"formatZH":"删除组 [type][name]","formatEN":"delete group [type][name]"} func (b *BaseApi) DeleteGroup(c *gin.Context) { 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -72,14 +62,10 @@ func (b *BaseApi) DeleteGroup(c *gin.Context) { // @x-panel-log {"bodyKeys":["name","type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新组 [name][type]","formatEN":"update group [name][type]"} func (b *BaseApi) UpdateGroup(c *gin.Context) { var req dto.GroupUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := groupService.Update(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -97,12 +83,7 @@ func (b *BaseApi) UpdateGroup(c *gin.Context) { // @Router /groups/search [post] func (b *BaseApi) ListGroup(c *gin.Context) { var req dto.GroupSearch - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/host.go b/backend/app/api/v1/host.go index b033e2912..1789b028f 100644 --- a/backend/app/api/v1/host.go +++ b/backend/app/api/v1/host.go @@ -4,7 +4,6 @@ import ( "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/1Panel-dev/1Panel/backend/utils/encrypt" "github.com/gin-gonic/gin" ) @@ -20,12 +19,7 @@ import ( // @x-panel-log {"bodyKeys":["name","addr"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建主机 [name][addr]","formatEN":"create host [name][addr]"} func (b *BaseApi) CreateHost(c *gin.Context) { var req dto.HostOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -47,12 +41,7 @@ func (b *BaseApi) CreateHost(c *gin.Context) { // @Router /hosts/test/byinfo [post] func (b *BaseApi) TestByInfo(c *gin.Context) { var req dto.HostConnTest - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -89,8 +78,7 @@ func (b *BaseApi) TestByID(c *gin.Context) { // @Router /hosts/tree [post] func (b *BaseApi) HostTree(c *gin.Context) { var req dto.SearchForTree - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBind(&req, c); err != nil { return } @@ -113,8 +101,7 @@ func (b *BaseApi) HostTree(c *gin.Context) { // @Router /hosts/search [post] func (b *BaseApi) SearchHost(c *gin.Context) { var req dto.SearchHostWithPage - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -141,12 +128,7 @@ func (b *BaseApi) SearchHost(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"hosts","output_column":"addr","output_value":"addrs"}],"formatZH":"删除主机 [addrs]","formatEN":"delete host [addrs]"} func (b *BaseApi) DeleteHost(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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -168,14 +150,10 @@ func (b *BaseApi) DeleteHost(c *gin.Context) { // @x-panel-log {"bodyKeys":["name","addr"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新主机信息 [name][addr]","formatEN":"update host [name][addr]"} func (b *BaseApi) UpdateHost(c *gin.Context) { var req dto.HostOperate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + var err error if len(req.Password) != 0 && req.AuthMode == "password" { req.Password, err = hostService.EncryptHost(req.Password) @@ -238,12 +216,7 @@ func (b *BaseApi) UpdateHost(c *gin.Context) { // @x-panel-log {"bodyKeys":["id","group"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"hosts","output_column":"addr","output_value":"addr"}],"formatZH":"切换主机[addr]分组 => [group]","formatEN":"change host [addr] group => [group]"} func (b *BaseApi) UpdateHostGroup(c *gin.Context) { var req dto.ChangeHostGroup - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/image.go b/backend/app/api/v1/image.go index 4980543a1..487aa666a 100644 --- a/backend/app/api/v1/image.go +++ b/backend/app/api/v1/image.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -19,12 +18,7 @@ import ( // @Router /containers/image/search [post] func (b *BaseApi) SearchImage(c *gin.Context) { var req dto.SearchWithPage - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -67,12 +61,7 @@ func (b *BaseApi) ListImage(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"构建镜像 [name]","formatEN":"build image [name]"} func (b *BaseApi) ImageBuild(c *gin.Context) { var req dto.ImageBuild - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -96,12 +85,7 @@ func (b *BaseApi) ImageBuild(c *gin.Context) { // @x-panel-log {"bodyKeys":["repoID","imageName"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"repoID","isList":false,"db":"image_repos","output_column":"name","output_value":"reponame"}],"formatZH":"镜像拉取 [reponame][imageName]","formatEN":"image pull [reponame][imageName]"} func (b *BaseApi) ImagePull(c *gin.Context) { var req dto.ImagePull - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -125,12 +109,7 @@ func (b *BaseApi) ImagePull(c *gin.Context) { // @x-panel-log {"bodyKeys":["repoID","tagName","name"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"repoID","isList":false,"db":"image_repos","output_column":"name","output_value":"reponame"}],"formatZH":"[tagName] 推送到 [reponame][name]","formatEN":"push [tagName] to [reponame][name]"} func (b *BaseApi) ImagePush(c *gin.Context) { var req dto.ImagePush - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -154,12 +133,7 @@ func (b *BaseApi) ImagePush(c *gin.Context) { // @x-panel-log {"bodyKeys":["names"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"移除镜像 [names]","formatEN":"remove image [names]"} func (b *BaseApi) ImageRemove(c *gin.Context) { var req dto.BatchDelete - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -182,12 +156,7 @@ func (b *BaseApi) ImageRemove(c *gin.Context) { // @x-panel-log {"bodyKeys":["tagName","path","name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"保留 [tagName] 为 [path]/[name]","formatEN":"save [tagName] as [path]/[name]"} func (b *BaseApi) ImageSave(c *gin.Context) { var req dto.ImageSave - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -210,12 +179,7 @@ func (b *BaseApi) ImageSave(c *gin.Context) { // @x-panel-log {"bodyKeys":["repoID","targetName"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"repoID","isList":false,"db":"image_repos","output_column":"name","output_value":"reponame"}],"formatZH":"tag 镜像 [reponame][targetName]","formatEN":"tag image [reponame][targetName]"} func (b *BaseApi) ImageTag(c *gin.Context) { var req dto.ImageTag - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -238,12 +202,7 @@ func (b *BaseApi) ImageTag(c *gin.Context) { // @x-panel-log {"bodyKeys":["path"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [path] 加载镜像","formatEN":"load image from [path]"} func (b *BaseApi) ImageLoad(c *gin.Context) { var req dto.ImageLoad - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/image_repo.go b/backend/app/api/v1/image_repo.go index f3e1a8537..b6e1c5c42 100644 --- a/backend/app/api/v1/image_repo.go +++ b/backend/app/api/v1/image_repo.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -19,12 +18,7 @@ import ( // @Router /containers/repo/search [post] func (b *BaseApi) SearchRepo(c *gin.Context) { var req dto.SearchWithPage - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -68,14 +62,10 @@ func (b *BaseApi) ListRepo(c *gin.Context) { // @Router /containers/repo/status [get] func (b *BaseApi) CheckRepoStatus(c *gin.Context) { 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := imageRepoService.Login(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -95,14 +85,10 @@ func (b *BaseApi) CheckRepoStatus(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建镜像仓库 [name]","formatEN":"create image repo [name]"} func (b *BaseApi) CreateRepo(c *gin.Context) { var req dto.ImageRepoCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := imageRepoService.Create(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -122,12 +108,7 @@ func (b *BaseApi) CreateRepo(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"image_repos","output_column":"name","output_value":"names"}],"formatZH":"删除镜像仓库 [names]","formatEN":"delete image repo [names]"} func (b *BaseApi) DeleteRepo(c *gin.Context) { var req dto.ImageRepoDelete - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -150,12 +131,7 @@ func (b *BaseApi) DeleteRepo(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"image_repos","output_column":"name","output_value":"name"}],"formatZH":"更新镜像仓库 [name]","formatEN":"update image repo information [name]"} func (b *BaseApi) UpdateRepo(c *gin.Context) { var req dto.ImageRepoUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/logs.go b/backend/app/api/v1/logs.go index 1121d22ac..72563328d 100644 --- a/backend/app/api/v1/logs.go +++ b/backend/app/api/v1/logs.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -18,8 +17,7 @@ import ( // @Router /logs/login [post] func (b *BaseApi) GetLoginLogs(c *gin.Context) { var req dto.SearchLgLogWithPage - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -45,8 +43,7 @@ func (b *BaseApi) GetLoginLogs(c *gin.Context) { // @Router /logs/operation [post] func (b *BaseApi) GetOperationLogs(c *gin.Context) { var req dto.SearchOpLogWithPage - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -73,12 +70,7 @@ func (b *BaseApi) GetOperationLogs(c *gin.Context) { // @x-panel-log {"bodyKeys":["logType"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清空 [logType] 日志信息","formatEN":"Clean the [logType] log information"} func (b *BaseApi) CleanLogs(c *gin.Context) { var req dto.CleanLog - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -114,10 +106,10 @@ func (b *BaseApi) GetSystemFiles(c *gin.Context) { // @Router /logs/system [post] func (b *BaseApi) GetSystemLogs(c *gin.Context) { var req dto.OperationWithName - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + data, err := logService.LoadSystemLog(req.Name) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) diff --git a/backend/app/api/v1/monitor.go b/backend/app/api/v1/monitor.go index 5173f121d..69929ff9b 100644 --- a/backend/app/api/v1/monitor.go +++ b/backend/app/api/v1/monitor.go @@ -17,14 +17,10 @@ import ( func (b *BaseApi) LoadMonitor(c *gin.Context) { var req dto.MonitorSearch - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + loc, _ := time.LoadLocation(common.LoadTimeZone()) req.StartTime = req.StartTime.In(loc) req.EndTime = req.EndTime.In(loc) diff --git a/backend/app/api/v1/setting.go b/backend/app/api/v1/setting.go index 0b606485a..6884f535f 100644 --- a/backend/app/api/v1/setting.go +++ b/backend/app/api/v1/setting.go @@ -49,12 +49,7 @@ func (b *BaseApi) GetSystemAvailable(c *gin.Context) { // @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改系统配置 [key] => [value]","formatEN":"update system setting [key] => [value]"} func (b *BaseApi) UpdateSetting(c *gin.Context) { var req dto.SettingUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -76,12 +71,7 @@ func (b *BaseApi) UpdateSetting(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改系统密码","formatEN":"update system password"} func (b *BaseApi) UpdatePassword(c *gin.Context) { var req dto.PasswordUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -103,12 +93,7 @@ func (b *BaseApi) UpdatePassword(c *gin.Context) { // @x-panel-log {"bodyKeys":["ssl"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改系统 ssl => [ssl]","formatEN":"update system ssl => [ssl]"} func (b *BaseApi) UpdateSSL(c *gin.Context) { var req dto.SSLUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -177,12 +162,7 @@ func (b *BaseApi) LoadInterfaceAddr(c *gin.Context) { // @x-panel-log {"bodyKeys":["ipv6", "bindAddress"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改系统监听信息 => ipv6: [ipv6], 监听 IP: [bindAddress]","formatEN":"update system bind info => ipv6: [ipv6], 监听 IP: [bindAddress]"} func (b *BaseApi) UpdateBindInfo(c *gin.Context) { var req dto.BindInfo - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -204,12 +184,7 @@ func (b *BaseApi) UpdateBindInfo(c *gin.Context) { // @x-panel-log {"bodyKeys":["serverPort"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改系统端口 => [serverPort]","formatEN":"update system port => [serverPort]"} func (b *BaseApi) UpdatePort(c *gin.Context) { var req dto.PortUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -231,12 +206,7 @@ func (b *BaseApi) UpdatePort(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"重置过期密码","formatEN":"reset an expired Password"} func (b *BaseApi) HandlePasswordExpired(c *gin.Context) { var req dto.PasswordUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -273,10 +243,10 @@ func (b *BaseApi) LoadTimeZone(c *gin.Context) { // @x-panel-log {"bodyKeys":["ntpSite"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"系统时间同步[ntpSite]","formatEN":"sync system time [ntpSite]"} func (b *BaseApi) SyncTime(c *gin.Context) { var req dto.SyncTime - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := settingService.SyncTime(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -340,10 +310,10 @@ func (b *BaseApi) ScanSystem(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"清理系统垃圾文件","formatEN":"Clean system junk files"} func (b *BaseApi) SystemClean(c *gin.Context) { var req []dto.Clean - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBind(&req, c); err != nil { return } + settingService.SystemClean(req) helper.SuccessWithData(c, nil) @@ -359,8 +329,7 @@ func (b *BaseApi) SystemClean(c *gin.Context) { // @Router /settings/mfa [post] func (b *BaseApi) LoadMFA(c *gin.Context) { var req dto.MfaRequest - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -384,10 +353,10 @@ func (b *BaseApi) LoadMFA(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"mfa 绑定","formatEN":"bind mfa"} func (b *BaseApi) MFABind(c *gin.Context) { var req dto.MfaCredential - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + success := mfa.ValidCode(req.Code, req.Interval, req.Secret) if !success { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("code is not valid")) diff --git a/backend/app/api/v1/snapshot.go b/backend/app/api/v1/snapshot.go index 3f18d20d4..44bf1ceb2 100644 --- a/backend/app/api/v1/snapshot.go +++ b/backend/app/api/v1/snapshot.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -19,14 +18,10 @@ import ( // @x-panel-log {"bodyKeys":["from", "description"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建系统快照 [description] 到 [from]","formatEN":"Create system backup [description] to [from]"} func (b *BaseApi) CreateSnapshot(c *gin.Context) { var req dto.SnapshotCreate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := snapshotService.SnapshotCreate(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -45,14 +40,10 @@ func (b *BaseApi) CreateSnapshot(c *gin.Context) { // @x-panel-log {"bodyKeys":["from", "names"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"从 [from] 同步系统快照 [names]","formatEN":"Sync system snapshots [names] from [from]"} func (b *BaseApi) ImportSnapshot(c *gin.Context) { var req dto.SnapshotImport - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := snapshotService.SnapshotImport(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -70,14 +61,10 @@ func (b *BaseApi) ImportSnapshot(c *gin.Context) { // @Router /settings/snapshot/status [post] func (b *BaseApi) LoadSnapShotStatus(c *gin.Context) { 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + data, err := snapshotService.LoadSnapShotStatus(req.ID) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -97,14 +84,10 @@ func (b *BaseApi) LoadSnapShotStatus(c *gin.Context) { // @x-panel-log {"bodyKeys":["id","description"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"snapshots","output_column":"name","output_value":"name"}],"formatZH":"快照 [name] 描述信息修改 [description]","formatEN":"The description of the snapshot [name] is modified => [description]"} func (b *BaseApi) UpdateSnapDescription(c *gin.Context) { var req dto.UpdateDescription - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := snapshotService.UpdateDescription(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return @@ -122,10 +105,10 @@ func (b *BaseApi) UpdateSnapDescription(c *gin.Context) { // @Router /settings/snapshot/search [post] func (b *BaseApi) SearchSnapshot(c *gin.Context) { var req dto.SearchWithPage - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + total, accounts, err := snapshotService.SearchWithPage(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -148,12 +131,7 @@ func (b *BaseApi) SearchSnapshot(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"snapshots","output_column":"name","output_value":"name"}],"formatZH":"从系统快照 [name] 恢复","formatEN":"Recover from system backup [name]"} func (b *BaseApi) RecoverSnapshot(c *gin.Context) { var req dto.SnapshotRecover - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -175,12 +153,7 @@ func (b *BaseApi) RecoverSnapshot(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"snapshots","output_column":"name","output_value":"name"}],"formatZH":"从系统快照 [name] 回滚","formatEN":"Rollback from system backup [name]"} func (b *BaseApi) RollbackSnapshot(c *gin.Context) { var req dto.SnapshotRecover - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -202,12 +175,7 @@ func (b *BaseApi) RollbackSnapshot(c *gin.Context) { // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"snapshots","output_column":"name","output_value":"name"}],"formatZH":"删除系统快照 [name]","formatEN":"Delete system backup [name]"} func (b *BaseApi) DeleteSnapshot(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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/ssh.go b/backend/app/api/v1/ssh.go index d600bd841..80bada30d 100644 --- a/backend/app/api/v1/ssh.go +++ b/backend/app/api/v1/ssh.go @@ -4,7 +4,6 @@ import ( "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" ) @@ -33,12 +32,7 @@ func (b *BaseApi) GetSSHInfo(c *gin.Context) { // @x-panel-log {"bodyKeys":["operation"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"[operation] SSH ","formatEN":"[operation] SSH"} func (b *BaseApi) OperateSSH(c *gin.Context) { var req dto.Operate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -60,12 +54,7 @@ func (b *BaseApi) OperateSSH(c *gin.Context) { // @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 SSH 配置 [key] => [value]","formatEN":"update SSH setting [key] => [value]"} func (b *BaseApi) UpdateSSH(c *gin.Context) { var req dto.SettingUpdate - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -87,12 +76,7 @@ func (b *BaseApi) UpdateSSH(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改 SSH 配置文件","formatEN":"update SSH conf"} func (b *BaseApi) UpdateSSHByfile(c *gin.Context) { var req dto.SSHConf - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -114,12 +98,7 @@ func (b *BaseApi) UpdateSSHByfile(c *gin.Context) { // @x-panel-log {"bodyKeys":[],"paramKeys":[],"BeforeFunctions":[],"formatZH":"生成 SSH 密钥 ","formatEN":"generate SSH secret"} func (b *BaseApi) GenerateSSH(c *gin.Context) { var req dto.GenerateSSH - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -140,12 +119,7 @@ func (b *BaseApi) GenerateSSH(c *gin.Context) { // @Router /host/ssh/secret [post] func (b *BaseApi) LoadSSHSecret(c *gin.Context) { var req dto.GenerateLoad - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -167,12 +141,7 @@ func (b *BaseApi) LoadSSHSecret(c *gin.Context) { // @Router /host/ssh/log/analysis [post] func (b *BaseApi) AnalysisLog(c *gin.Context) { var req dto.SearchForAnalysis - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } @@ -194,12 +163,7 @@ func (b *BaseApi) AnalysisLog(c *gin.Context) { // @Router /host/ssh/log [post] func (b *BaseApi) LoadSSHLogs(c *gin.Context) { var req dto.SearchSSHLog - 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) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/api/v1/upgrade.go b/backend/app/api/v1/upgrade.go index 772e7fdf3..f16d813f9 100644 --- a/backend/app/api/v1/upgrade.go +++ b/backend/app/api/v1/upgrade.go @@ -32,10 +32,10 @@ func (b *BaseApi) GetUpgradeInfo(c *gin.Context) { // @Router /settings/upgrade [get] func (b *BaseApi) GetNotesByVersion(c *gin.Context) { var req dto.Upgrade - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + notes, err := upgradeService.LoadNotes(req) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) @@ -55,10 +55,10 @@ func (b *BaseApi) GetNotesByVersion(c *gin.Context) { // @x-panel-log {"bodyKeys":["version"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新系统 => [version]","formatEN":"upgrade service => [version]"} func (b *BaseApi) Upgrade(c *gin.Context) { var req dto.Upgrade - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } + if err := upgradeService.Upgrade(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/dto/auth.go b/backend/app/dto/auth.go index 3d6e0084b..9f46df000 100644 --- a/backend/app/dto/auth.go +++ b/backend/app/dto/auth.go @@ -12,19 +12,19 @@ type UserLoginInfo struct { } type MfaRequest struct { - Title string `json:"title"` - Interval int `json:"interval"` + Title string `json:"title" validate:"required"` + Interval int `json:"interval" validate:"required"` } type MfaCredential struct { - Secret string `json:"secret"` - Code string `json:"code"` - Interval string `json:"interval"` + Secret string `json:"secret" validate:"required"` + Code string `json:"code" validate:"required"` + Interval string `json:"interval" validate:"required"` } type Login struct { - Name string `json:"name"` - Password string `json:"password"` + Name string `json:"name" validate:"required"` + Password string `json:"password" validate:"required"` IgnoreCaptcha bool `json:"ignoreCaptcha"` Captcha string `json:"captcha"` CaptchaID string `json:"captchaID"` @@ -33,8 +33,8 @@ type Login struct { } type MFALogin struct { - Name string `json:"name"` - Password string `json:"password"` - Code string `json:"code"` + Name string `json:"name" validate:"required"` + Password string `json:"password" validate:"required"` + Code string `json:"code" validate:"required"` AuthMethod string `json:"authMethod"` } diff --git a/backend/app/dto/container.go b/backend/app/dto/container.go index 99a8e2bbe..0648e90c0 100644 --- a/backend/app/dto/container.go +++ b/backend/app/dto/container.go @@ -12,8 +12,8 @@ type PageContainer struct { } type InspectReq struct { - ID string `json:"id"` - Type string `json:"type"` + ID string `json:"id" validate:"required"` + Type string `json:"type" validate:"required"` } type ContainerInfo struct { @@ -40,8 +40,8 @@ type ResourceLimit struct { type ContainerOperate struct { ContainerID string `json:"containerID"` ForcePull bool `json:"forcePull"` - Name string `json:"name"` - Image string `json:"image"` + Name string `json:"name" validate:"required"` + Image string `json:"image" validate:"required"` Network string `json:"network"` PublishAllPorts bool `json:"publishAllPorts"` ExposedPorts []PortHelper `json:"exposedPorts"` @@ -110,7 +110,7 @@ type ContainerOperation struct { type ContainerRename struct { Name string `json:"name" validate:"required"` - NewName string `json:"newName"` + NewName string `json:"newName" validate:"required"` } type ContainerPrune struct { @@ -135,8 +135,8 @@ type Network struct { Attachable bool `json:"attachable"` } type NetworkCreate struct { - Name string `json:"name"` - Driver string `json:"driver"` + Name string `json:"name" validate:"required"` + Driver string `json:"driver" validate:"required"` Options []string `json:"options"` Subnet string `json:"subnet"` Gateway string `json:"gateway"` @@ -152,8 +152,8 @@ type Volume struct { CreatedAt time.Time `json:"createdAt"` } type VolumeCreate struct { - Name string `json:"name"` - Driver string `json:"driver"` + Name string `json:"name" validate:"required"` + Driver string `json:"driver" validate:"required"` Options []string `json:"options"` Labels []string `json:"labels"` } diff --git a/backend/app/dto/cronjob.go b/backend/app/dto/cronjob.go index cd838ab2f..8c044e482 100644 --- a/backend/app/dto/cronjob.go +++ b/backend/app/dto/cronjob.go @@ -65,7 +65,7 @@ type CronjobClean struct { type CronjobBatchDelete struct { CleanData bool `json:"cleanData"` - IDs []uint `json:"ids"` + IDs []uint `json:"ids" validate:"required"` } type CronjobInfo struct { diff --git a/backend/app/dto/database.go b/backend/app/dto/database.go index 50e5d058d..970f2b58e 100644 --- a/backend/app/dto/database.go +++ b/backend/app/dto/database.go @@ -170,7 +170,7 @@ type RedisConfPersistenceUpdate struct { Save string `json:"save"` } type RedisConfUpdateByFile struct { - File string `json:"file"` + File string `json:"file" validate:"required"` RestartNow bool `json:"restartNow"` } diff --git a/backend/app/dto/setting.go b/backend/app/dto/setting.go index 3e099dcaf..0442fce69 100644 --- a/backend/app/dto/setting.go +++ b/backend/app/dto/setting.go @@ -135,7 +135,7 @@ type UpgradeInfo struct { } type SyncTime struct { - NtpSite string `json:"ntpSite"` + NtpSite string `json:"ntpSite" validate:"required"` } type BindInfo struct { @@ -144,7 +144,7 @@ type BindInfo struct { } type Upgrade struct { - Version string `json:"version"` + Version string `json:"version" validate:"required"` } type CleanData struct { diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 853423973..33a75d3d8 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -7377,7 +7377,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.SearchWithPage" + "$ref": "#/definitions/dto.RuleSearch" } } ], @@ -12983,6 +12983,10 @@ const docTemplate = `{ }, "dto.ContainerOperate": { "type": "object", + "required": [ + "image", + "name" + ], "properties": { "autoRemove": { "type": "boolean" @@ -13122,7 +13126,8 @@ const docTemplate = `{ "dto.ContainerRename": { "type": "object", "required": [ - "name" + "name", + "newName" ], "properties": { "name": { @@ -13182,6 +13187,9 @@ const docTemplate = `{ }, "dto.CronjobBatchDelete": { "type": "object", + "required": [ + "ids" + ], "properties": { "cleanData": { "type": "boolean" @@ -14272,6 +14280,10 @@ const docTemplate = `{ }, "dto.InspectReq": { "type": "object", + "required": [ + "id", + "type" + ], "properties": { "id": { "type": "string" @@ -14294,6 +14306,10 @@ const docTemplate = `{ }, "dto.Login": { "type": "object", + "required": [ + "name", + "password" + ], "properties": { "authMethod": { "type": "string" @@ -14320,6 +14336,11 @@ const docTemplate = `{ }, "dto.MFALogin": { "type": "object", + "required": [ + "code", + "name", + "password" + ], "properties": { "authMethod": { "type": "string" @@ -14337,6 +14358,11 @@ const docTemplate = `{ }, "dto.MfaCredential": { "type": "object", + "required": [ + "code", + "interval", + "secret" + ], "properties": { "code": { "type": "string" @@ -14751,6 +14777,10 @@ const docTemplate = `{ }, "dto.NetworkCreate": { "type": "object", + "required": [ + "driver", + "name" + ], "properties": { "driver": { "type": "string" @@ -15111,6 +15141,9 @@ const docTemplate = `{ }, "dto.RedisConfUpdateByFile": { "type": "object", + "required": [ + "file" + ], "properties": { "file": { "type": "string" @@ -15189,6 +15222,34 @@ const docTemplate = `{ } } }, + "dto.RuleSearch": { + "type": "object", + "required": [ + "page", + "pageSize", + "type" + ], + "properties": { + "info": { + "type": "string" + }, + "page": { + "type": "integer" + }, + "pageSize": { + "type": "integer" + }, + "status": { + "type": "string" + }, + "strategy": { + "type": "string" + }, + "type": { + "type": "string" + } + } + }, "dto.SSHConf": { "type": "object", "properties": { @@ -15706,6 +15767,9 @@ const docTemplate = `{ }, "dto.SyncTime": { "type": "object", + "required": [ + "ntpSite" + ], "properties": { "ntpSite": { "type": "string" @@ -15770,6 +15834,9 @@ const docTemplate = `{ }, "dto.Upgrade": { "type": "object", + "required": [ + "version" + ], "properties": { "version": { "type": "string" @@ -15806,6 +15873,10 @@ const docTemplate = `{ }, "dto.VolumeCreate": { "type": "object", + "required": [ + "driver", + "name" + ], "properties": { "driver": { "type": "string" @@ -16502,6 +16573,17 @@ const docTemplate = `{ } } }, + "request.ExposedPort": { + "type": "object", + "properties": { + "containerPort": { + "type": "integer" + }, + "hostPort": { + "type": "integer" + } + } + }, "request.FavoriteCreate": { "type": "object", "required": [ @@ -17270,6 +17352,12 @@ const docTemplate = `{ "codeDir": { "type": "string" }, + "exposedPorts": { + "type": "array", + "items": { + "$ref": "#/definitions/request.ExposedPort" + } + }, "image": { "type": "string" }, @@ -17355,6 +17443,12 @@ const docTemplate = `{ "codeDir": { "type": "string" }, + "exposedPorts": { + "type": "array", + "items": { + "$ref": "#/definitions/request.ExposedPort" + } + }, "id": { "type": "integer" }, diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 44b4b7549..1a9053808 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -7370,7 +7370,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.SearchWithPage" + "$ref": "#/definitions/dto.RuleSearch" } } ], @@ -12976,6 +12976,10 @@ }, "dto.ContainerOperate": { "type": "object", + "required": [ + "image", + "name" + ], "properties": { "autoRemove": { "type": "boolean" @@ -13115,7 +13119,8 @@ "dto.ContainerRename": { "type": "object", "required": [ - "name" + "name", + "newName" ], "properties": { "name": { @@ -13175,6 +13180,9 @@ }, "dto.CronjobBatchDelete": { "type": "object", + "required": [ + "ids" + ], "properties": { "cleanData": { "type": "boolean" @@ -14265,6 +14273,10 @@ }, "dto.InspectReq": { "type": "object", + "required": [ + "id", + "type" + ], "properties": { "id": { "type": "string" @@ -14287,6 +14299,10 @@ }, "dto.Login": { "type": "object", + "required": [ + "name", + "password" + ], "properties": { "authMethod": { "type": "string" @@ -14313,6 +14329,11 @@ }, "dto.MFALogin": { "type": "object", + "required": [ + "code", + "name", + "password" + ], "properties": { "authMethod": { "type": "string" @@ -14330,6 +14351,11 @@ }, "dto.MfaCredential": { "type": "object", + "required": [ + "code", + "interval", + "secret" + ], "properties": { "code": { "type": "string" @@ -14744,6 +14770,10 @@ }, "dto.NetworkCreate": { "type": "object", + "required": [ + "driver", + "name" + ], "properties": { "driver": { "type": "string" @@ -15104,6 +15134,9 @@ }, "dto.RedisConfUpdateByFile": { "type": "object", + "required": [ + "file" + ], "properties": { "file": { "type": "string" @@ -15182,6 +15215,34 @@ } } }, + "dto.RuleSearch": { + "type": "object", + "required": [ + "page", + "pageSize", + "type" + ], + "properties": { + "info": { + "type": "string" + }, + "page": { + "type": "integer" + }, + "pageSize": { + "type": "integer" + }, + "status": { + "type": "string" + }, + "strategy": { + "type": "string" + }, + "type": { + "type": "string" + } + } + }, "dto.SSHConf": { "type": "object", "properties": { @@ -15699,6 +15760,9 @@ }, "dto.SyncTime": { "type": "object", + "required": [ + "ntpSite" + ], "properties": { "ntpSite": { "type": "string" @@ -15763,6 +15827,9 @@ }, "dto.Upgrade": { "type": "object", + "required": [ + "version" + ], "properties": { "version": { "type": "string" @@ -15799,6 +15866,10 @@ }, "dto.VolumeCreate": { "type": "object", + "required": [ + "driver", + "name" + ], "properties": { "driver": { "type": "string" @@ -16495,6 +16566,17 @@ } } }, + "request.ExposedPort": { + "type": "object", + "properties": { + "containerPort": { + "type": "integer" + }, + "hostPort": { + "type": "integer" + } + } + }, "request.FavoriteCreate": { "type": "object", "required": [ @@ -17263,6 +17345,12 @@ "codeDir": { "type": "string" }, + "exposedPorts": { + "type": "array", + "items": { + "$ref": "#/definitions/request.ExposedPort" + } + }, "image": { "type": "string" }, @@ -17348,6 +17436,12 @@ "codeDir": { "type": "string" }, + "exposedPorts": { + "type": "array", + "items": { + "$ref": "#/definitions/request.ExposedPort" + } + }, "id": { "type": "integer" }, diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index 8ba2ad0dd..aab8f9ee9 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -428,6 +428,9 @@ definitions: items: $ref: '#/definitions/dto.VolumeHelper' type: array + required: + - image + - name type: object dto.ContainerOperation: properties: @@ -478,6 +481,7 @@ definitions: type: string required: - name + - newName type: object dto.ContainerStats: properties: @@ -518,6 +522,8 @@ definitions: items: type: integer type: array + required: + - ids type: object dto.CronjobClean: properties: @@ -1251,6 +1257,9 @@ definitions: type: string type: type: string + required: + - id + - type type: object dto.LogOption: properties: @@ -1275,6 +1284,9 @@ definitions: type: string password: type: string + required: + - name + - password type: object dto.MFALogin: properties: @@ -1286,6 +1298,10 @@ definitions: type: string password: type: string + required: + - code + - name + - password type: object dto.MfaCredential: properties: @@ -1295,6 +1311,10 @@ definitions: type: string secret: type: string + required: + - code + - interval + - secret type: object dto.MysqlConfUpdateByFile: properties: @@ -1588,6 +1608,9 @@ definitions: type: array subnet: type: string + required: + - driver + - name type: object dto.NginxKey: enum: @@ -1820,6 +1843,8 @@ definitions: type: string restartNow: type: boolean + required: + - file type: object dto.RedisPersistence: properties: @@ -1866,6 +1891,25 @@ definitions: memory: type: integer type: object + dto.RuleSearch: + properties: + info: + type: string + page: + type: integer + pageSize: + type: integer + status: + type: string + strategy: + type: string + type: + type: string + required: + - page + - pageSize + - type + type: object dto.SSHConf: properties: file: @@ -2214,6 +2258,8 @@ definitions: properties: ntpSite: type: string + required: + - ntpSite type: object dto.TreeChild: properties: @@ -2256,6 +2302,8 @@ definitions: properties: version: type: string + required: + - version type: object dto.UpgradeInfo: properties: @@ -2289,6 +2337,9 @@ definitions: items: type: string type: array + required: + - driver + - name type: object dto.VolumeHelper: properties: @@ -2737,6 +2788,13 @@ definitions: required: - path type: object + request.ExposedPort: + properties: + containerPort: + type: integer + hostPort: + type: integer + type: object request.FavoriteCreate: properties: path: @@ -3253,6 +3311,10 @@ definitions: type: boolean codeDir: type: string + exposedPorts: + items: + $ref: '#/definitions/request.ExposedPort' + type: array image: type: string install: @@ -3309,6 +3371,10 @@ definitions: type: boolean codeDir: type: string + exposedPorts: + items: + $ref: '#/definitions/request.ExposedPort' + type: array id: type: integer image: @@ -8926,7 +8992,7 @@ paths: name: request required: true schema: - $ref: '#/definitions/dto.SearchWithPage' + $ref: '#/definitions/dto.RuleSearch' responses: "200": description: OK diff --git a/frontend/src/views/container/container/operate/index.vue b/frontend/src/views/container/container/operate/index.vue index 4b81b0f26..563144be0 100644 --- a/frontend/src/views/container/container/operate/index.vue +++ b/frontend/src/views/container/container/operate/index.vue @@ -521,6 +521,7 @@ const updateContainerID = async () => { let params = { page: 1, pageSize: 1, + state: 'all', name: dialogData.value.rowData.name, filters: '', orderBy: 'created_at', diff --git a/frontend/src/views/container/container/rename/index.vue b/frontend/src/views/container/container/rename/index.vue index 314d478ff..bef920a1a 100644 --- a/frontend/src/views/container/container/rename/index.vue +++ b/frontend/src/views/container/container/rename/index.vue @@ -6,7 +6,11 @@ - +