288 lines
6.9 KiB
Go
288 lines
6.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"next-terminal/server/common"
|
|
"next-terminal/server/common/maps"
|
|
"next-terminal/server/dto"
|
|
"next-terminal/server/model"
|
|
"next-terminal/server/repository"
|
|
"next-terminal/server/utils"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type WebsiteApi struct{}
|
|
|
|
func (api WebsiteApi) AllEndpoint(c echo.Context) error {
|
|
items, err := repository.WebsiteRepository.FindAll(context.TODO())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, items)
|
|
}
|
|
|
|
func (api WebsiteApi) PagingEndpoint(c echo.Context) error {
|
|
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
|
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
|
keyword := c.QueryParam("keyword")
|
|
|
|
items, total, err := repository.WebsiteRepository.Find(context.TODO(), pageIndex, pageSize, keyword)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, maps.Map{
|
|
"total": total,
|
|
"items": items,
|
|
})
|
|
}
|
|
|
|
func serializeJSON(v interface{}) string {
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
switch val := v.(type) {
|
|
case string:
|
|
return val
|
|
case []interface{}, map[string]interface{}:
|
|
bytes, err := json.Marshal(val)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return string(bytes)
|
|
default:
|
|
bytes, err := json.Marshal(val)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return string(bytes)
|
|
}
|
|
}
|
|
|
|
func (api WebsiteApi) CreateEndpoint(c echo.Context) error {
|
|
var req dto.WebsiteDTO
|
|
if err := c.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
item := &model.Website{
|
|
ID: utils.UUID(),
|
|
Name: req.Name,
|
|
Enabled: req.Enabled,
|
|
TargetUrl: req.TargetUrl,
|
|
TargetHost: req.TargetHost,
|
|
TargetPort: req.TargetPort,
|
|
Domain: req.Domain,
|
|
AsciiDomain: req.AsciiDomain,
|
|
Entrance: req.Entrance,
|
|
Description: req.Description,
|
|
Status: req.Status,
|
|
StatusText: req.StatusText,
|
|
GatewayType: req.GatewayType,
|
|
GatewayId: req.GatewayId,
|
|
BasicAuth: serializeJSON(req.BasicAuth),
|
|
Headers: serializeJSON(req.Headers),
|
|
Cert: serializeJSON(req.Cert),
|
|
Public: serializeJSON(req.Public),
|
|
TempAllow: serializeJSON(req.TempAllow),
|
|
GroupId: req.GroupId,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
if err := repository.WebsiteRepository.Create(context.TODO(), item); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, "")
|
|
}
|
|
|
|
func (api WebsiteApi) UpdateEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
var req dto.WebsiteDTO
|
|
if err := c.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
item := &model.Website{
|
|
ID: id,
|
|
Name: req.Name,
|
|
Enabled: req.Enabled,
|
|
TargetUrl: req.TargetUrl,
|
|
TargetHost: req.TargetHost,
|
|
TargetPort: req.TargetPort,
|
|
Domain: req.Domain,
|
|
AsciiDomain: req.AsciiDomain,
|
|
Entrance: req.Entrance,
|
|
Description: req.Description,
|
|
Status: req.Status,
|
|
StatusText: req.StatusText,
|
|
GatewayType: req.GatewayType,
|
|
GatewayId: req.GatewayId,
|
|
BasicAuth: serializeJSON(req.BasicAuth),
|
|
Headers: serializeJSON(req.Headers),
|
|
Cert: serializeJSON(req.Cert),
|
|
Public: serializeJSON(req.Public),
|
|
TempAllow: serializeJSON(req.TempAllow),
|
|
GroupId: req.GroupId,
|
|
Sort: req.Sort,
|
|
}
|
|
|
|
if err := repository.WebsiteRepository.UpdateById(context.TODO(), item, id); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api WebsiteApi) DeleteEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
if err := repository.WebsiteRepository.DeleteById(context.TODO(), id); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api WebsiteApi) GetEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
item, err := repository.WebsiteRepository.FindById(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return Success(c, item)
|
|
}
|
|
|
|
func (api WebsiteApi) GroupsGetEndpoint(c echo.Context) error {
|
|
groups, err := repository.WebsiteGroupRepository.FindAll(context.TODO())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tree := buildWebsiteGroupTree(groups, "")
|
|
return Success(c, tree)
|
|
}
|
|
|
|
func buildWebsiteGroupTree(groups []model.WebsiteGroup, parentId string) []maps.Map {
|
|
var tree []maps.Map
|
|
for _, g := range groups {
|
|
if g.ParentId == parentId {
|
|
node := maps.Map{
|
|
"id": g.ID,
|
|
"name": g.Name,
|
|
"title": g.Name,
|
|
"key": "wgroup_" + g.ID,
|
|
"value": g.ID,
|
|
}
|
|
children := buildWebsiteGroupTree(groups, g.ID)
|
|
if len(children) > 0 {
|
|
node["children"] = children
|
|
}
|
|
tree = append(tree, node)
|
|
}
|
|
}
|
|
return tree
|
|
}
|
|
|
|
func (api WebsiteApi) GroupsSetEndpoint(c echo.Context) error {
|
|
var req []map[string]interface{}
|
|
if err := c.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
ctx := context.TODO()
|
|
_ = repository.WebsiteGroupRepository.DeleteAll(ctx)
|
|
for i, item := range req {
|
|
name := ""
|
|
if v, ok := item["name"].(string); ok {
|
|
name = v
|
|
} else if v, ok := item["title"].(string); ok {
|
|
name = v
|
|
}
|
|
group := model.WebsiteGroup{
|
|
ID: utils.UUID(),
|
|
Name: name,
|
|
ParentId: "",
|
|
Sort: i,
|
|
Created: common.NowJsonTime(),
|
|
}
|
|
_ = repository.WebsiteGroupRepository.Create(ctx, &group)
|
|
if subChildren, ok := item["children"].([]interface{}); ok {
|
|
saveWebsiteGroupChildren(ctx, subChildren, group.ID)
|
|
}
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func saveWebsiteGroupChildren(ctx context.Context, children []interface{}, parentId string) {
|
|
for i, child := range children {
|
|
m, ok := child.(map[string]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
name := ""
|
|
if v, ok := m["name"].(string); ok {
|
|
name = v
|
|
} else if v, ok := m["title"].(string); ok {
|
|
name = v
|
|
}
|
|
group := model.WebsiteGroup{
|
|
ID: utils.UUID(),
|
|
Name: name,
|
|
ParentId: parentId,
|
|
Sort: i,
|
|
Created: common.NowJsonTime(),
|
|
}
|
|
_ = repository.WebsiteGroupRepository.Create(ctx, &group)
|
|
if subChildren, ok := m["children"].([]interface{}); ok {
|
|
saveWebsiteGroupChildren(ctx, subChildren, group.ID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (api WebsiteApi) GroupsDeleteEndpoint(c echo.Context) error {
|
|
id := c.Param("id")
|
|
|
|
count, err := repository.WebsiteGroupRepository.CountByParentId(context.TODO(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
return Fail(c, -1, "该分组下存在子分组,无法删除")
|
|
}
|
|
|
|
if err := repository.WebsiteGroupRepository.DeleteById(context.TODO(), id); err != nil {
|
|
return err
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api WebsiteApi) ChangeGroupEndpoint(c echo.Context) error {
|
|
var req struct {
|
|
WebsiteIds []string `json:"websiteIds"`
|
|
GroupId string `json:"groupId"`
|
|
}
|
|
if err := c.Bind(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, websiteId := range req.WebsiteIds {
|
|
website, err := repository.WebsiteRepository.FindById(context.TODO(), websiteId)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
website.GroupId = req.GroupId
|
|
_ = repository.WebsiteRepository.UpdateById(context.TODO(), &website, websiteId)
|
|
}
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api WebsiteApi) ChangeGatewayEndpoint(c echo.Context) error {
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api WebsiteApi) SortEndpoint(c echo.Context) error {
|
|
return Success(c, nil)
|
|
}
|
|
|
|
func (api WebsiteApi) FaviconEndpoint(c echo.Context) error {
|
|
return Success(c, "")
|
|
}
|