实现基本的匹配条件管理

This commit is contained in:
GoEdgeLab
2020-09-29 11:28:52 +08:00
parent b6ef74d2fb
commit 3af4c6052c
7 changed files with 85 additions and 78 deletions

View File

@@ -2,7 +2,6 @@ package serverconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"regexp"
"strings"
)
@@ -13,12 +12,12 @@ var (
// gzip配置
type HTTPGzipConfig struct {
Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Level int8 `yaml:"level" json:"level"` // 1-9
MinLength *shared.SizeCapacity `yaml:"minLength" json:"minLength"` // 最小压缩对象比如4m, 24k
MaxLength *shared.SizeCapacity `yaml:"minLength" json:"maxLength"` // 最大压缩对象 TODO 需要实现
MimeTypes []string `yaml:"mimeTypes" json:"mimeTypes"` // 比如text/html, text/* // TODO 需要实现可能需要用RequestConds替代
Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Level int8 `yaml:"level" json:"level"` // 1-9
MinLength *shared.SizeCapacity `yaml:"minLength" json:"minLength"` // 最小压缩对象比如4m, 24k
MaxLength *shared.SizeCapacity `yaml:"maxLength" json:"maxLength"` // 最大压缩对象 TODO 需要实现
CondGroups []*shared.HTTPRequestCondGroup `yaml:"condGroups" json:"condGroups"` // 匹配条件
minLength int64
mimeTypes []*MimeTypeRule
@@ -30,30 +29,6 @@ func (this *HTTPGzipConfig) Init() error {
this.minLength = this.MinLength.Bytes()
}
if len(this.MimeTypes) == 0 {
this.MimeTypes = DefaultGzipMimeTypes
}
this.mimeTypes = []*MimeTypeRule{}
for _, mimeType := range this.MimeTypes {
if strings.Contains(mimeType, "*") {
mimeType = regexp.QuoteMeta(mimeType)
mimeType = strings.Replace(mimeType, "\\*", ".*", -1)
reg, err := regexp.Compile("^" + mimeType + "$")
if err != nil {
return err
}
this.mimeTypes = append(this.mimeTypes, &MimeTypeRule{
Value: mimeType,
Regexp: reg,
})
} else {
this.mimeTypes = append(this.mimeTypes, &MimeTypeRule{
Value: mimeType,
Regexp: nil,
})
}
}
return nil
}

View File

@@ -18,19 +18,15 @@ import (
// 重写条件定义
type HTTPRequestCond struct {
Id string `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Type string `yaml:"type" json:"type"` // 类型,在特殊条件时使用
// 要测试的字符串
// 其中可以使用跟请求相关的参数,比如:
// ${arg.name}, ${requestPath}
Param string `yaml:"param" json:"param"`
// 运算符
Operator RequestCondOperator `yaml:"operator" json:"operator"`
// 对比
Value string `yaml:"value" json:"value"`
Operator RequestCondOperator `yaml:"operator" json:"operator"` // 运算符
Value string `yaml:"value" json:"value"` // 对比值
isInt bool
isFloat bool

View File

@@ -2,10 +2,11 @@ package shared
// 请求条件分组
type HTTPRequestCondGroup struct {
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Connector string `yaml:"connector" json:"connector"` // 条件之间的关系
Conds []*HTTPRequestCond `yaml:"conds" json:"conds"` // 条件列表
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Connector string `yaml:"connector" json:"connector"` // 条件之间的关系
Conds []*HTTPRequestCond `yaml:"conds" json:"conds"` // 条件列表
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配
Description string `yaml:"description" json:"description"` // 说明
}
// 初始化