2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-07 00:57:30 +08:00

feat: 优化验证器使用

This commit is contained in:
耗子
2024-06-18 03:41:34 +08:00
parent 46b431de30
commit 0a3d3828c8
6 changed files with 66 additions and 136 deletions

View File

@@ -50,33 +50,29 @@ func (r *SshController) GetInfo(ctx http.Context) http.Response {
// UpdateInfo 更新 SSH 配置
func (r *SshController) UpdateInfo(ctx http.Context) http.Response {
validator, err := ctx.Request().Validate(map[string]string{
if sanitize := Sanitize(ctx, map[string]string{
"host": "required",
"port": "required",
"user": "required",
"password": "required",
})
if err != nil {
return Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if validator.Fails() {
return Error(ctx, http.StatusUnprocessableEntity, validator.Errors().One())
}); sanitize != nil {
return sanitize
}
host := ctx.Request().Input("host")
port := ctx.Request().Input("port")
user := ctx.Request().Input("user")
password := ctx.Request().Input("password")
if err = r.setting.Set(models.SettingKeySshHost, host); err != nil {
if err := r.setting.Set(models.SettingKeySshHost, host); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if err = r.setting.Set(models.SettingKeySshPort, port); err != nil {
if err := r.setting.Set(models.SettingKeySshPort, port); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if err = r.setting.Set(models.SettingKeySshUser, user); err != nil {
if err := r.setting.Set(models.SettingKeySshUser, user); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if err = r.setting.Set(models.SettingKeySshPassword, password); err != nil {
if err := r.setting.Set(models.SettingKeySshPassword, password); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}