138 lines
3.3 KiB
Go
138 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"next-terminal/server/common/maps"
|
|
"next-terminal/server/model"
|
|
"next-terminal/server/repository"
|
|
"next-terminal/server/utils"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type AgentGatewayApi struct{}
|
|
|
|
func (api AgentGatewayApi) AllEndpoint(c echo.Context) error {
|
|
items, err := repository.AgentGatewayRepository.FindAll(context.TODO())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, items)
|
|
}
|
|
|
|
func (api AgentGatewayApi) PagingEndpoint(c echo.Context) error {
|
|
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
|
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
|
name := c.QueryParam("name")
|
|
|
|
items, total, err := repository.AgentGatewayRepository.Find(context.TODO(), pageIndex, pageSize, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, maps.Map{
|
|
"total": total,
|
|
"items": items,
|
|
})
|
|
}
|
|
|
|
func (api AgentGatewayApi) CreateEndpoint(c echo.Context) error {
|
|
var item model.AgentGateway
|
|
if err := c.Bind(&item); err != nil {
|
|
return err
|
|
}
|
|
item.ID = utils.UUID()
|
|
item.Online = false
|
|
|
|
if err := repository.AgentGatewayRepository.Create(context.TODO(), &item); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, "")
|
|
}
|
|
|
|
func (api AgentGatewayApi) UpdateEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
var item model.AgentGateway
|
|
if err := c.Bind(&item); err != nil {
|
|
return err
|
|
}
|
|
if err := repository.AgentGatewayRepository.UpdateById(context.TODO(), &item, id); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api AgentGatewayApi) DeleteEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
if err := repository.AgentGatewayRepository.DeleteById(context.TODO(), id); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api AgentGatewayApi) GetEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
item, err := repository.AgentGatewayRepository.FindById(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, item)
|
|
}
|
|
|
|
func (api AgentGatewayApi) GetRegisterParamEndpoint(c echo.Context) error {
|
|
return Success(c, maps.Map{
|
|
"endpoint": "",
|
|
"token": "",
|
|
})
|
|
}
|
|
|
|
func (api AgentGatewayApi) SetRegisterAddrEndpoint(c echo.Context) error {
|
|
endpoint := c.QueryParam("endpoint")
|
|
_ = endpoint
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api AgentGatewayApi) GetStatEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
_, err := repository.AgentGatewayRepository.FindById(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, maps.Map{
|
|
"ping": 0,
|
|
"cpu": maps.Map{"percent": 0},
|
|
"memory": maps.Map{"percent": 0},
|
|
"disk": maps.Map{"percent": 0},
|
|
"network": maps.Map{},
|
|
"load": maps.Map{},
|
|
"host": maps.Map{},
|
|
"process": maps.Map{"total": 0},
|
|
"connections": 0,
|
|
"tcp_states": []string{},
|
|
"errors": maps.Map{},
|
|
})
|
|
}
|
|
|
|
func (api AgentGatewayApi) UpdateSortEndpoint(c echo.Context) error {
|
|
var items []struct {
|
|
ID string `json:"id"`
|
|
SortOrder int `json:"sortOrder"`
|
|
}
|
|
if err := c.Bind(&items); err != nil {
|
|
return err
|
|
}
|
|
ctx := context.TODO()
|
|
for _, item := range items {
|
|
sortStr := strconv.Itoa(item.SortOrder * 100)
|
|
repository.AgentGatewayRepository.UpdateSort(ctx, item.ID, sortStr)
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api AgentGatewayApi) VersionEndpoint(c echo.Context) error {
|
|
return Success(c, maps.Map{
|
|
"version": "1.0.0",
|
|
})
|
|
}
|