goploy/controller/MonitorController.go
2021-05-15 21:28:20 +08:00

170 lines
5.0 KiB
Go

package controller
import (
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
"net"
"strconv"
"time"
)
// Monitor struct
type Monitor Controller
// GetList monitor list
func (Monitor) GetList(gp *core.Goploy) *core.Response {
pagination, err := model.PaginationFrom(gp.URLQuery)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
monitorList, err := model.Monitor{NamespaceID: gp.Namespace.ID}.GetList(pagination)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{
Data: struct {
Monitors model.Monitors `json:"list"`
}{Monitors: monitorList},
}
}
// GetTotal monitor total
func (Monitor) GetTotal(gp *core.Goploy) *core.Response {
total, err := model.Monitor{NamespaceID: gp.Namespace.ID}.GetTotal()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{
Data: struct {
Total int64 `json:"total"`
}{Total: total},
}
}
// Check one monitor
func (Monitor) Check(gp *core.Goploy) *core.Response {
type ReqData struct {
Domain string `json:"domain" validate:"required"`
Port int `json:"port" validate:"min=0,max=65535"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
_, err := net.DialTimeout("tcp", reqData.Domain+":"+strconv.Itoa(reqData.Port), 5*time.Second)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{Message: "Connected"}
}
// Add one monitor
func (Monitor) Add(gp *core.Goploy) *core.Response {
type ReqData struct {
Name string `json:"name" validate:"required"`
Domain string `json:"domain" validate:"required"`
Port int `json:"port" validate:"min=0,max=65535"`
Second int `json:"second" validate:"gt=0"`
Times uint16 `json:"times" validate:"gt=0"`
NotifyType uint8 `json:"notifyType" validate:"gt=0"`
NotifyTarget string `json:"notifyTarget" validate:"required"`
NotifyTimes uint16 `json:"notifyTimes" validate:"gt=0"`
Description string `json:"description" validate:"max=255"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
id, err := model.Monitor{
NamespaceID: gp.Namespace.ID,
Name: reqData.Name,
Domain: reqData.Domain,
Port: reqData.Port,
Second: reqData.Second,
Times: reqData.Times,
NotifyType: reqData.NotifyType,
NotifyTarget: reqData.NotifyTarget,
NotifyTimes: reqData.NotifyTimes,
Description: reqData.Description,
}.AddRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{
Data: struct {
ID int64 `json:"id"`
}{ID: id},
}
}
// Edit one monitor
func (Monitor) Edit(gp *core.Goploy) *core.Response {
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
Name string `json:"name" validate:"required"`
Domain string `json:"domain" validate:"required"`
Port int `json:"port" validate:"min=0,max=65535"`
Second int `json:"second" validate:"gt=0"`
Times uint16 `json:"times" validate:"gt=0"`
NotifyType uint8 `json:"notifyType" validate:"gt=0"`
NotifyTarget string `json:"notifyTarget" validate:"required"`
NotifyTimes uint16 `json:"notifyTimes" validate:"gt=0"`
Description string `json:"description" validate:"max=255"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err := model.Monitor{
ID: reqData.ID,
Name: reqData.Name,
Domain: reqData.Domain,
Port: reqData.Port,
Second: reqData.Second,
Times: reqData.Times,
NotifyType: reqData.NotifyType,
NotifyTarget: reqData.NotifyTarget,
NotifyTimes: reqData.NotifyTimes,
Description: reqData.Description,
}.EditRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
// Toggle Monitor state
func (Monitor) Toggle(gp *core.Goploy) *core.Response {
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
if err := (model.Monitor{ID: reqData.ID}).ToggleState(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
// Remove one Monitor
func (Monitor) Remove(gp *core.Goploy) *core.Response {
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
if err := (model.Monitor{ID: reqData.ID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}