wireguard-dashboard/http/api/setting.go
coward b02ce4b0ba
All checks were successful
continuous-integration/drone/tag Build is passing
🎨完成基本构想的全部
2024-06-06 17:02:45 +08:00

106 lines
2.4 KiB
Go

package api
import (
"encoding/json"
"gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin"
"wireguard-dashboard/config"
"wireguard-dashboard/http/param"
"wireguard-dashboard/model/entity"
"wireguard-dashboard/queues"
"wireguard-dashboard/repository"
"wireguard-dashboard/utils"
)
type setting struct{}
func Setting() setting {
return setting{}
}
// SetSetting
// @description: 添加/更改设置
// @receiver setting
// @param c
func (setting) SetSetting(c *gin.Context) {
var p param.SetSetting
if err := c.ShouldBind(&p); err != nil {
utils.GinResponse(c).FailedWithErr("参数错误", err)
return
}
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()
}
// SetServerGlobal
// @description: 设置服务端的全局设定
// @receiver setting
// @param c
func (setting) SetServerGlobal(c *gin.Context) {
var p param.SetServerGlobal
if err := c.ShouldBind(&p); err != nil {
utils.GinResponse(c).FailedWithErr("参数错误", err)
return
}
data, _ := json.Marshal(p)
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)
}
// GetPublicNetworkIP
// @description: 获取当前机器的公网IP
// @receiver setting
// @param c
func (setting) GetPublicNetworkIP(c *gin.Context) {
utils.GinResponse(c).OKWithData(map[string]string{
"IP": utils.Network().GetHostPublicIP(),
})
}
// GetServerRestartRule
// @description: 获取服务重启规则
// @receiver setting
// @param c
func (setting) GetServerRestartRule(c *gin.Context) {
utils.GinResponse(c).OKWithData(map[string]string{
"rule": config.Config.Wireguard.ListenConfig,
})
}