goploy/controller/ServerController.go

178 lines
5.1 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package controller
import (
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/utils"
2021-02-14 14:22:22 +08:00
"io/ioutil"
2020-08-04 14:28:25 +08:00
)
// Server struct
type Server Controller
2020-08-15 13:38:06 +08:00
// GetList -
2020-09-26 10:45:28 +08:00
func (Server) GetList(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
pagination, err := model.PaginationFrom(gp.URLQuery)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
serverList, err := model.Server{NamespaceID: gp.Namespace.ID}.GetList(pagination)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
Servers model.Servers `json:"list"`
}{Servers: serverList},
}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// GetTotal -
2020-09-26 10:45:28 +08:00
func (Server) GetTotal(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
total, err := model.Server{NamespaceID: gp.Namespace.ID}.GetTotal()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
Total int64 `json:"total"`
}{Total: total},
}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// GetOption -
2020-09-26 10:45:28 +08:00
func (Server) GetOption(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
serverList, err := model.Server{NamespaceID: gp.Namespace.ID}.GetAll()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
Servers model.Servers `json:"list"`
}{Servers: serverList},
}
2020-08-04 14:28:25 +08:00
}
2021-02-14 14:22:22 +08:00
// GetPublicKey -
func (Server) GetPublicKey(gp *core.Goploy) *core.Response {
path := gp.URLQuery.Get("path")
contentByte, err := ioutil.ReadFile(path + ".pub")
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{
Data: string(contentByte),
}
}
2020-08-15 13:38:06 +08:00
// Check server
2020-09-26 10:45:28 +08:00
func (Server) Check(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
2021-02-07 15:43:02 +08:00
IP string `json:"ip" validate:"required,ip|hostname"`
Port int `json:"port" validate:"min=0,max=65535"`
Owner string `json:"owner" validate:"required,max=255"`
Path string `json:"path" validate:"required,max=255"`
Password string `json:"password" validate:"max=255"`
2020-08-04 14:28:25 +08:00
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2021-02-12 16:21:05 +08:00
if _, err := utils.DialSSH(reqData.Owner, reqData.Password, reqData.Path, reqData.IP, reqData.Port); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{Message: "Connected"}
}
2020-08-15 13:38:06 +08:00
// Add server
2020-09-26 10:45:28 +08:00
func (Server) Add(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
Name string `json:"name" validate:"required"`
2021-02-26 16:45:13 +08:00
NamespaceID int64 `json:"namespaceId" validate:"gte=0"`
IP string `json:"ip" validate:"ip|hostname"`
2020-08-04 14:28:25 +08:00
Port int `json:"port" validate:"min=0,max=65535"`
2021-02-07 15:43:02 +08:00
Owner string `json:"owner" validate:"required,max=255"`
Path string `json:"path" validate:"required,max=255"`
Password string `json:"password"`
2020-08-04 14:28:25 +08:00
Description string `json:"description" validate:"max=255"`
}
2020-09-26 11:45:44 +08:00
2020-08-04 14:28:25 +08:00
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
id, err := model.Server{
2021-02-26 16:45:13 +08:00
NamespaceID: reqData.NamespaceID,
2020-08-04 14:28:25 +08:00
Name: reqData.Name,
IP: reqData.IP,
Port: reqData.Port,
Owner: reqData.Owner,
2021-02-07 15:43:02 +08:00
Path: reqData.Path,
Password: reqData.Password,
2020-08-04 14:28:25 +08:00
Description: reqData.Description,
}.AddRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
ID int64 `json:"id"`
}{ID: id},
}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// Edit server
2020-09-26 10:45:28 +08:00
func (Server) Edit(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
2021-02-26 16:45:13 +08:00
NamespaceID int64 `json:"namespaceId" validate:"gte=0"`
2020-08-04 14:28:25 +08:00
Name string `json:"name" validate:"required"`
2021-02-04 11:15:43 +08:00
IP string `json:"ip" validate:"required,ip|hostname"`
2020-08-04 14:28:25 +08:00
Port int `json:"port" validate:"min=0,max=65535"`
2021-02-07 15:43:02 +08:00
Owner string `json:"owner" validate:"required,max=255"`
Path string `json:"path" validate:"required,max=255"`
Password string `json:"password" validate:"max=255"`
2020-08-04 14:28:25 +08:00
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.Server{
ID: reqData.ID,
2021-02-26 16:45:13 +08:00
NamespaceID: reqData.NamespaceID,
2020-08-04 14:28:25 +08:00
Name: reqData.Name,
IP: reqData.IP,
Port: reqData.Port,
Owner: reqData.Owner,
2021-02-07 15:43:02 +08:00
Path: reqData.Path,
Password: reqData.Password,
2020-08-04 14:28:25 +08:00
Description: reqData.Description,
}.EditRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
2020-08-15 13:38:06 +08:00
// RemoveRow server
2020-09-26 10:45:28 +08:00
func (Server) Remove(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
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()}
}
2020-08-15 13:38:06 +08:00
if err := (model.Server{ID: reqData.ID}).RemoveRow(); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}