|
@@ -9,9 +9,14 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
+// HostService 接口定义
|
|
|
type HostService interface {
|
|
|
GetHost(ctx context.Context, id int64) (*model.Host, error)
|
|
|
GetGameShieldConfig(ctx context.Context, hostId int) (v1.GameShieldHostBackendConfigResponse, error)
|
|
|
+ GetGlobalLimitConfig(ctx context.Context, hostId int) (v1.GlobalLimitConfigResponse, error)
|
|
|
+ GetTcpLimitConfig(ctx context.Context, hostId int) (v1.TcpLimitRequest, error)
|
|
|
+ GetUdpLimitConfig(ctx context.Context, hostId int) (v1.UdpLimitRequest, error)
|
|
|
+ GetWebLimitConfig(ctx context.Context, hostId int) (v1.WebLimitRequest, error)
|
|
|
}
|
|
|
|
|
|
func NewHostService(
|
|
@@ -24,13 +29,31 @@ func NewHostService(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// 常量定义,包含新增的配置项名称
|
|
|
const (
|
|
|
- ConfigOnlineDevices = "在线设备数"
|
|
|
- ConfigRuleEntries = "规则条目"
|
|
|
- ConfigMaxBandwidth = "高带宽转发规则条目"
|
|
|
- ConfigSourceMachines = "支持源机"
|
|
|
+ ConfigOnlineDevices = "在线设备数"
|
|
|
+ ConfigRuleEntries = "规则条目"
|
|
|
+ ConfigMaxBandwidth = "高带宽转发规则条目"
|
|
|
+ ConfigSourceMachines = "支持源机"
|
|
|
+ ConfigBps = "最大带宽"
|
|
|
+ ConfigMaxBytesMonth = "每月流量"
|
|
|
+ ConfigTcpConnCount = "TCP连接次数"
|
|
|
+ ConfigTcpConnDuration = "TCP连接时长"
|
|
|
+ ConfigTcpMaxConnCount = "TCP最大连接数"
|
|
|
+ ConfigUdpQosPacketCount = "UDPQPS包数"
|
|
|
+ ConfigUdpQosPacketDuration = "UDPQPS周期"
|
|
|
+ ConfigUdpMaxConnCount = "UDP最大会话数"
|
|
|
+ ConfigWebQpsCount = "WebQPS连接次数"
|
|
|
+ ConfigWebQpsDuration = "WebQPS周期"
|
|
|
)
|
|
|
|
|
|
+// unitSuffixMap 存储需要去除的单位后缀
|
|
|
+var unitSuffixMap = map[string]string{
|
|
|
+ ConfigOnlineDevices: "个",
|
|
|
+ ConfigRuleEntries: "个",
|
|
|
+ ConfigMaxBandwidth: "条",
|
|
|
+}
|
|
|
+
|
|
|
type hostService struct {
|
|
|
*Service
|
|
|
hostRepository repository.HostRepository
|
|
@@ -40,6 +63,7 @@ func (s *hostService) GetHost(ctx context.Context, id int64) (*model.Host, error
|
|
|
return s.hostRepository.GetHost(ctx, id)
|
|
|
}
|
|
|
|
|
|
+// GetHostConfig 保持不变,它是一个通用的底层数据获取方法。
|
|
|
func (s *hostService) GetHostConfig(ctx context.Context, hostId int) ([]map[string]string, error) {
|
|
|
configOptions, err := s.hostRepository.GetHostConfig(ctx, hostId)
|
|
|
if err != nil {
|
|
@@ -93,46 +117,170 @@ func (s *hostService) GetHostConfig(ctx context.Context, hostId int) ([]map[stri
|
|
|
return data, nil
|
|
|
}
|
|
|
|
|
|
-func (s *hostService) tidyGetGameShieldConfig(ctx context.Context, configName string, optionName string) (map[string]int, error) {
|
|
|
+// cleanConfigOptionName 简化后的清理函数,直接返回清理后的字符串。
|
|
|
+func (s *hostService) cleanConfigOptionName(configName string, optionName string) string {
|
|
|
// 根据配置名称去除相应的单位后缀
|
|
|
- switch configName {
|
|
|
- case ConfigOnlineDevices, ConfigRuleEntries:
|
|
|
- optionName = strings.TrimSuffix(optionName, "个")
|
|
|
- case ConfigMaxBandwidth:
|
|
|
- optionName = strings.TrimSuffix(optionName, "条")
|
|
|
+ if suffix, ok := unitSuffixMap[configName]; ok {
|
|
|
+ optionName = strings.TrimSuffix(optionName, suffix)
|
|
|
+ }
|
|
|
+ return optionName
|
|
|
+}
|
|
|
+
|
|
|
+// getHostConfigsMap 新增的辅助函数,用于获取所有清理后的配置映射。
|
|
|
+func (s *hostService) getHostConfigsMap(ctx context.Context, hostId int) (map[string]string, error) {
|
|
|
+ baseData, err := s.GetHostConfig(ctx, hostId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
|
|
|
- // 转换为整数并返回
|
|
|
- return map[string]int{configName: cast.ToInt(optionName)}, nil
|
|
|
+ configsMap := make(map[string]string)
|
|
|
+ for _, item := range baseData {
|
|
|
+ configName := item["config_name"]
|
|
|
+ optionName := item["option_name"]
|
|
|
+ // 将清理后的值存储到 map 中
|
|
|
+ configsMap[configName] = s.cleanConfigOptionName(configName, optionName)
|
|
|
+ }
|
|
|
+ return configsMap, nil
|
|
|
}
|
|
|
+
|
|
|
func (s *hostService) GetGameShieldConfig(ctx context.Context, hostId int) (v1.GameShieldHostBackendConfigResponse, error) {
|
|
|
- baseData, err := s.GetHostConfig(ctx, hostId)
|
|
|
+ // 调用新辅助函数获取处理好的配置映射
|
|
|
+ configsMap, err := s.getHostConfigsMap(ctx, hostId)
|
|
|
if err != nil {
|
|
|
return v1.GameShieldHostBackendConfigResponse{}, err
|
|
|
}
|
|
|
|
|
|
var data v1.GameShieldHostBackendConfigResponse
|
|
|
+ var parseErr error // 用于统一处理类型转换错误
|
|
|
|
|
|
- for _, item := range baseData {
|
|
|
- configName := item["config_name"]
|
|
|
- optionName := item["option_name"]
|
|
|
+ // 从映射中获取值并进行类型转换
|
|
|
+ if val, ok := configsMap[ConfigOnlineDevices]; ok {
|
|
|
+ data.OnlineDevicesCount, parseErr = cast.ToInt64E(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigRuleEntries]; ok {
|
|
|
+ data.RuleEntriesCount, parseErr = cast.ToInt64E(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigMaxBandwidth]; ok {
|
|
|
+ data.MaxBandwidthCount, parseErr = cast.ToInt64E(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigSourceMachines]; ok {
|
|
|
+ data.SourceMachinesCount, parseErr = cast.ToInt64E(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return data, nil
|
|
|
+}
|
|
|
|
|
|
- res, err := s.tidyGetGameShieldConfig(ctx, configName, optionName)
|
|
|
- if err != nil {
|
|
|
- return v1.GameShieldHostBackendConfigResponse{}, err
|
|
|
+func (s *hostService) GetGlobalLimitConfig(ctx context.Context, hostId int) (v1.GlobalLimitConfigResponse, error) {
|
|
|
+ configsMap, err := s.getHostConfigsMap(ctx, hostId)
|
|
|
+ if err != nil {
|
|
|
+ return v1.GlobalLimitConfigResponse{}, err
|
|
|
+ }
|
|
|
+ data := v1.GlobalLimitConfigResponse{
|
|
|
+ MaxBytesMonth: "0",
|
|
|
+ Bps: "0",
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigBps]; ok {
|
|
|
+ data.Bps = val
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigMaxBytesMonth]; ok {
|
|
|
+ data.MaxBytesMonth = val
|
|
|
+ }
|
|
|
+ return data, nil
|
|
|
+}
|
|
|
+
|
|
|
+// GetTcpLimitConfig 修正返回类型,并使用新的辅助函数
|
|
|
+func (s *hostService) GetTcpLimitConfig(ctx context.Context, hostId int) (v1.TcpLimitRequest, error) {
|
|
|
+ configsMap, err := s.getHostConfigsMap(ctx, hostId)
|
|
|
+ if err != nil {
|
|
|
+ return v1.TcpLimitRequest{}, err
|
|
|
+ }
|
|
|
+ data := v1.TcpLimitRequest{
|
|
|
+ ConnCount: 0,
|
|
|
+ ConnDuration: "0s",
|
|
|
+ MaxConnCount: 0,
|
|
|
+ }
|
|
|
+ var parseErr error
|
|
|
+
|
|
|
+ if val, ok := configsMap[ConfigTcpConnCount]; ok {
|
|
|
+ data.ConnCount, parseErr = cast.ToIntE(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigTcpConnDuration]; ok {
|
|
|
+ data.ConnDuration = val
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigTcpMaxConnCount]; ok {
|
|
|
+ data.MaxConnCount, parseErr = cast.ToIntE(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
}
|
|
|
+ }
|
|
|
+ return data, nil // 返回结构体
|
|
|
+}
|
|
|
|
|
|
- // 使用switch直接设置对应字段
|
|
|
- switch configName {
|
|
|
- case ConfigOnlineDevices:
|
|
|
- data.OnlineDevicesCount = int64(res[configName])
|
|
|
- case ConfigRuleEntries:
|
|
|
- data.RuleEntriesCount = int64(res[configName])
|
|
|
- case ConfigMaxBandwidth:
|
|
|
- data.MaxBandwidthCount = int64(res[configName])
|
|
|
- case ConfigSourceMachines:
|
|
|
- data.SourceMachinesCount = int64(res[configName])
|
|
|
+// GetUdpLimitConfig
|
|
|
+func (s *hostService) GetUdpLimitConfig(ctx context.Context, hostId int) (v1.UdpLimitRequest, error) {
|
|
|
+ configsMap, err := s.getHostConfigsMap(ctx, hostId)
|
|
|
+ if err != nil {
|
|
|
+ return v1.UdpLimitRequest{}, err
|
|
|
+ }
|
|
|
+ data := v1.UdpLimitRequest{
|
|
|
+ QosPacketCount: 0,
|
|
|
+ QosPacketDuration: "0s",
|
|
|
+ MaxConnCount: 0,
|
|
|
+ }
|
|
|
+ var parseErr error
|
|
|
+
|
|
|
+ if val, ok := configsMap[ConfigUdpQosPacketCount]; ok {
|
|
|
+ data.QosPacketCount, parseErr = cast.ToIntE(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigUdpQosPacketDuration]; ok {
|
|
|
+ data.QosPacketDuration = val
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigUdpMaxConnCount]; ok {
|
|
|
+ data.MaxConnCount, parseErr = cast.ToIntE(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
}
|
|
|
}
|
|
|
return data, nil
|
|
|
}
|
|
|
+
|
|
|
+// GetWebLimitConfig 修正返回类型,并使用新的辅助函数
|
|
|
+func (s *hostService) GetWebLimitConfig(ctx context.Context, hostId int) (v1.WebLimitRequest, error) {
|
|
|
+ configsMap, err := s.getHostConfigsMap(ctx, hostId)
|
|
|
+ if err != nil {
|
|
|
+ return v1.WebLimitRequest{}, err
|
|
|
+ }
|
|
|
+ data := v1.WebLimitRequest{
|
|
|
+ QpsCount: 0,
|
|
|
+ QpsDuration: "0s",
|
|
|
+ }
|
|
|
+ var parseErr error
|
|
|
+
|
|
|
+ if val, ok := configsMap[ConfigWebQpsCount]; ok {
|
|
|
+ data.QpsCount, parseErr = cast.ToIntE(val)
|
|
|
+ if parseErr != nil {
|
|
|
+ return data, parseErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if val, ok := configsMap[ConfigWebQpsDuration]; ok {
|
|
|
+ data.QpsDuration = val
|
|
|
+ }
|
|
|
+ return data, nil
|
|
|
+}
|