mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-10 01:10:27 +08:00
集群可以设置WebP策略
This commit is contained in:
46
pkg/nodeconfigs/image_webp_policy.go
Normal file
46
pkg/nodeconfigs/image_webp_policy.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
|
||||||
|
package nodeconfigs
|
||||||
|
|
||||||
|
import "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
_ = DefaultWebPImagePolicy.Init()
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultWebPImagePolicy = &WebPImagePolicy{
|
||||||
|
IsOn: true,
|
||||||
|
RequireCache: true,
|
||||||
|
MinLength: shared.NewSizeCapacity(0, shared.SizeCapacityUnitKB),
|
||||||
|
MaxLength: shared.NewSizeCapacity(128, shared.SizeCapacityUnitMB),
|
||||||
|
}
|
||||||
|
|
||||||
|
// WebPImagePolicy WebP策略
|
||||||
|
type WebPImagePolicy struct {
|
||||||
|
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||||
|
RequireCache bool `yaml:"requireCache" json:"requireCache"` // 需要在缓存条件下进行
|
||||||
|
MinLength *shared.SizeCapacity `yaml:"minLength" json:"minLength"` // 最小压缩对象比如4m, 24k
|
||||||
|
MaxLength *shared.SizeCapacity `yaml:"maxLength" json:"maxLength"` // 最大压缩对象
|
||||||
|
|
||||||
|
minLength int64
|
||||||
|
maxLength int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *WebPImagePolicy) Init() error {
|
||||||
|
if this.MinLength != nil {
|
||||||
|
this.minLength = this.MinLength.Bytes()
|
||||||
|
}
|
||||||
|
if this.MaxLength != nil {
|
||||||
|
this.maxLength = this.MaxLength.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *WebPImagePolicy) MinLengthBytes() int64 {
|
||||||
|
return this.minLength
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *WebPImagePolicy) MaxLengthBytes() int64 {
|
||||||
|
return this.maxLength
|
||||||
|
}
|
||||||
@@ -68,6 +68,9 @@ type NodeConfig struct {
|
|||||||
// 脚本
|
// 脚本
|
||||||
CommonScripts []*serverconfigs.CommonScript `yaml:"commonScripts" json:"commonScripts"`
|
CommonScripts []*serverconfigs.CommonScript `yaml:"commonScripts" json:"commonScripts"`
|
||||||
|
|
||||||
|
// WebP
|
||||||
|
WebPImagePolicies map[int64]*WebPImagePolicy `yaml:"webpImagePolicies" json:"webpImagePolicies"`
|
||||||
|
|
||||||
paddedId string
|
paddedId string
|
||||||
|
|
||||||
// firewall
|
// firewall
|
||||||
@@ -284,6 +287,16 @@ func (this *NodeConfig) Init() (err error, serverErrors []*ServerError) {
|
|||||||
this.allowedIPMap[allowIP] = true
|
this.allowedIPMap[allowIP] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// webp image policy
|
||||||
|
if this.WebPImagePolicies != nil {
|
||||||
|
for _, policy := range this.WebPImagePolicies {
|
||||||
|
err = policy.Init()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,3 +465,11 @@ func (this *NodeConfig) UpdateCertOCSP(certId int64, ocsp []byte, expiresAt int6
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FindWebPImagePolicyWithClusterId 使用集群ID查找WebP策略
|
||||||
|
func (this *NodeConfig) FindWebPImagePolicyWithClusterId(clusterId int64) *WebPImagePolicy {
|
||||||
|
if this.WebPImagePolicies == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return this.WebPImagePolicies[clusterId]
|
||||||
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -115,6 +115,12 @@ service NodeClusterService {
|
|||||||
|
|
||||||
// 设置集群是否置顶
|
// 设置集群是否置顶
|
||||||
rpc updateNodeClusterPinned(UpdateNodeClusterPinnedRequest) returns (RPCSuccess);
|
rpc updateNodeClusterPinned(UpdateNodeClusterPinnedRequest) returns (RPCSuccess);
|
||||||
|
|
||||||
|
// 读取集群WebP策略
|
||||||
|
rpc findEnabledNodeClusterWebPPolicy(FindEnabledNodeClusterWebPPolicyRequest) returns (FindEnabledNodeClusterWebPPolicyResponse);
|
||||||
|
|
||||||
|
// 设置集群WebP策略
|
||||||
|
rpc updateNodeClusterWebPPolicy(UpdateNodeClusterWebPPolicyRequest) returns (RPCSuccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取所有集群的信息
|
// 获取所有集群的信息
|
||||||
@@ -421,6 +427,7 @@ message FindEnabledNodeClusterConfigInfoResponse {
|
|||||||
bool hasMessageReceivers = 4;
|
bool hasMessageReceivers = 4;
|
||||||
bool isTOAEnabled = 5;
|
bool isTOAEnabled = 5;
|
||||||
bool hasMetricItems = 6;
|
bool hasMetricItems = 6;
|
||||||
|
bool webpIsOn = 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置集群是否置顶
|
// 设置集群是否置顶
|
||||||
@@ -428,3 +435,18 @@ message UpdateNodeClusterPinnedRequest {
|
|||||||
int64 nodeClusterId = 1;
|
int64 nodeClusterId = 1;
|
||||||
bool isPinned = 2;
|
bool isPinned = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 读取集群WebP策略
|
||||||
|
message FindEnabledNodeClusterWebPPolicyRequest {
|
||||||
|
int64 nodeClusterId = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message FindEnabledNodeClusterWebPPolicyResponse {
|
||||||
|
bytes webpPolicyJSON = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置集群WebP策略
|
||||||
|
message UpdateNodeClusterWebPPolicyRequest {
|
||||||
|
int64 nodeClusterId = 1;
|
||||||
|
bytes webpPolicyJSON = 2;
|
||||||
|
}
|
||||||
@@ -21,6 +21,13 @@ type SizeCapacity struct {
|
|||||||
Unit SizeCapacityUnit `json:"unit" yaml:"unit"`
|
Unit SizeCapacityUnit `json:"unit" yaml:"unit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewSizeCapacity(count int64, unit SizeCapacityUnit) *SizeCapacity {
|
||||||
|
return &SizeCapacity{
|
||||||
|
Count: count,
|
||||||
|
Unit: unit,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (this *SizeCapacity) Bytes() int64 {
|
func (this *SizeCapacity) Bytes() int64 {
|
||||||
if this.Count < 0 {
|
if this.Count < 0 {
|
||||||
return -1
|
return -1
|
||||||
|
|||||||
Reference in New Issue
Block a user