实现gzip

This commit is contained in:
GoEdgeLab
2020-09-29 17:22:33 +08:00
parent b2fa7c01ba
commit add9c74761
10 changed files with 47 additions and 45 deletions

View File

@@ -118,13 +118,13 @@ func (this *HTTPAccessLogPolicyDAO) ComposeAccessLogPolicyConfig(policyId int64)
} }
// 条件 // 条件
if IsNotNull(policy.CondGroups) { if IsNotNull(policy.Conds) {
condGroups := []*shared.HTTPRequestCondGroup{} condsConfig := &shared.HTTPRequestCondsConfig{}
err = json.Unmarshal([]byte(policy.CondGroups), &condGroups) err = json.Unmarshal([]byte(policy.Conds), condsConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
config.CondGroups = condGroups config.Conds = condsConfig
} }
return config, nil return config, nil

View File

@@ -12,7 +12,7 @@ type HTTPAccessLogPolicy struct {
IsOn uint8 `field:"isOn"` // 是否启用 IsOn uint8 `field:"isOn"` // 是否启用
Type string `field:"type"` // 存储类型 Type string `field:"type"` // 存储类型
Options string `field:"options"` // 存储选项 Options string `field:"options"` // 存储选项
CondGroups string `field:"condGroups"` // 请求条件 Conds string `field:"conds"` // 请求条件
} }
type HTTPAccessLogPolicyOperator struct { type HTTPAccessLogPolicyOperator struct {
@@ -26,7 +26,7 @@ type HTTPAccessLogPolicyOperator struct {
IsOn interface{} // 是否启用 IsOn interface{} // 是否启用
Type interface{} // 存储类型 Type interface{} // 存储类型
Options interface{} // 存储选项 Options interface{} // 存储选项
CondGroups interface{} // 请求条件 Conds interface{} // 请求条件
} }
func NewHTTPAccessLogPolicyOperator() *HTTPAccessLogPolicyOperator { func NewHTTPAccessLogPolicyOperator() *HTTPAccessLogPolicyOperator {

View File

@@ -16,7 +16,7 @@ type HTTPCachePolicy struct {
SkipCacheControlValues string `field:"skipCacheControlValues"` // 忽略的cache-control SkipCacheControlValues string `field:"skipCacheControlValues"` // 忽略的cache-control
SkipSetCookie uint8 `field:"skipSetCookie"` // 是否忽略Set-Cookie Header SkipSetCookie uint8 `field:"skipSetCookie"` // 是否忽略Set-Cookie Header
EnableRequestCachePragma uint8 `field:"enableRequestCachePragma"` // 是否支持客户端的Pragma: no-cache EnableRequestCachePragma uint8 `field:"enableRequestCachePragma"` // 是否支持客户端的Pragma: no-cache
CondGroups string `field:"condGroups"` // 请求条件 Conds string `field:"conds"` // 请求条件
CreatedAt uint64 `field:"createdAt"` // 创建时间 CreatedAt uint64 `field:"createdAt"` // 创建时间
State uint8 `field:"state"` // 状态 State uint8 `field:"state"` // 状态
} }
@@ -36,7 +36,7 @@ type HTTPCachePolicyOperator struct {
SkipCacheControlValues interface{} // 忽略的cache-control SkipCacheControlValues interface{} // 忽略的cache-control
SkipSetCookie interface{} // 是否忽略Set-Cookie Header SkipSetCookie interface{} // 是否忽略Set-Cookie Header
EnableRequestCachePragma interface{} // 是否支持客户端的Pragma: no-cache EnableRequestCachePragma interface{} // 是否支持客户端的Pragma: no-cache
CondGroups interface{} // 请求条件 Conds interface{} // 请求条件
CreatedAt interface{} // 创建时间 CreatedAt interface{} // 创建时间
State interface{} // 状态 State interface{} // 状态
} }

View File

@@ -12,7 +12,7 @@ type HTTPFirewallPolicy struct {
Name string `field:"name"` // 名称 Name string `field:"name"` // 名称
Inbound string `field:"inbound"` // 入站规则 Inbound string `field:"inbound"` // 入站规则
Outbound string `field:"outbound"` // 出站规则 Outbound string `field:"outbound"` // 出站规则
CondGroups string `field:"condGroups"` // 条件 Conds string `field:"conds"` // 条件
} }
type HTTPFirewallPolicyOperator struct { type HTTPFirewallPolicyOperator struct {
@@ -26,7 +26,7 @@ type HTTPFirewallPolicyOperator struct {
Name interface{} // 名称 Name interface{} // 名称
Inbound interface{} // 入站规则 Inbound interface{} // 入站规则
Outbound interface{} // 出站规则 Outbound interface{} // 出站规则
CondGroups interface{} // 条件 Conds interface{} // 条件
} }
func NewHTTPFirewallPolicyOperator() *HTTPFirewallPolicyOperator { func NewHTTPFirewallPolicyOperator() *HTTPFirewallPolicyOperator {

View File

@@ -107,20 +107,20 @@ func (this *HTTPGzipDAO) ComposeGzipConfig(gzipId int64) (*serverconfigs.HTTPGzi
} }
config.Level = types.Int8(gzip.Level) config.Level = types.Int8(gzip.Level)
if IsNotNull(gzip.CondGroups) { if IsNotNull(gzip.Conds) {
groups := []*shared.HTTPRequestCondGroup{} condsConfig := &shared.HTTPRequestCondsConfig{}
err = json.Unmarshal([]byte(gzip.CondGroups), &groups) err = json.Unmarshal([]byte(gzip.Conds), condsConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
config.CondGroups = groups config.Conds = condsConfig
} }
return config, nil return config, nil
} }
// 创建Gzip // 创建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 := NewHTTPGzipOperator()
op.State = HTTPGzipStateEnabled op.State = HTTPGzipStateEnabled
op.IsOn = true op.IsOn = true
@@ -131,8 +131,8 @@ func (this *HTTPGzipDAO) CreateGzip(level int, minLengthJSON []byte, maxLengthJS
if len(maxLengthJSON) > 0 { if len(maxLengthJSON) > 0 {
op.MaxLength = JSONBytes(maxLengthJSON) op.MaxLength = JSONBytes(maxLengthJSON)
} }
if len(condGroupsJSON) > 0 { if len(condsJSON) > 0 {
op.CondGroups = JSONBytes(condGroupsJSON) op.Conds = JSONBytes(condsJSON)
} }
_, err := this.Save(op) _, err := this.Save(op)
if err != nil { if err != nil {
@@ -142,7 +142,7 @@ func (this *HTTPGzipDAO) CreateGzip(level int, minLengthJSON []byte, maxLengthJS
} }
// 修改Gzip // 修改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 { if gzipId <= 0 {
return errors.New("invalid gzipId") return errors.New("invalid gzipId")
} }
@@ -155,8 +155,8 @@ func (this *HTTPGzipDAO) UpdateGzip(gzipId int64, level int, minLengthJSON []byt
if len(maxLengthJSON) > 0 { if len(maxLengthJSON) > 0 {
op.MaxLength = JSONBytes(maxLengthJSON) op.MaxLength = JSONBytes(maxLengthJSON)
} }
if len(condGroupsJSON) > 0 { if len(condsJSON) > 0 {
op.CondGroups = JSONBytes(condGroupsJSON) op.Conds = JSONBytes(condsJSON)
} }
_, err := this.Save(op) _, err := this.Save(op)
return err return err

View File

@@ -2,29 +2,29 @@ package models
// Gzip配置 // Gzip配置
type HTTPGzip struct { type HTTPGzip struct {
Id uint32 `field:"id"` // ID Id uint32 `field:"id"` // ID
AdminId uint32 `field:"adminId"` // 管理员ID AdminId uint32 `field:"adminId"` // 管理员ID
UserId uint32 `field:"userId"` // 用户ID UserId uint32 `field:"userId"` // 用户ID
IsOn uint8 `field:"isOn"` // 是否启用 IsOn uint8 `field:"isOn"` // 是否启用
Level uint32 `field:"level"` // 压缩级别 Level uint32 `field:"level"` // 压缩级别
MinLength string `field:"minLength"` // 可压缩最小值 MinLength string `field:"minLength"` // 可压缩最小值
MaxLength string `field:"maxLength"` // 可压缩最大值 MaxLength string `field:"maxLength"` // 可压缩最大值
State uint8 `field:"state"` // 状态 State uint8 `field:"state"` // 状态
CreatedAt uint64 `field:"createdAt"` // 创建时间 CreatedAt uint64 `field:"createdAt"` // 创建时间
CondGroups string `field:"condGroups"` // 条件 Conds string `field:"conds"` // 条件
} }
type HTTPGzipOperator struct { type HTTPGzipOperator struct {
Id interface{} // ID Id interface{} // ID
AdminId interface{} // 管理员ID AdminId interface{} // 管理员ID
UserId interface{} // 用户ID UserId interface{} // 用户ID
IsOn interface{} // 是否启用 IsOn interface{} // 是否启用
Level interface{} // 压缩级别 Level interface{} // 压缩级别
MinLength interface{} // 可压缩最小值 MinLength interface{} // 可压缩最小值
MaxLength interface{} // 可压缩最大值 MaxLength interface{} // 可压缩最大值
State interface{} // 状态 State interface{} // 状态
CreatedAt interface{} // 创建时间 CreatedAt interface{} // 创建时间
CondGroups interface{} // 条件 Conds interface{} // 条件
} }
func NewHTTPGzipOperator() *HTTPGzipOperator { func NewHTTPGzipOperator() *HTTPGzipOperator {

View File

@@ -17,7 +17,7 @@ type HTTPLocation struct {
ReverseProxy string `field:"reverseProxy"` // 反向代理 ReverseProxy string `field:"reverseProxy"` // 反向代理
UrlPrefix string `field:"urlPrefix"` // URL前缀 UrlPrefix string `field:"urlPrefix"` // URL前缀
IsBreak uint8 `field:"isBreak"` // 是否终止匹配 IsBreak uint8 `field:"isBreak"` // 是否终止匹配
CondGroups string `field:"condGroups"` // 匹配条件 Conds string `field:"conds"` // 匹配条件
} }
type HTTPLocationOperator struct { type HTTPLocationOperator struct {
@@ -36,7 +36,7 @@ type HTTPLocationOperator struct {
ReverseProxy interface{} // 反向代理 ReverseProxy interface{} // 反向代理
UrlPrefix interface{} // URL前缀 UrlPrefix interface{} // URL前缀
IsBreak interface{} // 是否终止匹配 IsBreak interface{} // 是否终止匹配
CondGroups interface{} // 匹配条件 Conds interface{} // 匹配条件
} }
func NewHTTPLocationOperator() *HTTPLocationOperator { func NewHTTPLocationOperator() *HTTPLocationOperator {

View File

@@ -16,6 +16,7 @@ type HTTPRewriteRule struct {
ProxyHost string `field:"proxyHost"` // 代理的主机名 ProxyHost string `field:"proxyHost"` // 代理的主机名
IsBreak uint8 `field:"isBreak"` // 是否终止解析 IsBreak uint8 `field:"isBreak"` // 是否终止解析
WithQuery uint8 `field:"withQuery"` // 是否保留URI参数 WithQuery uint8 `field:"withQuery"` // 是否保留URI参数
Conds string `field:"conds"` // 匹配条件
} }
type HTTPRewriteRuleOperator struct { type HTTPRewriteRuleOperator struct {
@@ -33,6 +34,7 @@ type HTTPRewriteRuleOperator struct {
ProxyHost interface{} // 代理的主机名 ProxyHost interface{} // 代理的主机名
IsBreak interface{} // 是否终止解析 IsBreak interface{} // 是否终止解析
WithQuery interface{} // 是否保留URI参数 WithQuery interface{} // 是否保留URI参数
Conds interface{} // 匹配条件
} }
func NewHTTPRewriteRuleOperator() *HTTPRewriteRuleOperator { func NewHTTPRewriteRuleOperator() *HTTPRewriteRuleOperator {

View File

@@ -31,7 +31,7 @@ func (this *HTTPAccessLogPolicyService) FindAllEnabledHTTPAccessLogPolicies(ctx
IsOn: policy.IsOn == 1, IsOn: policy.IsOn == 1,
Type: policy.Name, Type: policy.Name,
OptionsJSON: []byte(policy.Options), OptionsJSON: []byte(policy.Options),
CondsJSON: []byte(policy.CondGroups), CondsJSON: []byte(policy.Conds),
}) })
} }

View File

@@ -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 { if err != nil {
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }