Skip to content

Commit

Permalink
feat: 规范统一使用 422 状态码
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 28, 2023
1 parent 08a4433 commit fe4a6e2
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 208 deletions.
30 changes: 15 additions & 15 deletions app/http/controllers/cron_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ func (c *CronController) Add(ctx http.Context) http.Response {
"backup_type": "required_if:type,backup|in:website,mysql,postgresql",
})
if err != nil {
return Error(ctx, http.StatusBadRequest, err.Error())
return Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return Error(ctx, http.StatusBadRequest, validator.Errors().One())
return Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

// 单独验证时间格式
if !regexp.MustCompile(`^((\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+)(,(\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+))*\s?){5}$`).MatchString(ctx.Request().Input("time")) {
return Error(ctx, http.StatusBadRequest, "时间格式错误")
return Error(ctx, http.StatusUnprocessableEntity, "时间格式错误")
}

shell := ctx.Request().Input("script")
Expand Down Expand Up @@ -151,7 +151,7 @@ func (c *CronController) Script(ctx http.Context) http.Response {
var cron models.Cron
err := facades.Orm().Query().Where("id", ctx.Request().Input("id")).FirstOrFail(&cron)
if err != nil {
return Error(ctx, http.StatusBadRequest, "计划任务不存在")
return Error(ctx, http.StatusUnprocessableEntity, "计划任务不存在")
}

return Success(ctx, tools.Read(cron.Shell))
Expand All @@ -165,25 +165,25 @@ func (c *CronController) Update(ctx http.Context) http.Response {
"script": "required",
})
if err != nil {
return Error(ctx, http.StatusBadRequest, err.Error())
return Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return Error(ctx, http.StatusBadRequest, validator.Errors().One())
return Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

// 单独验证时间格式
if !regexp.MustCompile(`^((\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+)(,(\*|\d+|\d+-\d+|\d+/\d+|\d+-\d+/\d+|\*/\d+))*\s?){5}$`).MatchString(ctx.Request().Input("time")) {
return Error(ctx, http.StatusBadRequest, "时间格式错误")
return Error(ctx, http.StatusUnprocessableEntity, "时间格式错误")
}

var cron models.Cron
err = facades.Orm().Query().Where("id", ctx.Request().Input("id")).FirstOrFail(&cron)
if err != nil {
return Error(ctx, http.StatusBadRequest, "计划任务不存在")
return Error(ctx, http.StatusUnprocessableEntity, "计划任务不存在")
}

if !cron.Status {
return Error(ctx, http.StatusBadRequest, "计划任务已禁用")
return Error(ctx, http.StatusUnprocessableEntity, "计划任务已禁用")
}

cron.Time = ctx.Request().Input("time")
Expand Down Expand Up @@ -213,7 +213,7 @@ func (c *CronController) Delete(ctx http.Context) http.Response {
var cron models.Cron
err := facades.Orm().Query().Where("id", ctx.Request().Input("id")).FirstOrFail(&cron)
if err != nil {
return Error(ctx, http.StatusBadRequest, "计划任务不存在")
return Error(ctx, http.StatusUnprocessableEntity, "计划任务不存在")
}

c.cron.DeleteFromSystem(cron)
Expand All @@ -234,16 +234,16 @@ func (c *CronController) Status(ctx http.Context) http.Response {
"status": "bool",
})
if err != nil {
return Error(ctx, http.StatusBadRequest, err.Error())
return Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return Error(ctx, http.StatusBadRequest, validator.Errors().One())
return Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

var cron models.Cron
err = facades.Orm().Query().Where("id", ctx.Request().Input("id")).FirstOrFail(&cron)
if err != nil {
return Error(ctx, http.StatusBadRequest, "计划任务不存在")
return Error(ctx, http.StatusUnprocessableEntity, "计划任务不存在")
}

cron.Status = ctx.Request().InputBool("status")
Expand All @@ -266,11 +266,11 @@ func (c *CronController) Log(ctx http.Context) http.Response {
var cron models.Cron
err := facades.Orm().Query().Where("id", ctx.Request().Input("id")).FirstOrFail(&cron)
if err != nil {
return Error(ctx, http.StatusBadRequest, "计划任务不存在")
return Error(ctx, http.StatusUnprocessableEntity, "计划任务不存在")
}

if !tools.Exists(cron.Log) {
return Error(ctx, http.StatusBadRequest, "日志文件不存在")
return Error(ctx, http.StatusUnprocessableEntity, "日志文件不存在")
}

return Success(ctx, tools.Read(cron.Log))
Expand Down
8 changes: 4 additions & 4 deletions app/http/controllers/plugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (r *PluginController) Install(ctx http.Context) http.Response {
}

if installedPlugin.ID != 0 {
return Error(ctx, http.StatusBadRequest, "插件已安装")
return Error(ctx, http.StatusUnprocessableEntity, "插件已安装")
}

var lock sync.RWMutex
Expand Down Expand Up @@ -161,7 +161,7 @@ func (r *PluginController) Uninstall(ctx http.Context) http.Response {
}

if installedPlugin.ID == 0 {
return Error(ctx, http.StatusBadRequest, "插件未安装")
return Error(ctx, http.StatusUnprocessableEntity, "插件未安装")
}

var lock sync.RWMutex
Expand Down Expand Up @@ -217,7 +217,7 @@ func (r *PluginController) Update(ctx http.Context) http.Response {
}

if installedPlugin.ID == 0 {
return Error(ctx, http.StatusBadRequest, "插件未安装")
return Error(ctx, http.StatusUnprocessableEntity, "插件未安装")
}

var lock sync.RWMutex
Expand Down Expand Up @@ -272,7 +272,7 @@ func (r *PluginController) UpdateShow(ctx http.Context) http.Response {
return Error(ctx, http.StatusInternalServerError, "系统内部错误")
}
if plugin.ID == 0 {
return Error(ctx, http.StatusBadRequest, "插件未安装")
return Error(ctx, http.StatusUnprocessableEntity, "插件未安装")
}

plugin.Show = show
Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/fail2ban/fail2ban_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ func (c *Fail2banController) List(ctx http.Context) http.Response {
limit := ctx.Request().QueryInt("limit", 10)
raw := tools.Read("/etc/fail2ban/jail.local")
if len(raw) == 0 {
return controllers.Error(ctx, http.StatusBadRequest, "Fail2ban 规则为空")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "Fail2ban 规则为空")
}

jailList := regexp.MustCompile(`\[(.*?)]`).FindAllStringSubmatch(raw, -1)
if len(jailList) == 0 {
return controllers.Error(ctx, http.StatusBadRequest, "Fail2ban 规则为空")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "Fail2ban 规则为空")
}

var jails []Jail
Expand Down
74 changes: 46 additions & 28 deletions app/http/controllers/plugins/mysql57/mysql57_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (c *Mysql57Controller) SaveConfig(ctx http.Context) http.Response {

config := ctx.Request().Input("config")
if len(config) == 0 {
return controllers.Error(ctx, http.StatusBadRequest, "配置不能为空")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "配置不能为空")
}

if !tools.Write("/www/server/mysql/conf/my.cnf", config, 0644) {
Expand All @@ -171,7 +171,7 @@ func (c *Mysql57Controller) Load(ctx http.Context) http.Response {

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
if len(rootPassword) == 0 {
return controllers.Error(ctx, http.StatusBadRequest, "MySQL root密码为空")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "MySQL root密码为空")
}

status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'")
Expand All @@ -181,7 +181,7 @@ func (c *Mysql57Controller) Load(ctx http.Context) http.Response {

raw := tools.Exec("/www/server/mysql/bin/mysqladmin -uroot -p" + rootPassword + " extended-status 2>&1")
if strings.Contains(raw, "Access denied for user") {
return controllers.Error(ctx, http.StatusBadRequest, "MySQL root密码错误")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "MySQL root密码错误")
}
if !strings.Contains(raw, "Uptime") {
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL负载失败")
Expand Down Expand Up @@ -290,7 +290,7 @@ func (c *Mysql57Controller) GetRootPassword(ctx http.Context) http.Response {

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
if len(rootPassword) == 0 {
return controllers.Error(ctx, http.StatusBadRequest, "MySQL root密码为空")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "MySQL root密码为空")
}

return controllers.Success(ctx, rootPassword)
Expand All @@ -313,7 +313,7 @@ func (c *Mysql57Controller) SetRootPassword(ctx http.Context) http.Response {

rootPassword := ctx.Request().Input(models.SettingKeyMysqlRootPassword)
if len(rootPassword) == 0 {
return controllers.Error(ctx, http.StatusBadRequest, "MySQL root密码不能为空")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "MySQL root密码不能为空")
}

oldRootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
Expand Down Expand Up @@ -407,10 +407,10 @@ func (c *Mysql57Controller) AddDatabase(ctx http.Context) http.Response {
"password": "required|min_len:8|max_len:255",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
Expand All @@ -437,10 +437,10 @@ func (c *Mysql57Controller) DeleteDatabase(ctx http.Context) http.Response {
"database": "required|min_len:1|max_len:255|regex:^[a-zA-Z][a-zA-Z0-9_]+$|not_in:information_schema,mysql,performance_schema,sys",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
Expand All @@ -463,7 +463,25 @@ func (c *Mysql57Controller) BackupList(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "获取备份列表失败")
}

return controllers.Success(ctx, backupList)
page := ctx.Request().QueryInt("page", 1)
limit := ctx.Request().QueryInt("limit", 10)
startIndex := (page - 1) * limit
endIndex := page * limit
if startIndex > len(backupList) {
return controllers.Success(ctx, http.Json{
"total": 0,
"items": []services.BackupFile{},
})
}
if endIndex > len(backupList) {
endIndex = len(backupList)
}
pagedBackupList := backupList[startIndex:endIndex]

return controllers.Success(ctx, http.Json{
"total": len(backupList),
"items": pagedBackupList,
})
}

// UploadBackup 上传备份
Expand All @@ -475,7 +493,7 @@ func (c *Mysql57Controller) UploadBackup(ctx http.Context) http.Response {

file, err := ctx.Request().File("file")
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, "上传文件失败")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "上传文件失败")
}

backupPath := c.setting.Get(models.SettingKeyBackupPath) + "/mysql"
Expand All @@ -486,7 +504,7 @@ func (c *Mysql57Controller) UploadBackup(ctx http.Context) http.Response {
name := file.GetClientOriginalName()
_, err = file.StoreAs(backupPath, name)
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, "上传文件失败")
return controllers.Error(ctx, http.StatusUnprocessableEntity, "上传文件失败")
}

return controllers.Success(ctx, "上传文件成功")
Expand All @@ -503,10 +521,10 @@ func (c *Mysql57Controller) CreateBackup(ctx http.Context) http.Response {
"database": "required|min_len:1|max_len:255|regex:^[a-zA-Z][a-zA-Z0-9_]+$|not_in:information_schema,mysql,performance_schema,sys",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

database := ctx.Request().Input("database")
Expand All @@ -527,17 +545,17 @@ func (c *Mysql57Controller) DeleteBackup(ctx http.Context) http.Response {
}

validator, err := ctx.Request().Validate(map[string]string{
"backup": "required|min_len:1|max_len:255",
"name": "required|min_len:1|max_len:255",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

backupPath := c.setting.Get(models.SettingKeyBackupPath) + "/mysql"
fileName := ctx.Request().Input("backup")
fileName := ctx.Request().Input("name")
tools.Remove(backupPath + "/" + fileName)

return controllers.Success(ctx, "删除备份成功")
Expand All @@ -555,10 +573,10 @@ func (c *Mysql57Controller) RestoreBackup(ctx http.Context) http.Response {
"database": "required|min_len:1|max_len:255|regex:^[a-zA-Z][a-zA-Z0-9_]+$|not_in:information_schema,mysql,performance_schema,sys",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

err = c.backup.MysqlRestore(ctx.Request().Input("database"), ctx.Request().Input("backup"))
Expand Down Expand Up @@ -669,10 +687,10 @@ func (c *Mysql57Controller) AddUser(ctx http.Context) http.Response {
"password": "required|min_len:8|max_len:255",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
Expand All @@ -697,10 +715,10 @@ func (c *Mysql57Controller) DeleteUser(ctx http.Context) http.Response {
"user": "required|min_len:1|max_len:255|regex:^[a-zA-Z][a-zA-Z0-9_]+$",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
Expand All @@ -722,10 +740,10 @@ func (c *Mysql57Controller) SetUserPassword(ctx http.Context) http.Response {
"password": "required|min_len:8|max_len:255",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
Expand All @@ -749,10 +767,10 @@ func (c *Mysql57Controller) SetUserPrivileges(ctx http.Context) http.Response {
"database": "required|min_len:1|max_len:255",
})
if err != nil {
return controllers.Error(ctx, http.StatusBadRequest, err.Error())
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return controllers.Error(ctx, http.StatusBadRequest, validator.Errors().One())
return controllers.Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}

rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword)
Expand Down
Loading

0 comments on commit fe4a6e2

Please sign in to comment.