wireguard-dashboard/http/api/setting.go

106 lines
2.4 KiB
Go
Raw Permalink Normal View History

2024-03-14 15:23:16 +08:00
package api
import (
2024-03-14 16:00:28 +08:00
"encoding/json"
2024-03-14 15:23:16 +08:00
"gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin"
2024-06-06 17:02:45 +08:00
"wireguard-dashboard/config"
2024-03-14 15:23:16 +08:00
"wireguard-dashboard/http/param"
2024-03-14 16:00:28 +08:00
"wireguard-dashboard/model/entity"
"wireguard-dashboard/queues"
2024-03-14 15:23:16 +08:00
"wireguard-dashboard/repository"
"wireguard-dashboard/utils"
)
type setting struct{}
func Setting() setting {
return setting{}
}
2024-03-14 16:00:28 +08:00
// SetSetting
// @description: 添加/更改设置
2024-03-14 15:23:16 +08:00
// @receiver setting
// @param c
2024-03-14 16:00:28 +08:00
func (setting) SetSetting(c *gin.Context) {
var p param.SetSetting
if err := c.ShouldBind(&p); err != nil {
utils.GinResponse(c).FailedWithErr("参数错误", err)
return
2024-03-14 15:23:16 +08:00
}
2024-03-14 16:00:28 +08:00
if err := repository.System().Save(&entity.Setting{
Code: p.Code,
Data: p.Data,
Describe: p.Describe,
}); err != nil {
utils.GinResponse(c).FailedWithErr("操作失败", err)
return
}
utils.GinResponse(c).OK()
2024-03-14 15:23:16 +08:00
}
2024-03-14 16:00:28 +08:00
// SetServerGlobal
// @description: 设置服务端的全局设定
2024-03-14 15:23:16 +08:00
// @receiver setting
// @param c
2024-03-14 16:00:28 +08:00
func (setting) SetServerGlobal(c *gin.Context) {
var p param.SetServerGlobal
2024-03-14 15:23:16 +08:00
if err := c.ShouldBind(&p); err != nil {
utils.GinResponse(c).FailedWithErr("参数错误", err)
return
}
2024-05-24 15:19:26 +08:00
data, _ := json.Marshal(p)
2024-03-14 16:00:28 +08:00
var ent entity.Setting
ent.Code = "SERVER_SETTING"
ent.Data = string(data)
if err := repository.System().Save(&ent); err != nil {
utils.GinResponse(c).FailedWithErr("操作失败", err)
return
}
go func() {
if err := queues.PutAsyncWireguardConfigFile(""); err != nil {
log.Errorf("[设置服务端],发起同步配置文件失败: %v", err.Error())
}
}()
utils.GinResponse(c).OK()
}
// GetGlobalSetting
// @description: 获取全局设置配置
// @receiver setting
// @param c
func (setting) GetGlobalSetting(c *gin.Context) {
data, err := repository.System().GetServerSetting()
if err != nil {
log.Errorf("获取配置失败: %v", err.Error())
}
utils.GinResponse(c).OKWithData(data)
2024-03-14 15:23:16 +08:00
}
// GetPublicNetworkIP
// @description: 获取当前机器的公网IP
// @receiver setting
// @param c
func (setting) GetPublicNetworkIP(c *gin.Context) {
utils.GinResponse(c).OKWithData(map[string]string{
"IP": utils.Network().GetHostPublicIP(),
})
}
2024-06-06 17:02:45 +08:00
// GetServerRestartRule
// @description: 获取服务重启规则
// @receiver setting
// @param c
func (setting) GetServerRestartRule(c *gin.Context) {
utils.GinResponse(c).OKWithData(map[string]string{
"rule": config.Config.Wireguard.ListenConfig,
})
}