143 lines
2.4 KiB
Go
143 lines
2.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"platform/web/auth"
|
|
"platform/web/core"
|
|
g "platform/web/globals"
|
|
s "platform/web/services"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// ====================
|
|
// admin 路由
|
|
// ====================
|
|
|
|
func PageProxyByAdmin(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeProxyRead)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req core.PageReq
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
list, total, err := s.Proxy.Page(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(core.PageResp{
|
|
List: list,
|
|
Total: int(total),
|
|
Page: req.GetPage(),
|
|
Size: req.GetSize(),
|
|
})
|
|
}
|
|
|
|
func AllProxyByAdmin(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeProxyRead)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
list, err := s.Proxy.All()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(list)
|
|
}
|
|
|
|
func CreateProxy(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeProxyWrite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req s.CreateProxy
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.Proxy.Create(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(nil)
|
|
}
|
|
|
|
func UpdateProxy(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeProxyWrite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req s.UpdateProxy
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.Proxy.Update(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(nil)
|
|
}
|
|
|
|
func UpdateProxyStatus(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeProxyWriteStatus)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req s.UpdateProxyStatus
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.Proxy.UpdateStatus(&req); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(nil)
|
|
}
|
|
|
|
func SyncProxyPool(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeProxyWrite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req core.IdReq
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.Proxy.SyncPool(req.Id); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(nil)
|
|
}
|
|
|
|
func RemoveProxy(c *fiber.Ctx) error {
|
|
_, err := auth.GetAuthCtx(c).PermitAdmin(core.ScopeProxyWrite)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var req core.IdReq
|
|
if err := g.Validator.ParseBody(c, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := s.Proxy.Remove(req.Id); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(nil)
|
|
}
|