mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	集群可以设置WebP策略
This commit is contained in:
		@@ -898,7 +898,8 @@ func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, ca
 | 
			
		||||
 | 
			
		||||
	cluster, err := this.Query(tx).
 | 
			
		||||
		Pk(clusterId).
 | 
			
		||||
		Result("timeZone", "nodeMaxThreads", "nodeTCPMaxConnections", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts").
 | 
			
		||||
		State(NodeClusterStateEnabled).
 | 
			
		||||
		Result("timeZone", "nodeMaxThreads", "nodeTCPMaxConnections", "cachePolicyId", "httpFirewallPolicyId", "autoOpenPorts", "webp", "isOn").
 | 
			
		||||
		Find()
 | 
			
		||||
	if err != nil || cluster == nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -909,6 +910,65 @@ func (this *NodeClusterDAO) FindClusterBasicInfo(tx *dbs.Tx, clusterId int64, ca
 | 
			
		||||
	return cluster.(*NodeCluster), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateClusterWebPPolicy 修改WebP设置
 | 
			
		||||
func (this *NodeClusterDAO) UpdateClusterWebPPolicy(tx *dbs.Tx, clusterId int64, webpPolicy *nodeconfigs.WebPImagePolicy) error {
 | 
			
		||||
	if webpPolicy == nil {
 | 
			
		||||
		err := this.Query(tx).
 | 
			
		||||
			Pk(clusterId).
 | 
			
		||||
			Set("webp", dbs.SQL("null")).
 | 
			
		||||
			UpdateQuickly()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return this.NotifyUpdate(tx, clusterId)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	webpPolicyJSON, err := json.Marshal(webpPolicy)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	err = this.Query(tx).
 | 
			
		||||
		Pk(clusterId).
 | 
			
		||||
		Set("webp", webpPolicyJSON).
 | 
			
		||||
		UpdateQuickly()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.NotifyUpdate(tx, clusterId)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindClusterWebPPolicy 查询WebP设置
 | 
			
		||||
func (this *NodeClusterDAO) FindClusterWebPPolicy(tx *dbs.Tx, clusterId int64, cacheMap *utils.CacheMap) (*nodeconfigs.WebPImagePolicy, error) {
 | 
			
		||||
	var cacheKey = this.Table + ":FindClusterWebPPolicy:" + types.String(clusterId)
 | 
			
		||||
	if cacheMap != nil {
 | 
			
		||||
		cache, ok := cacheMap.Get(cacheKey)
 | 
			
		||||
		if ok {
 | 
			
		||||
			return cache.(*nodeconfigs.WebPImagePolicy), nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	webpJSON, err := this.Query(tx).
 | 
			
		||||
		Pk(clusterId).
 | 
			
		||||
		Result("webp").
 | 
			
		||||
		FindJSONCol()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if IsNull(webpJSON) {
 | 
			
		||||
		return nodeconfigs.DefaultWebPImagePolicy, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var policy = &nodeconfigs.WebPImagePolicy{}
 | 
			
		||||
	err = json.Unmarshal(webpJSON, policy)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return policy, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotifyUpdate 通知更新
 | 
			
		||||
func (this *NodeClusterDAO) NotifyUpdate(tx *dbs.Tx, clusterId int64) error {
 | 
			
		||||
	return SharedNodeTaskDAO.CreateClusterTask(tx, nodeconfigs.NodeRoleNode, clusterId, 0, NodeTaskTypeConfigChanged)
 | 
			
		||||
 
 | 
			
		||||
@@ -33,6 +33,7 @@ type NodeCluster struct {
 | 
			
		||||
	NodeTCPMaxConnections uint32   `field:"nodeTCPMaxConnections"` // TCP最大连接数
 | 
			
		||||
	AutoOpenPorts         uint8    `field:"autoOpenPorts"`         // 是否自动尝试开放端口
 | 
			
		||||
	IsPinned              bool     `field:"isPinned"`              // 是否置顶
 | 
			
		||||
	Webp                  dbs.JSON `field:"webp"`                  // WebP设置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type NodeClusterOperator struct {
 | 
			
		||||
@@ -65,6 +66,7 @@ type NodeClusterOperator struct {
 | 
			
		||||
	NodeTCPMaxConnections interface{} // TCP最大连接数
 | 
			
		||||
	AutoOpenPorts         interface{} // 是否自动尝试开放端口
 | 
			
		||||
	IsPinned              interface{} // 是否置顶
 | 
			
		||||
	Webp                  interface{} // WebP设置
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewNodeClusterOperator() *NodeClusterOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -733,7 +733,7 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
 | 
			
		||||
		return nil, errors.New("node not found '" + strconv.FormatInt(nodeId, 10) + "'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config := &nodeconfigs.NodeConfig{
 | 
			
		||||
	var config = &nodeconfigs.NodeConfig{
 | 
			
		||||
		Id:       int64(node.Id),
 | 
			
		||||
		NodeId:   node.UniqueId,
 | 
			
		||||
		Secret:   node.Secret,
 | 
			
		||||
@@ -801,12 +801,13 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
 | 
			
		||||
	var clusterIds = []int64{primaryClusterId}
 | 
			
		||||
	clusterIds = append(clusterIds, node.DecodeSecondaryClusterIds()...)
 | 
			
		||||
	var clusterIndex = 0
 | 
			
		||||
	config.WebPImagePolicies = map[int64]*nodeconfigs.WebPImagePolicy{}
 | 
			
		||||
	for _, clusterId := range clusterIds {
 | 
			
		||||
		nodeCluster, err := SharedNodeClusterDAO.FindClusterBasicInfo(tx, clusterId, cacheMap)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		if nodeCluster == nil {
 | 
			
		||||
		if nodeCluster == nil || !nodeCluster.IsOn {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -848,6 +849,16 @@ func (this *NodeDAO) ComposeNodeConfig(tx *dbs.Tx, nodeId int64, cacheMap *utils
 | 
			
		||||
			config.AutoOpenPorts = nodeCluster.AutoOpenPorts == 1
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// webp
 | 
			
		||||
		if IsNotNull(nodeCluster.Webp) {
 | 
			
		||||
			var webpPolicy = &nodeconfigs.WebPImagePolicy{}
 | 
			
		||||
			err = json.Unmarshal(nodeCluster.Webp, webpPolicy)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
			config.WebPImagePolicies[clusterId] = webpPolicy
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		clusterIndex++
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -30,6 +30,17 @@ func IsNotNull(data []byte) bool {
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsNull 判断JSON是否为空
 | 
			
		||||
func IsNull(data []byte) bool {
 | 
			
		||||
	if len(data) == 0 {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	if len(data) == 4 && string(data) == "null" {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewQuery 构造Query
 | 
			
		||||
func NewQuery(tx *dbs.Tx, dao dbs.DAOWrapper, adminId int64, userId int64) *dbs.Query {
 | 
			
		||||
	query := dao.Object().Query(tx)
 | 
			
		||||
 
 | 
			
		||||
@@ -1025,6 +1025,18 @@ func (this *NodeClusterService) FindEnabledNodeClusterConfigInfo(ctx context.Con
 | 
			
		||||
	}
 | 
			
		||||
	result.HasMetricItems = countMetricItems > 0
 | 
			
		||||
 | 
			
		||||
	// webp
 | 
			
		||||
	if models.IsNotNull(cluster.Webp) {
 | 
			
		||||
		var webpPolicy = &nodeconfigs.WebPImagePolicy{}
 | 
			
		||||
		err = json.Unmarshal(cluster.Webp, webpPolicy)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		result.WebpIsOn = webpPolicy.IsOn
 | 
			
		||||
	} else {
 | 
			
		||||
		result.WebpIsOn = nodeconfigs.DefaultWebPImagePolicy.IsOn
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return result, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1043,3 +1055,45 @@ func (this *NodeClusterService) UpdateNodeClusterPinned(ctx context.Context, req
 | 
			
		||||
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledNodeClusterWebPPolicy 读取集群WebP策略
 | 
			
		||||
func (this *NodeClusterService) FindEnabledNodeClusterWebPPolicy(ctx context.Context, req *pb.FindEnabledNodeClusterWebPPolicyRequest) (*pb.FindEnabledNodeClusterWebPPolicyResponse, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	webpPolicy, err := models.SharedNodeClusterDAO.FindClusterWebPPolicy(tx, req.NodeClusterId, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	webpPolicyJSON, err := json.Marshal(webpPolicy)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return &pb.FindEnabledNodeClusterWebPPolicyResponse{
 | 
			
		||||
		WebpPolicyJSON: webpPolicyJSON,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateNodeClusterWebPPolicy 设置集群WebP策略
 | 
			
		||||
func (this *NodeClusterService) UpdateNodeClusterWebPPolicy(ctx context.Context, req *pb.UpdateNodeClusterWebPPolicyRequest) (*pb.RPCSuccess, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
	var webpPolicy = &nodeconfigs.WebPImagePolicy{}
 | 
			
		||||
	err = json.Unmarshal(req.WebpPolicyJSON, webpPolicy)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedNodeClusterDAO.UpdateClusterWebPPolicy(tx, req.NodeClusterId, webpPolicy)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
		Reference in New Issue
	
	Block a user