mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	实现gzip
This commit is contained in:
		@@ -118,13 +118,13 @@ func (this *HTTPAccessLogPolicyDAO) ComposeAccessLogPolicyConfig(policyId int64)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 条件
 | 
			
		||||
	if IsNotNull(policy.CondGroups) {
 | 
			
		||||
		condGroups := []*shared.HTTPRequestCondGroup{}
 | 
			
		||||
		err = json.Unmarshal([]byte(policy.CondGroups), &condGroups)
 | 
			
		||||
	if IsNotNull(policy.Conds) {
 | 
			
		||||
		condsConfig := &shared.HTTPRequestCondsConfig{}
 | 
			
		||||
		err = json.Unmarshal([]byte(policy.Conds), condsConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.CondGroups = condGroups
 | 
			
		||||
		config.Conds = condsConfig
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return config, nil
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ type HTTPAccessLogPolicy struct {
 | 
			
		||||
	IsOn       uint8  `field:"isOn"`       // 是否启用
 | 
			
		||||
	Type       string `field:"type"`       // 存储类型
 | 
			
		||||
	Options    string `field:"options"`    // 存储选项
 | 
			
		||||
	CondGroups string `field:"condGroups"` // 请求条件
 | 
			
		||||
	Conds      string `field:"conds"`      // 请求条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPAccessLogPolicyOperator struct {
 | 
			
		||||
@@ -26,7 +26,7 @@ type HTTPAccessLogPolicyOperator struct {
 | 
			
		||||
	IsOn       interface{} // 是否启用
 | 
			
		||||
	Type       interface{} // 存储类型
 | 
			
		||||
	Options    interface{} // 存储选项
 | 
			
		||||
	CondGroups interface{} // 请求条件
 | 
			
		||||
	Conds      interface{} // 请求条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHTTPAccessLogPolicyOperator() *HTTPAccessLogPolicyOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ type HTTPCachePolicy struct {
 | 
			
		||||
	SkipCacheControlValues   string `field:"skipCacheControlValues"`   // 忽略的cache-control
 | 
			
		||||
	SkipSetCookie            uint8  `field:"skipSetCookie"`            // 是否忽略Set-Cookie Header
 | 
			
		||||
	EnableRequestCachePragma uint8  `field:"enableRequestCachePragma"` // 是否支持客户端的Pragma: no-cache
 | 
			
		||||
	CondGroups               string `field:"condGroups"`               // 请求条件
 | 
			
		||||
	Conds                    string `field:"conds"`                    // 请求条件
 | 
			
		||||
	CreatedAt                uint64 `field:"createdAt"`                // 创建时间
 | 
			
		||||
	State                    uint8  `field:"state"`                    // 状态
 | 
			
		||||
}
 | 
			
		||||
@@ -36,7 +36,7 @@ type HTTPCachePolicyOperator struct {
 | 
			
		||||
	SkipCacheControlValues   interface{} // 忽略的cache-control
 | 
			
		||||
	SkipSetCookie            interface{} // 是否忽略Set-Cookie Header
 | 
			
		||||
	EnableRequestCachePragma interface{} // 是否支持客户端的Pragma: no-cache
 | 
			
		||||
	CondGroups               interface{} // 请求条件
 | 
			
		||||
	Conds                    interface{} // 请求条件
 | 
			
		||||
	CreatedAt                interface{} // 创建时间
 | 
			
		||||
	State                    interface{} // 状态
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ type HTTPFirewallPolicy struct {
 | 
			
		||||
	Name       string `field:"name"`       // 名称
 | 
			
		||||
	Inbound    string `field:"inbound"`    // 入站规则
 | 
			
		||||
	Outbound   string `field:"outbound"`   // 出站规则
 | 
			
		||||
	CondGroups string `field:"condGroups"` // 条件
 | 
			
		||||
	Conds      string `field:"conds"`      // 条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPFirewallPolicyOperator struct {
 | 
			
		||||
@@ -26,7 +26,7 @@ type HTTPFirewallPolicyOperator struct {
 | 
			
		||||
	Name       interface{} // 名称
 | 
			
		||||
	Inbound    interface{} // 入站规则
 | 
			
		||||
	Outbound   interface{} // 出站规则
 | 
			
		||||
	CondGroups interface{} // 条件
 | 
			
		||||
	Conds      interface{} // 条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHTTPFirewallPolicyOperator() *HTTPFirewallPolicyOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -107,20 +107,20 @@ func (this *HTTPGzipDAO) ComposeGzipConfig(gzipId int64) (*serverconfigs.HTTPGzi
 | 
			
		||||
	}
 | 
			
		||||
	config.Level = types.Int8(gzip.Level)
 | 
			
		||||
 | 
			
		||||
	if IsNotNull(gzip.CondGroups) {
 | 
			
		||||
		groups := []*shared.HTTPRequestCondGroup{}
 | 
			
		||||
		err = json.Unmarshal([]byte(gzip.CondGroups), &groups)
 | 
			
		||||
	if IsNotNull(gzip.Conds) {
 | 
			
		||||
		condsConfig := &shared.HTTPRequestCondsConfig{}
 | 
			
		||||
		err = json.Unmarshal([]byte(gzip.Conds), condsConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		config.CondGroups = groups
 | 
			
		||||
		config.Conds = condsConfig
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return config, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 创建Gzip
 | 
			
		||||
func (this *HTTPGzipDAO) CreateGzip(level int, minLengthJSON []byte, maxLengthJSON []byte, condGroupsJSON []byte) (int64, error) {
 | 
			
		||||
func (this *HTTPGzipDAO) CreateGzip(level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
 | 
			
		||||
	op := NewHTTPGzipOperator()
 | 
			
		||||
	op.State = HTTPGzipStateEnabled
 | 
			
		||||
	op.IsOn = true
 | 
			
		||||
@@ -131,8 +131,8 @@ func (this *HTTPGzipDAO) CreateGzip(level int, minLengthJSON []byte, maxLengthJS
 | 
			
		||||
	if len(maxLengthJSON) > 0 {
 | 
			
		||||
		op.MaxLength = JSONBytes(maxLengthJSON)
 | 
			
		||||
	}
 | 
			
		||||
	if len(condGroupsJSON) > 0 {
 | 
			
		||||
		op.CondGroups = JSONBytes(condGroupsJSON)
 | 
			
		||||
	if len(condsJSON) > 0 {
 | 
			
		||||
		op.Conds = JSONBytes(condsJSON)
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.Save(op)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -142,7 +142,7 @@ func (this *HTTPGzipDAO) CreateGzip(level int, minLengthJSON []byte, maxLengthJS
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 修改Gzip
 | 
			
		||||
func (this *HTTPGzipDAO) UpdateGzip(gzipId int64, level int, minLengthJSON []byte, maxLengthJSON []byte, condGroupsJSON []byte) error {
 | 
			
		||||
func (this *HTTPGzipDAO) UpdateGzip(gzipId int64, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) error {
 | 
			
		||||
	if gzipId <= 0 {
 | 
			
		||||
		return errors.New("invalid gzipId")
 | 
			
		||||
	}
 | 
			
		||||
@@ -155,8 +155,8 @@ func (this *HTTPGzipDAO) UpdateGzip(gzipId int64, level int, minLengthJSON []byt
 | 
			
		||||
	if len(maxLengthJSON) > 0 {
 | 
			
		||||
		op.MaxLength = JSONBytes(maxLengthJSON)
 | 
			
		||||
	}
 | 
			
		||||
	if len(condGroupsJSON) > 0 {
 | 
			
		||||
		op.CondGroups = JSONBytes(condGroupsJSON)
 | 
			
		||||
	if len(condsJSON) > 0 {
 | 
			
		||||
		op.Conds = JSONBytes(condsJSON)
 | 
			
		||||
	}
 | 
			
		||||
	_, err := this.Save(op)
 | 
			
		||||
	return err
 | 
			
		||||
 
 | 
			
		||||
@@ -2,29 +2,29 @@ package models
 | 
			
		||||
 | 
			
		||||
// Gzip配置
 | 
			
		||||
type HTTPGzip struct {
 | 
			
		||||
	Id         uint32 `field:"id"`         // ID
 | 
			
		||||
	AdminId    uint32 `field:"adminId"`    // 管理员ID
 | 
			
		||||
	UserId     uint32 `field:"userId"`     // 用户ID
 | 
			
		||||
	IsOn       uint8  `field:"isOn"`       // 是否启用
 | 
			
		||||
	Level      uint32 `field:"level"`      // 压缩级别
 | 
			
		||||
	MinLength  string `field:"minLength"`  // 可压缩最小值
 | 
			
		||||
	MaxLength  string `field:"maxLength"`  // 可压缩最大值
 | 
			
		||||
	State      uint8  `field:"state"`      // 状态
 | 
			
		||||
	CreatedAt  uint64 `field:"createdAt"`  // 创建时间
 | 
			
		||||
	CondGroups string `field:"condGroups"` // 条件
 | 
			
		||||
	Id        uint32 `field:"id"`        // ID
 | 
			
		||||
	AdminId   uint32 `field:"adminId"`   // 管理员ID
 | 
			
		||||
	UserId    uint32 `field:"userId"`    // 用户ID
 | 
			
		||||
	IsOn      uint8  `field:"isOn"`      // 是否启用
 | 
			
		||||
	Level     uint32 `field:"level"`     // 压缩级别
 | 
			
		||||
	MinLength string `field:"minLength"` // 可压缩最小值
 | 
			
		||||
	MaxLength string `field:"maxLength"` // 可压缩最大值
 | 
			
		||||
	State     uint8  `field:"state"`     // 状态
 | 
			
		||||
	CreatedAt uint64 `field:"createdAt"` // 创建时间
 | 
			
		||||
	Conds     string `field:"conds"`     // 条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPGzipOperator struct {
 | 
			
		||||
	Id         interface{} // ID
 | 
			
		||||
	AdminId    interface{} // 管理员ID
 | 
			
		||||
	UserId     interface{} // 用户ID
 | 
			
		||||
	IsOn       interface{} // 是否启用
 | 
			
		||||
	Level      interface{} // 压缩级别
 | 
			
		||||
	MinLength  interface{} // 可压缩最小值
 | 
			
		||||
	MaxLength  interface{} // 可压缩最大值
 | 
			
		||||
	State      interface{} // 状态
 | 
			
		||||
	CreatedAt  interface{} // 创建时间
 | 
			
		||||
	CondGroups interface{} // 条件
 | 
			
		||||
	Id        interface{} // ID
 | 
			
		||||
	AdminId   interface{} // 管理员ID
 | 
			
		||||
	UserId    interface{} // 用户ID
 | 
			
		||||
	IsOn      interface{} // 是否启用
 | 
			
		||||
	Level     interface{} // 压缩级别
 | 
			
		||||
	MinLength interface{} // 可压缩最小值
 | 
			
		||||
	MaxLength interface{} // 可压缩最大值
 | 
			
		||||
	State     interface{} // 状态
 | 
			
		||||
	CreatedAt interface{} // 创建时间
 | 
			
		||||
	Conds     interface{} // 条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHTTPGzipOperator() *HTTPGzipOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ type HTTPLocation struct {
 | 
			
		||||
	ReverseProxy string `field:"reverseProxy"` // 反向代理
 | 
			
		||||
	UrlPrefix    string `field:"urlPrefix"`    // URL前缀
 | 
			
		||||
	IsBreak      uint8  `field:"isBreak"`      // 是否终止匹配
 | 
			
		||||
	CondGroups   string `field:"condGroups"`   // 匹配条件
 | 
			
		||||
	Conds        string `field:"conds"`        // 匹配条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPLocationOperator struct {
 | 
			
		||||
@@ -36,7 +36,7 @@ type HTTPLocationOperator struct {
 | 
			
		||||
	ReverseProxy interface{} // 反向代理
 | 
			
		||||
	UrlPrefix    interface{} // URL前缀
 | 
			
		||||
	IsBreak      interface{} // 是否终止匹配
 | 
			
		||||
	CondGroups   interface{} // 匹配条件
 | 
			
		||||
	Conds        interface{} // 匹配条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHTTPLocationOperator() *HTTPLocationOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,7 @@ type HTTPRewriteRule struct {
 | 
			
		||||
	ProxyHost      string `field:"proxyHost"`      // 代理的主机名
 | 
			
		||||
	IsBreak        uint8  `field:"isBreak"`        // 是否终止解析
 | 
			
		||||
	WithQuery      uint8  `field:"withQuery"`      // 是否保留URI参数
 | 
			
		||||
	Conds          string `field:"conds"`          // 匹配条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPRewriteRuleOperator struct {
 | 
			
		||||
@@ -33,6 +34,7 @@ type HTTPRewriteRuleOperator struct {
 | 
			
		||||
	ProxyHost      interface{} // 代理的主机名
 | 
			
		||||
	IsBreak        interface{} // 是否终止解析
 | 
			
		||||
	WithQuery      interface{} // 是否保留URI参数
 | 
			
		||||
	Conds          interface{} // 匹配条件
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewHTTPRewriteRuleOperator() *HTTPRewriteRuleOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -31,7 +31,7 @@ func (this *HTTPAccessLogPolicyService) FindAllEnabledHTTPAccessLogPolicies(ctx
 | 
			
		||||
			IsOn:        policy.IsOn == 1,
 | 
			
		||||
			Type:        policy.Name,
 | 
			
		||||
			OptionsJSON: []byte(policy.Options),
 | 
			
		||||
			CondsJSON:   []byte(policy.CondGroups),
 | 
			
		||||
			CondsJSON:   []byte(policy.Conds),
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -42,7 +42,7 @@ func (this *HTTPGzipService) CreateHTTPGzip(ctx context.Context, req *pb.CreateH
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	gzipId, err := models.SharedHTTPGzipDAO.CreateGzip(int(req.Level), minLengthJSON, maxLengthJSON, req.CondGroupsJSON)
 | 
			
		||||
	gzipId, err := models.SharedHTTPGzipDAO.CreateGzip(int(req.Level), minLengthJSON, maxLengthJSON, req.CondsJSON)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -100,7 +100,7 @@ func (this *HTTPGzipService) UpdateHTTPGzip(ctx context.Context, req *pb.UpdateH
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedHTTPGzipDAO.UpdateGzip(req.GzipId, int(req.Level), minLengthJSON, maxLengthJSON, req.CondGroupsJSON)
 | 
			
		||||
	err = models.SharedHTTPGzipDAO.UpdateGzip(req.GzipId, int(req.Level), minLengthJSON, maxLengthJSON, req.CondsJSON)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user