wireguard-dashboard/http/api/client.go

70 lines
1.4 KiB
Go
Raw Normal View History

2024-03-11 14:53:28 +08:00
package api
import (
2024-03-13 17:05:02 +08:00
"gitee.ltd/lxh/logger/log"
2024-03-11 14:53:28 +08:00
"github.com/gin-gonic/gin"
"wireguard-dashboard/http/param"
2024-03-13 17:05:02 +08:00
"wireguard-dashboard/model/entity"
"wireguard-dashboard/queues"
2024-03-11 14:53:28 +08:00
"wireguard-dashboard/repository"
"wireguard-dashboard/utils"
)
type clients struct{}
func Client() clients {
return clients{}
}
// List
// @description: 客户端列表
// @receiver clients
// @param c
func (clients) List(c *gin.Context) {
var p param.ClientList
if err := c.ShouldBind(&p); err != nil {
utils.GinResponse(c).FailedWithErr("参数错误", err)
return
}
data, total, err := repository.Client().List(p)
if err != nil {
utils.GinResponse(c).FailedWithMsg("获取失败")
return
}
utils.GinResponse(c).OkWithPage(data, total, p.Current, p.Size)
}
2024-03-11 17:26:41 +08:00
// Save
// @description: 新增/更新客户端
// @receiver clients
// @param c
func (clients) Save(c *gin.Context) {
var p param.SaveClient
if err := c.ShouldBind(&p); err != nil {
utils.GinResponse(c).FailedWithErr("参数错误", err)
return
}
2024-03-13 17:05:02 +08:00
info, ok := c.Get("user")
if !ok {
utils.GinResponse(c).FailedWithMsg("获取信息失败")
return
}
_, err := repository.Client().Save(p, info.(*entity.User).Id)
if err != nil {
utils.GinResponse(c).FailedWithMsg("操作失败")
return
}
go func() {
if err = queues.PutAsyncWireguardConfigFile(p.ServerId); err != nil {
log.Errorf("[新增/编辑客户端]同步配置文件失败: %v", err.Error())
}
}()
utils.GinResponse(c).OK()
2024-03-11 17:26:41 +08:00
}