可以在分组中设置一些全局配置选项

This commit is contained in:
GoEdgeLab
2021-09-22 19:39:43 +08:00
parent cbf95a9cc0
commit 13e176059f
7 changed files with 629 additions and 24 deletions

View File

@@ -1,10 +1,13 @@
package models
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
@@ -34,7 +37,7 @@ func init() {
})
}
// 启用条目
// EnableServerGroup 启用条目
func (this *ServerGroupDAO) EnableServerGroup(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -43,7 +46,7 @@ func (this *ServerGroupDAO) EnableServerGroup(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableServerGroup 禁用条目
func (this *ServerGroupDAO) DisableServerGroup(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -52,7 +55,7 @@ func (this *ServerGroupDAO) DisableServerGroup(tx *dbs.Tx, id int64) error {
return err
}
// 查找启用中的条目
// FindEnabledServerGroup 查找启用中的条目
func (this *ServerGroupDAO) FindEnabledServerGroup(tx *dbs.Tx, id int64) (*ServerGroup, error) {
result, err := this.Query(tx).
Pk(id).
@@ -64,7 +67,7 @@ func (this *ServerGroupDAO) FindEnabledServerGroup(tx *dbs.Tx, id int64) (*Serve
return result.(*ServerGroup), err
}
// 根据主键查找名称
// FindServerGroupName 根据主键查找名称
func (this *ServerGroupDAO) FindServerGroupName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -72,11 +75,12 @@ func (this *ServerGroupDAO) FindServerGroupName(tx *dbs.Tx, id int64) (string, e
FindStringCol("")
}
// 创建分组
// CreateGroup 创建分组
func (this *ServerGroupDAO) CreateGroup(tx *dbs.Tx, name string) (groupId int64, err error) {
op := NewServerGroupOperator()
op.State = ServerGroupStateEnabled
op.Name = name
op.IsOn = true
err = this.Save(tx, op)
if err != nil {
return 0, err
@@ -84,7 +88,7 @@ func (this *ServerGroupDAO) CreateGroup(tx *dbs.Tx, name string) (groupId int64,
return types.Int64(op.Id), nil
}
// 修改分组
// UpdateGroup 修改分组
func (this *ServerGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, name string) error {
if groupId <= 0 {
return errors.New("invalid groupId")
@@ -96,7 +100,7 @@ func (this *ServerGroupDAO) UpdateGroup(tx *dbs.Tx, groupId int64, name string)
return err
}
// 查找所有分组
// FindAllEnabledGroups 查找所有分组
func (this *ServerGroupDAO) FindAllEnabledGroups(tx *dbs.Tx) (result []*ServerGroup, err error) {
_, err = this.Query(tx).
State(ServerGroupStateEnabled).
@@ -107,7 +111,7 @@ func (this *ServerGroupDAO) FindAllEnabledGroups(tx *dbs.Tx) (result []*ServerGr
return
}
// 修改分组排序
// UpdateGroupOrders 修改分组排序
func (this *ServerGroupDAO) UpdateGroupOrders(tx *dbs.Tx, groupIds []int64) error {
for index, groupId := range groupIds {
_, err := this.Query(tx).
@@ -120,3 +124,221 @@ func (this *ServerGroupDAO) UpdateGroupOrders(tx *dbs.Tx, groupIds []int64) erro
}
return nil
}
// FindHTTPReverseProxyRef 根据条件获取HTTP反向代理配置
func (this *ServerGroupDAO) FindHTTPReverseProxyRef(tx *dbs.Tx, groupId int64) (*serverconfigs.ReverseProxyRef, error) {
reverseProxy, err := this.Query(tx).
Pk(groupId).
Result("httpReverseProxy").
FindStringCol("")
if err != nil {
return nil, err
}
if len(reverseProxy) == 0 || reverseProxy == "null" {
return nil, nil
}
config := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal([]byte(reverseProxy), config)
return config, err
}
// FindTCPReverseProxyRef 根据条件获取TCP反向代理配置
func (this *ServerGroupDAO) FindTCPReverseProxyRef(tx *dbs.Tx, groupId int64) (*serverconfigs.ReverseProxyRef, error) {
reverseProxy, err := this.Query(tx).
Pk(groupId).
Result("tcpReverseProxy").
FindStringCol("")
if err != nil {
return nil, err
}
if len(reverseProxy) == 0 || reverseProxy == "null" {
return nil, nil
}
config := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal([]byte(reverseProxy), config)
return config, err
}
// FindUDPReverseProxyRef 根据条件获取UDP反向代理配置
func (this *ServerGroupDAO) FindUDPReverseProxyRef(tx *dbs.Tx, groupId int64) (*serverconfigs.ReverseProxyRef, error) {
reverseProxy, err := this.Query(tx).
Pk(groupId).
Result("udpReverseProxy").
FindStringCol("")
if err != nil {
return nil, err
}
if len(reverseProxy) == 0 || reverseProxy == "null" {
return nil, nil
}
config := &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal([]byte(reverseProxy), config)
return config, err
}
// UpdateHTTPReverseProxy 修改HTTP反向代理配置
func (this *ServerGroupDAO) UpdateHTTPReverseProxy(tx *dbs.Tx, groupId int64, config []byte) error {
if groupId <= 0 {
return errors.New("groupId should not be smaller than 0")
}
op := NewServerGroupOperator()
op.Id = groupId
op.HttpReverseProxy = JSONBytes(config)
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, groupId)
}
// UpdateTCPReverseProxy 修改TCP反向代理配置
func (this *ServerGroupDAO) UpdateTCPReverseProxy(tx *dbs.Tx, groupId int64, config []byte) error {
if groupId <= 0 {
return errors.New("groupId should not be smaller than 0")
}
op := NewServerGroupOperator()
op.Id = groupId
op.TcpReverseProxy = JSONBytes(config)
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, groupId)
}
// UpdateUDPReverseProxy 修改UDP反向代理配置
func (this *ServerGroupDAO) UpdateUDPReverseProxy(tx *dbs.Tx, groupId int64, config []byte) error {
if groupId <= 0 {
return errors.New("groupId should not be smaller than 0")
}
op := NewServerGroupOperator()
op.Id = groupId
op.UdpReverseProxy = JSONBytes(config)
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, groupId)
}
// ComposeGroupConfig 组合配置
func (this *ServerGroupDAO) ComposeGroupConfig(tx *dbs.Tx, groupId int64, cacheMap maps.Map) (*serverconfigs.ServerGroupConfig, error) {
if cacheMap == nil {
cacheMap = maps.Map{}
}
var cacheKey = this.Table + ":config:" + types.String(groupId)
var cacheConfig = cacheMap.Get(cacheKey)
if cacheConfig != nil {
// 克隆防止分解后的Server配置相互受到影响
configJSON, err := json.Marshal(cacheConfig)
if err != nil {
return nil, err
}
var clonedConfig = &serverconfigs.ServerGroupConfig{}
err = json.Unmarshal(configJSON, clonedConfig)
if err != nil {
return nil, err
}
return clonedConfig, nil
}
group, err := this.FindEnabledServerGroup(tx, groupId)
if err != nil {
return nil, err
}
if group == nil {
return nil, nil
}
var config = &serverconfigs.ServerGroupConfig{
Id: int64(group.Id),
Name: group.Name,
IsOn: group.IsOn == 1,
}
if len(group.HttpReverseProxy) > 0 {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal([]byte(group.HttpReverseProxy), reverseProxyRef)
if err != nil {
return nil, err
}
config.HTTPReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.HTTPReverseProxy = reverseProxyConfig
}
}
if len(group.TcpReverseProxy) > 0 {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal([]byte(group.TcpReverseProxy), reverseProxyRef)
if err != nil {
return nil, err
}
config.TCPReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.TCPReverseProxy = reverseProxyConfig
}
}
if len(group.UdpReverseProxy) > 0 {
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
err := json.Unmarshal([]byte(group.UdpReverseProxy), reverseProxyRef)
if err != nil {
return nil, err
}
config.UDPReverseProxyRef = reverseProxyRef
reverseProxyConfig, err := SharedReverseProxyDAO.ComposeReverseProxyConfig(tx, reverseProxyRef.ReverseProxyId, cacheMap)
if err != nil {
return nil, err
}
if reverseProxyConfig != nil {
config.UDPReverseProxy = reverseProxyConfig
}
}
cacheMap[cacheKey] = config
return config, nil
}
// FindEnabledGroupIdWithReverseProxyId 查找包含某个反向代理的服务分组
func (this *ServerGroupDAO) FindEnabledGroupIdWithReverseProxyId(tx *dbs.Tx, reverseProxyId int64) (serverId int64, err error) {
return this.Query(tx).
State(ServerStateEnabled).
Where("(JSON_CONTAINS(httpReverseProxy, :jsonQuery) OR JSON_CONTAINS(tcpReverseProxy, :jsonQuery) OR JSON_CONTAINS(udpReverseProxy, :jsonQuery))").
Param("jsonQuery", maps.Map{"reverseProxyId": reverseProxyId}.AsJSON()).
ResultPk().
FindInt64Col(0)
}
// NotifyUpdate 通知更新
func (this *ServerGroupDAO) NotifyUpdate(tx *dbs.Tx, groupId int64) error {
serverIds, err := SharedServerDAO.FindAllEnabledServerIdsWithGroupId(tx, groupId)
if err != nil {
return err
}
for _, serverId := range serverIds {
err = SharedServerDAO.NotifyUpdate(tx, serverId)
if err != nil {
return err
}
}
return nil
}