gocron/routers/host/host.go

53 lines
1.4 KiB
Go
Raw Normal View History

2017-04-07 01:13:36 +00:00
package host
2017-04-07 09:26:46 +00:00
import (
"gopkg.in/macaron.v1"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/utils"
"github.com/ouqiang/gocron/modules/logger"
)
2017-04-07 01:13:36 +00:00
func Index(ctx *macaron.Context) {
hostModel := new(models.Host)
hosts, err := hostModel.List()
if err != nil {
logger.Error(err)
}
ctx.Data["Title"] = "主机列表"
2017-04-09 09:55:55 +00:00
ctx.Data["URI"] = "/host"
ctx.Data["Hosts"] = hosts
ctx.HTML(200, "host/index")
}
2017-04-07 01:13:36 +00:00
func Create(ctx *macaron.Context) {
ctx.Data["Title"] = "添加主机"
2017-04-09 09:55:55 +00:00
ctx.Data["URI"] = "/host/create"
2017-04-07 01:13:36 +00:00
ctx.HTML(200, "host/create")
}
2017-04-07 09:26:46 +00:00
type HostForm struct {
Name string `binding:"Required"`
Alias string `binding:"Required"`
Username string `binding:"Required"`
Password string
Port int `binding:"Required;Range(1-65535)"`
Remark string `binding:"Required"`
}
func Store(ctx *macaron.Context, form HostForm) string {
json := utils.Json{}
hostModel := new(models.Host)
hostModel.Name = form.Name
hostModel.Alias = form.Alias
hostModel.Username = form.Username
hostModel.Password = form.Password
hostModel.Port = form.Port
hostModel.Remark = form.Remark
_, err := hostModel.Create()
if err != nil {
logger.Error(err)
return json.Failure(utils.ResponseFailure, "保存失败")
}
2017-04-07 01:13:36 +00:00
2017-04-07 09:26:46 +00:00
return json.Success("保存成功", nil)
2017-04-07 01:13:36 +00:00
}