diff --git a/pkg/serverconfigs/http_cache_cond.go b/pkg/serverconfigs/http_cache_cond.go deleted file mode 100644 index 6378779..0000000 --- a/pkg/serverconfigs/http_cache_cond.go +++ /dev/null @@ -1,67 +0,0 @@ -package serverconfigs - -import ( - "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" - "github.com/iwind/TeaGo/lists" - "strings" - "time" -) - -var DefaultSkippedResponseCacheControlValues = []string{"private", "no-cache", "no-store"} - -type HTTPCacheCond struct { - Key string `yaml:"key" json:"key"` // 每个缓存的Key规则,里面可以有变量 - Life *shared.TimeDuration `yaml:"life" json:"life"` // 时间 - Status []int `yaml:"status" json:"status"` // 缓存的状态码列表 - MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 能够请求的最大尺寸 - - SkipResponseCacheControlValues []string `yaml:"skipCacheControlValues" json:"skipCacheControlValues"` // 可以跳过的响应的Cache-Control值 - SkipResponseSetCookie bool `yaml:"skipSetCookie" json:"skipSetCookie"` // 是否跳过响应的Set-Cookie Header - EnableRequestCachePragma bool `yaml:"enableRequestCachePragma" json:"enableRequestCachePragma"` // 是否支持客户端的Pragma: no-cache - - Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 请求条件 - - life time.Duration - maxSize int64 - uppercaseSkipCacheControlValues []string -} - -func (this *HTTPCacheCond) Init() error { - if this.MaxSize != nil { - this.maxSize = this.MaxSize.Bytes() - } - if this.Life != nil { - this.life = this.Life.Duration() - } - - // control-values - this.uppercaseSkipCacheControlValues = []string{} - for _, value := range this.SkipResponseCacheControlValues { - this.uppercaseSkipCacheControlValues = append(this.uppercaseSkipCacheControlValues, strings.ToUpper(value)) - } - - // conds - if this.Conds != nil { - err := this.Conds.Init() - if err != nil { - return err - } - } - - return nil -} - -// 最大数据尺寸 -func (this *HTTPCacheCond) MaxDataSize() int64 { - return this.maxSize -} - -// 生命周期 -func (this *HTTPCacheCond) LifeDuration() time.Duration { - return this.life -} - -// 是否包含某个Cache-Control值 -func (this *HTTPCacheCond) ContainsCacheControl(value string) bool { - return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value)) -} diff --git a/pkg/serverconfigs/http_cache_config.go b/pkg/serverconfigs/http_cache_config.go new file mode 100644 index 0000000..a3398f7 --- /dev/null +++ b/pkg/serverconfigs/http_cache_config.go @@ -0,0 +1,18 @@ +package serverconfigs + +type HTTPCacheConfig struct { + IsPrior bool `yaml:"isPrior" json:"isPrior"` + IsOn bool `yaml:"isOn" json:"isOn"` + + CacheRefs []*HTTPCacheRef `yaml:"cacheRefs" json:"cacheRefs"` // 缓存配置 +} + +func (this *HTTPCacheConfig) Init() error { + for _, cacheRef := range this.CacheRefs { + err := cacheRef.Init() + if err != nil { + return err + } + } + return nil +} diff --git a/pkg/serverconfigs/http_cache_config_file.go b/pkg/serverconfigs/http_cache_config_file.go deleted file mode 100644 index 119359c..0000000 --- a/pkg/serverconfigs/http_cache_config_file.go +++ /dev/null @@ -1,11 +0,0 @@ -package serverconfigs - -type CachePolicyType = string - -type HTTPFileCacheConfig struct { - Dir string `yaml:"dir" json:"dir"` // 目录 -} - -func (this *HTTPFileCacheConfig) Init() error { - return nil -} diff --git a/pkg/serverconfigs/http_cache_config_memory.go b/pkg/serverconfigs/http_cache_config_memory.go deleted file mode 100644 index cd837ac..0000000 --- a/pkg/serverconfigs/http_cache_config_memory.go +++ /dev/null @@ -1,8 +0,0 @@ -package serverconfigs - -type HTTPMemoryCacheConfig struct { -} - -func (this *HTTPMemoryCacheConfig) Init() error { - return nil -} diff --git a/pkg/serverconfigs/http_cache_policy.go b/pkg/serverconfigs/http_cache_policy.go index 71a8f16..03e81f5 100644 --- a/pkg/serverconfigs/http_cache_policy.go +++ b/pkg/serverconfigs/http_cache_policy.go @@ -15,7 +15,7 @@ type HTTPCachePolicy struct { Capacity *shared.SizeCapacity `yaml:"capacity" json:"capacity"` // 最大内容容量 TODO 需要实现 MaxKeys int64 `yaml:"maxKeys" json:"maxKeys"` // 最多Key值 TODO 需要实现 MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 单个缓存最大尺寸 TODO 需要实现 - Type CachePolicyType `yaml:"type" json:"type"` // 类型 + Type CachePolicyStorageType `yaml:"type" json:"type"` // 类型 Options map[string]interface{} `yaml:"options" json:"options"` // 选项 capacity int64 diff --git a/pkg/serverconfigs/http_cache_policy_types.go b/pkg/serverconfigs/http_cache_policy_types.go index 10f6bac..6b005d3 100644 --- a/pkg/serverconfigs/http_cache_policy_types.go +++ b/pkg/serverconfigs/http_cache_policy_types.go @@ -2,25 +2,27 @@ package serverconfigs import "github.com/iwind/TeaGo/maps" +type CachePolicyStorageType = string + const ( - CachePolicyTypeFile CachePolicyType = "file" - CachePolicyTypeMemory CachePolicyType = "memory" + CachePolicyStorageFile CachePolicyStorageType = "file" + CachePolicyStorageMemory CachePolicyStorageType = "memory" ) -var AllCachePolicyTypes = []maps.Map{ +var AllCachePolicyStorageTypes = []maps.Map{ { "name": "文件缓存", - "type": CachePolicyTypeFile, + "type": CachePolicyStorageFile, }, { "name": "内存缓存", - "type": CachePolicyTypeMemory, + "type": CachePolicyStorageMemory, }, } // 根据类型查找名称 -func FindCachePolicyTypeName(policyType CachePolicyType) string { - for _, t := range AllCachePolicyTypes { +func FindCachePolicyStorageName(policyType CachePolicyStorageType) string { + for _, t := range AllCachePolicyStorageTypes { if t.GetString("type") == policyType { return t.GetString("name") } diff --git a/pkg/serverconfigs/http_cache_ref.go b/pkg/serverconfigs/http_cache_ref.go index ac712c5..a34b4c4 100644 --- a/pkg/serverconfigs/http_cache_ref.go +++ b/pkg/serverconfigs/http_cache_ref.go @@ -1,18 +1,80 @@ package serverconfigs +import ( + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" + "github.com/iwind/TeaGo/lists" + "strings" + "time" +) + +var DefaultSkippedResponseCacheControlValues = []string{"private", "no-cache", "no-store"} + type HTTPCacheRef struct { - IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否覆盖 - IsOn bool `yaml:"isOn" json:"isOn"` // 是否开启 - CachePolicyId int64 `yaml:"cachePolicyId" json:"cachePolicyId"` // 缓存策略ID - Cond *HTTPCacheCond `yaml:"cond" json:"cond"` // 条件 + IsOn bool `yaml:"isOn" json:"isOn"` + CachePolicyId int64 `yaml:"cachePolicyId" json:"cachePolicyId"` + + Key string `yaml:"key" json:"key"` // 每个缓存的Key规则,里面可以有变量 + Life *shared.TimeDuration `yaml:"life" json:"life"` // 时间 + Status []int `yaml:"status" json:"status"` // 缓存的状态码列表 + MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 能够请求的最大尺寸 + + SkipResponseCacheControlValues []string `yaml:"skipCacheControlValues" json:"skipCacheControlValues"` // 可以跳过的响应的Cache-Control值 + SkipResponseSetCookie bool `yaml:"skipSetCookie" json:"skipSetCookie"` // 是否跳过响应的Set-Cookie Header + EnableRequestCachePragma bool `yaml:"enableRequestCachePragma" json:"enableRequestCachePragma"` // 是否支持客户端的Pragma: no-cache + + Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 请求条件 + + CachePolicy *HTTPCachePolicy `yaml:"cachePolicy" json:"cachePolicy"` + + life time.Duration + maxSize int64 + uppercaseSkipCacheControlValues []string } func (this *HTTPCacheRef) Init() error { - if this.Cond != nil { - err := this.Cond.Init() + if this.MaxSize != nil { + this.maxSize = this.MaxSize.Bytes() + } + if this.Life != nil { + this.life = this.Life.Duration() + } + + // control-values + this.uppercaseSkipCacheControlValues = []string{} + for _, value := range this.SkipResponseCacheControlValues { + this.uppercaseSkipCacheControlValues = append(this.uppercaseSkipCacheControlValues, strings.ToUpper(value)) + } + + // conds + if this.Conds != nil { + err := this.Conds.Init() if err != nil { return err } } + + // policy + if this.CachePolicy != nil { + err := this.CachePolicy.Init() + if err != nil { + return err + } + } + return nil } + +// 最大数据尺寸 +func (this *HTTPCacheRef) MaxDataSize() int64 { + return this.maxSize +} + +// 生命周期 +func (this *HTTPCacheRef) LifeDuration() time.Duration { + return this.life +} + +// 是否包含某个Cache-Control值 +func (this *HTTPCacheRef) ContainsCacheControl(value string) bool { + return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value)) +} diff --git a/pkg/serverconfigs/http_cache_storage_file.go b/pkg/serverconfigs/http_cache_storage_file.go new file mode 100644 index 0000000..f16958c --- /dev/null +++ b/pkg/serverconfigs/http_cache_storage_file.go @@ -0,0 +1,9 @@ +package serverconfigs + +type HTTPFileCacheStorage struct { + Dir string `yaml:"dir" json:"dir"` // 目录 +} + +func (this *HTTPFileCacheStorage) Init() error { + return nil +} diff --git a/pkg/serverconfigs/http_cache_storage_memory.go b/pkg/serverconfigs/http_cache_storage_memory.go new file mode 100644 index 0000000..309ce78 --- /dev/null +++ b/pkg/serverconfigs/http_cache_storage_memory.go @@ -0,0 +1,8 @@ +package serverconfigs + +type HTTPMemoryCacheStorage struct { +} + +func (this *HTTPMemoryCacheStorage) Init() error { + return nil +} diff --git a/pkg/serverconfigs/http_web_config.go b/pkg/serverconfigs/http_web_config.go index 5e6b955..bda1940 100644 --- a/pkg/serverconfigs/http_web_config.go +++ b/pkg/serverconfigs/http_web_config.go @@ -17,13 +17,12 @@ type HTTPWebConfig struct { MaxRequestBodySize string `yaml:"maxRequestBodySize" json:"maxRequestBodySize"` // 请求body最大尺寸 TODO 需要实现 AccessLogRef *HTTPAccessLogRef `yaml:"accessLog" json:"accessLog"` // 访问日志配置 StatRef *HTTPStatRef `yaml:"statRef" json:"statRef"` // 统计配置 - CacheRefs []*HTTPCacheRef `yaml:"cacheRefs" json:"cacheRefs"` // 缓存配置 - CachePolicies []*HTTPCachePolicy `yaml:"cachePolicies" json:"cachePolicies"` // 缓存策略 - FirewallRef *HTTPFirewallRef `yaml:"firewallRef" json:"firewallRef"` // 防火墙设置 - WebsocketRef *HTTPWebsocketRef `yaml:"websocketRef" json:"websocketRef"` // Websocket应用配置 - Websocket *HTTPWebsocketConfig `yaml:"websocket" json:"websocket"` // Websocket配置 - RewriteRefs []*HTTPRewriteRef `yaml:"rewriteRefs" json:"rewriteRefs"` // 重写规则配置 - RewriteRules []*HTTPRewriteRule `yaml:"rewriteRules" json:"rewriteRules"` // 重写规则 + Cache *HTTPCacheConfig `yaml:"cache" json:"cache"` + FirewallRef *HTTPFirewallRef `yaml:"firewallRef" json:"firewallRef"` // 防火墙设置 + WebsocketRef *HTTPWebsocketRef `yaml:"websocketRef" json:"websocketRef"` // Websocket应用配置 + Websocket *HTTPWebsocketConfig `yaml:"websocket" json:"websocket"` // Websocket配置 + RewriteRefs []*HTTPRewriteRef `yaml:"rewriteRefs" json:"rewriteRefs"` // 重写规则配置 + RewriteRules []*HTTPRewriteRule `yaml:"rewriteRules" json:"rewriteRules"` // 重写规则 RequestHeaderPolicyRef *shared.HTTPHeaderPolicyRef `yaml:"requestHeaderPolicyRef" json:"requestHeaderPolicyRef"` // 请求Header RequestHeaderPolicy *shared.HTTPHeaderPolicy `yaml:"requestHeaderPolicy" json:"requestHeaderPolicy"` // 请求Header策略 @@ -112,14 +111,8 @@ func (this *HTTPWebConfig) Init() error { } // cache - for _, cacheRef := range this.CacheRefs { - err := cacheRef.Init() - if err != nil { - return err - } - } - for _, cachePolicy := range this.CachePolicies { - err := cachePolicy.Init() + if this.Cache != nil { + err := this.Cache.Init() if err != nil { return err }