mirror of https://github.com/ouqiang/gocron
完善表单验证
parent
dfa96bc9f2
commit
29e4daaf93
|
@ -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)
|
||||
|
|
|
@ -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("/")
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
<h5 class="ui header">{{{.Task.Name}}} {{{if eq .Status 1}}}<i class="large checkmark blue icon"></i> {{{else}}} <i class="large red minus icon"></i> {{{end}}}
|
||||
</h5>
|
||||
<p>任务ID: <span class="stress">{{{.Id}}}</span></p>
|
||||
<p>状态: <span class="stress">{{{if eq .Status 1}}}激活{{{else}}}停止{{{end}}}</span></p>
|
||||
<p>cron表达式: {{{.Spec}}}</p>
|
||||
<p>执行方式: {{{if eq .Protocol 1}}} HTTP {{{else if eq .Protocol 2}}} SSH {{{else if eq .Protocol 3}}}本地命令{{{end}}}</p>
|
||||
<p class="sensorStatus">命令:{{{.Command}}}</p>
|
||||
|
@ -74,7 +75,7 @@
|
|||
<div class="ui buttons operation">
|
||||
<a class="ui purple button" href="/task/edit/{{{.Id}}}">编辑</a>
|
||||
{{{if eq .Status 1}}}
|
||||
<button class="ui primary button" @click="changeStatus({{{.Id}}},{{{.Status}}})">暂停</button>
|
||||
<button class="ui primary button" @click="changeStatus({{{.Id}}},{{{.Status}}})">停止</button>
|
||||
{{{else}}}
|
||||
<button class="ui blue button" @click="changeStatus({{{.Id}}},{{{.Status}}})">激活 </button>
|
||||
{{{end}}}
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
</div>
|
||||
<div class="field">
|
||||
<select name="protocol" id="protocol">
|
||||
<option value="0">协议</option>
|
||||
<option value="0">执行方式</option>
|
||||
<option value="3" {{{if eq .Params.Protocol 3}}}selected{{{end}}}>系统命令</option>
|
||||
<option value="2" {{{if eq .Params.Protocol 2}}}selected{{{end}}} data-match="host_id" data-validate-type="selectProtocol">SSH</option>
|
||||
<option value="1" {{{if eq .Params.Protocol 1}}}selected{{{end}}}>HTTP</option>
|
||||
|
|
|
@ -97,7 +97,7 @@
|
|||
任务添加成功后,是否立即调度
|
||||
</div>
|
||||
<select name="status">
|
||||
<option value="2"{{{if .Task}}} {{{if eq .Task.Status 0}}}selected{{{end}}} {{{end}}}>暂停</option>
|
||||
<option value="2"{{{if .Task}}} {{{if eq .Task.Status 0}}}selected{{{end}}} {{{end}}}>停止</option>
|
||||
<option value="1" {{{if .Task}}} {{{if eq .Task.Status 1}}}selected{{{end}}} {{{end}}}>激活</option>
|
||||
</select>
|
||||
</div>
|
||||
|
@ -295,6 +295,10 @@
|
|||
{
|
||||
type : 'empty',
|
||||
prompt : '请输入任务名称'
|
||||
},
|
||||
{
|
||||
type : 'maxLength[32]',
|
||||
prompt : '长度不能超过32'
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -304,6 +308,10 @@
|
|||
{
|
||||
type : 'empty',
|
||||
prompt : '请输入crontab格式表达式'
|
||||
},
|
||||
{
|
||||
type : 'maxLength[64]',
|
||||
prompt : '长度不能超过64'
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -313,6 +321,10 @@
|
|||
{
|
||||
type : 'empty',
|
||||
prompt : '请输入任务命令'
|
||||
},
|
||||
{
|
||||
type : 'maxLength[256]',
|
||||
prompt : '长度不能超过256'
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -324,6 +336,33 @@
|
|||
prompt : '请选择主机'
|
||||
}
|
||||
]
|
||||
},
|
||||
timeout: {
|
||||
identifier : 'timeout',
|
||||
rules: [
|
||||
{
|
||||
type : 'integer[-1..86400]',
|
||||
prompt : '取值范围-1 - 86400'
|
||||
}
|
||||
]
|
||||
},
|
||||
remark: {
|
||||
identifier : 'remark',
|
||||
rules: [
|
||||
{
|
||||
type : 'maxLength[100]',
|
||||
prompt : '长度不能超过100'
|
||||
}
|
||||
]
|
||||
},
|
||||
retryTimes: {
|
||||
identifier : 'retry_times',
|
||||
rules: [
|
||||
{
|
||||
type : 'integer[0..10]',
|
||||
prompt : '取值范围 0 - 10'
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
inline : true
|
||||
|
|
Loading…
Reference in New Issue