diff --git a/routers/host/host.go b/routers/host/host.go index f173ad3..56c1244 100644 --- a/routers/host/host.go +++ b/routers/host/host.go @@ -12,6 +12,7 @@ import ( "fmt" "html/template" "github.com/ouqiang/gocron/routers/base" + "github.com/go-macaron/binding" ) func Index(ctx *macaron.Context) { @@ -96,7 +97,7 @@ func Ping(ctx *macaron.Context) string { type HostForm struct { Id int16 - Name string `binding:"Required;MaxSize(100)"` + Name string `binding:"Required;MaxSize(64)"` Alias string `binding:"Required;MaxSize(32)"` Username string `binding:"Required;MaxSize(32)"` Port int `binding:"Required;Range(1-65535)"` @@ -104,6 +105,16 @@ type HostForm struct { Remark string } +func (f HostForm) Error(ctx *macaron.Context, errs binding.Errors) { + if len(errs) == 0 { + return + } + json := utils.JsonResponse{} + content := json.CommonFailure("表单验证失败, 请检测输入") + + ctx.Resp.Write([]byte(content)) +} + func Store(ctx *macaron.Context, form HostForm) string { json := utils.JsonResponse{} hostModel := new(models.Host) diff --git a/routers/install/install.go b/routers/install/install.go index 5e182d3..e5cb53b 100644 --- a/routers/install/install.go +++ b/routers/install/install.go @@ -9,6 +9,7 @@ import ( "strconv" "fmt" "github.com/ouqiang/gocron/service" + "github.com/go-macaron/binding" ) // 系统安装 @@ -27,6 +28,16 @@ type InstallForm struct { AdminEmail string `binding:"Required;Email;MaxSize(50)"` } +func (f InstallForm) Error(ctx *macaron.Context, errs binding.Errors) { + if len(errs) == 0 { + return + } + json := utils.JsonResponse{} + content := json.CommonFailure("表单验证失败, 请检测输入") + + ctx.Resp.Write([]byte(content)) +} + func Create(ctx *macaron.Context) { if app.Installed { ctx.Redirect("/") diff --git a/routers/task/task.go b/routers/task/task.go index c31bac1..4d72a7f 100644 --- a/routers/task/task.go +++ b/routers/task/task.go @@ -12,14 +12,15 @@ import ( "fmt" "html/template" "github.com/ouqiang/gocron/routers/base" + "github.com/go-macaron/binding" ) type TaskForm struct { Id int - Name string `binding:"Required;"` + Name string `binding:"Required;MaxSize(32)"` Spec string `binding:"Required;MaxSize(64)"` Protocol models.TaskProtocol `binding:"In(1,2,3)"` - Command string `binding:"Required;MaxSize(512)"` + Command string `binding:"Required;MaxSize(256)"` Timeout int `binding:"Range(-1,86400)"` Multi int8 `binding:"In(1,2)"` RetryTimes int8 @@ -31,6 +32,17 @@ type TaskForm struct { NotifyReceiverId string } + +func (f TaskForm) Error(ctx *macaron.Context, errs binding.Errors) { + if len(errs) == 0 { + return + } + json := utils.JsonResponse{} + content := json.CommonFailure("表单验证失败, 请检测输入") + + ctx.Resp.Write([]byte(content)) +} + // 首页 func Index(ctx *macaron.Context) { taskModel := new(models.Task) @@ -141,6 +153,9 @@ func Store(ctx *macaron.Context, form TaskForm) string { if taskModel.Protocol == models.TaskHTTP && taskModel.Timeout == -1 { return json.CommonFailure("HTTP任务不支持后台运行", err) } + if taskModel.RetryTimes > 10 || taskModel < 0 { + return json.CommonFailure("任务重试次数取值0-10") + } if id == 0 { id, err = taskModel.Create() } else { diff --git a/templates/host/host_form.html b/templates/host/host_form.html index e2c35fc..53d3963 100644 --- a/templates/host/host_form.html +++ b/templates/host/host_form.html @@ -91,6 +91,10 @@ { type : 'empty', prompt : '请输入主机名' + }, + { + type : 'maxLength[64]', + prompt : '长度不能超过64' } ] }, @@ -100,6 +104,10 @@ { type : 'empty', prompt : '请输入主机别名' + }, + { + type : 'maxLength[32]', + prompt : '长度不能超过32' } ] }, @@ -109,6 +117,10 @@ { type : 'empty', prompt : '请输入SSH用户名' + }, + { + type : 'maxLength[32]', + prompt : '长度不能超过32' } ] }, @@ -120,6 +132,15 @@ prompt : '请输入有效的端口号' } ] + }, + remark: { + identifier : 'remark', + rules: [ + { + type : 'maxLength[100]', + prompt : '长度不能超过100' + } + ] } }, inline : true diff --git a/templates/task/index.html b/templates/task/index.html index ccbc437..66c84c9 100644 --- a/templates/task/index.html +++ b/templates/task/index.html @@ -58,6 +58,7 @@
任务ID: {{{.Id}}}
+状态: {{{if eq .Status 1}}}激活{{{else}}}停止{{{end}}}
cron表达式: {{{.Spec}}}
执行方式: {{{if eq .Protocol 1}}} HTTP {{{else if eq .Protocol 2}}} SSH {{{else if eq .Protocol 3}}}本地命令{{{end}}}
命令:{{{.Command}}}
@@ -74,7 +75,7 @@