gocron/routers/host/host.go

171 lines
4.8 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-13 09:35:59 +00:00
"strconv"
2017-04-20 01:36:42 +00:00
"github.com/ouqiang/gocron/service"
2017-04-23 06:11:53 +00:00
"github.com/Unknwon/paginater"
"fmt"
"html/template"
2017-04-30 22:02:49 +00:00
"github.com/ouqiang/gocron/routers/base"
2017-05-05 08:31:24 +00:00
"github.com/go-macaron/binding"
2017-05-28 15:13:22 +00:00
"github.com/ouqiang/gocron/modules/rpc/grpcpool"
"strings"
2017-04-07 09:26:46 +00:00
)
2017-04-07 01:13:36 +00:00
func Index(ctx *macaron.Context) {
hostModel := new(models.Host)
queryParams := parseQueryParams(ctx)
2017-04-23 06:11:53 +00:00
total, err := hostModel.Total(queryParams)
hosts, err := hostModel.List(queryParams)
if err != nil {
logger.Error(err)
}
2017-04-23 06:11:53 +00:00
name, ok := queryParams["name"].(string)
var safeNameHTML = ""
if ok {
safeNameHTML = template.HTMLEscapeString(name)
}
PageParams := fmt.Sprintf("id=%d&name=%s&page_size=%d",
queryParams["Id"], safeNameHTML, queryParams["PageSize"]);
queryParams["PageParams"] = template.URL(PageParams)
p := paginater.New(int(total), queryParams["PageSize"].(int), queryParams["Page"].(int), 5)
ctx.Data["Pagination"] = p
ctx.Data["Title"] = "主机列表"
ctx.Data["Hosts"] = hosts
ctx.Data["Params"] = queryParams
ctx.HTML(200, "host/index")
}
2017-04-07 01:13:36 +00:00
func Create(ctx *macaron.Context) {
ctx.Data["Title"] = "添加主机"
2017-04-16 08:24:09 +00:00
ctx.HTML(200, "host/host_form")
}
func Edit(ctx *macaron.Context) {
ctx.Data["Title"] = "编辑主机"
hostModel := new(models.Host)
id := ctx.ParamsInt(":id")
err := hostModel.Find(id)
if err != nil {
logger.Errorf("获取主机详情失败#主机id-%d", id)
}
ctx.Data["Host"] = hostModel
ctx.HTML(200, "host/host_form")
2017-04-07 01:13:36 +00:00
}
2017-04-07 09:26:46 +00:00
type HostForm struct {
2017-04-20 01:36:42 +00:00
Id int16
2017-05-05 08:31:24 +00:00
Name string `binding:"Required;MaxSize(64)"`
2017-04-13 09:35:59 +00:00
Alias string `binding:"Required;MaxSize(32)"`
2017-04-07 09:26:46 +00:00
Port int `binding:"Required;Range(1-65535)"`
2017-04-13 09:35:59 +00:00
Remark string
2017-04-07 09:26:46 +00:00
}
2017-05-05 08:31:24 +00:00
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))
}
2017-04-07 09:26:46 +00:00
func Store(ctx *macaron.Context, form HostForm) string {
2017-04-13 09:35:59 +00:00
json := utils.JsonResponse{}
2017-04-07 09:26:46 +00:00
hostModel := new(models.Host)
2017-04-20 01:36:42 +00:00
id := form.Id
nameExist, err := hostModel.NameExists(form.Name, form.Id)
2017-04-13 09:35:59 +00:00
if err != nil {
return json.CommonFailure("操作失败", err)
}
if nameExist {
return json.CommonFailure("主机名已存在")
}
2017-05-28 15:13:22 +00:00
hostModel.Name = strings.TrimSpace(form.Name)
hostModel.Alias = strings.TrimSpace(form.Alias)
2017-04-07 09:26:46 +00:00
hostModel.Port = form.Port
2017-05-28 15:13:22 +00:00
hostModel.Remark = strings.TrimSpace(form.Remark)
2017-04-20 01:36:42 +00:00
isCreate := false
2017-05-28 15:13:22 +00:00
oldHostModel := new(models.Host)
err = oldHostModel.Find(int(id))
if err != nil {
return json.CommonFailure("主机不存在")
}
2017-04-20 01:36:42 +00:00
if id > 0 {
_, err = hostModel.UpdateBean(id)
2017-04-20 01:36:42 +00:00
} else {
isCreate = true
id, err = hostModel.Create()
}
2017-04-07 09:26:46 +00:00
if err != nil {
2017-04-13 09:35:59 +00:00
return json.CommonFailure("保存失败", err)
2017-04-07 09:26:46 +00:00
}
2017-04-07 01:13:36 +00:00
2017-05-28 15:13:22 +00:00
if !isCreate {
oldAddr := fmt.Sprintf("%s:%d", oldHostModel.Name, oldHostModel.Port)
newAddr := fmt.Sprintf("%s:%d", hostModel.Name, hostModel.Port)
if oldAddr != newAddr {
grpcpool.Pool.Release(oldAddr)
}
taskModel := new(models.TaskHost)
tasks, err := taskModel.ActiveListByHostId(id)
if err != nil {
return json.CommonFailure("刷新任务主机信息失败", err)
}
2017-04-20 01:36:42 +00:00
serviceTask := new(service.Task)
serviceTask.BatchAdd(tasks)
}
2017-04-07 09:26:46 +00:00
return json.Success("保存成功", nil)
2017-04-13 09:35:59 +00:00
}
func Remove(ctx *macaron.Context) string {
id, err := strconv.Atoi(ctx.Params(":id"))
json := utils.JsonResponse{}
if err != nil {
return json.CommonFailure("参数错误", err)
}
taskModel := new(models.Task)
exist,err := taskModel.HostIdExist(int16(id))
if err != nil {
return json.CommonFailure("操作失败", err)
}
if exist {
return json.CommonFailure("有任务引用此主机,不能删除")
}
hostModel := new(models.Host)
2017-05-28 15:13:22 +00:00
err = hostModel.Find(int(id))
if err != nil {
return json.CommonFailure("主机不存在")
}
2017-04-13 09:35:59 +00:00
_, err =hostModel.Delete(id)
if err != nil {
return json.CommonFailure("操作失败", err)
}
2017-05-28 15:13:22 +00:00
addr := fmt.Sprintf("%s:%d", hostModel.Name, hostModel.Port)
grpcpool.Pool.Release(addr)
2017-04-13 09:35:59 +00:00
return json.Success("操作成功", nil)
}
// 解析查询参数
func parseQueryParams(ctx *macaron.Context) (models.CommonMap) {
var params models.CommonMap = models.CommonMap{}
params["Id"] = ctx.QueryInt("id")
params["Name"] = ctx.QueryTrim("name")
2017-04-30 22:02:49 +00:00
base.ParsePageAndPageSize(ctx, params)
return params
2017-04-07 01:13:36 +00:00
}