mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 23:20:26 +08:00
支持brotli和deflate压缩
This commit is contained in:
8
go.sum
8
go.sum
@@ -182,14 +182,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
|
|||||||
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
|
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df/go.mod h1:QMZY7/J/KSQEhKWFeDesPjMj+wCHReeknARU3wqlyN4=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210411134150-ddf57e240c2f/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210628135026-38575a4ab060/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210806054428-5534da0db9d1 h1:AZKkwTNEZYrpyv62zIkxpLJsWhfOS7OEFovAcwd0aco=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210806054428-5534da0db9d1/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210809112119-a57ed0e84e34 h1:ZCNQXLiGF5Z1cV3Pi03zCWzwwjPfsI5XhcrNhTvCFIU=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210809112119-a57ed0e84e34/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210824034952-1a56ad7d0b5e h1:GDCU57lQD6W9u5KT2834MmK022FSeAbskb7H0p2eaJY=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210824034952-1a56ad7d0b5e/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210829020150-9c36d31301a5 h1:ybjIXGT3E/ZbfkRhIb903WMfLyt2Uv5p4niAqi8jwvM=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210829020150-9c36d31301a5/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13 h1:HuEJ5xJfujW1Q6rNDhOu5LQXEBB2qLPah3jYslT8Gz4=
|
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13 h1:HuEJ5xJfujW1Q6rNDhOu5LQXEBB2qLPah3jYslT8Gz4=
|
||||||
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
github.com/iwind/TeaGo v0.0.0-20210831140440-a2a442471b13/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc=
|
||||||
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c=
|
||||||
|
|||||||
170
internal/db/models/http_brotli_policy_dao.go
Normal file
170
internal/db/models/http_brotli_policy_dao.go
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTPBrotliPolicyStateEnabled = 1 // 已启用
|
||||||
|
HTTPBrotliPolicyStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPBrotliPolicyDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewHTTPBrotliPolicyDAO() *HTTPBrotliPolicyDAO {
|
||||||
|
return dbs.NewDAO(&HTTPBrotliPolicyDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeHTTPBrotliPolicies",
|
||||||
|
Model: new(HTTPBrotliPolicy),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*HTTPBrotliPolicyDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedHTTPBrotliPolicyDAO *HTTPBrotliPolicyDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedHTTPBrotliPolicyDAO = NewHTTPBrotliPolicyDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableHTTPBrotliPolicy 启用条目
|
||||||
|
func (this *HTTPBrotliPolicyDAO) EnableHTTPBrotliPolicy(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", HTTPBrotliPolicyStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableHTTPBrotliPolicy 禁用条目
|
||||||
|
func (this *HTTPBrotliPolicyDAO) DisableHTTPBrotliPolicy(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", HTTPBrotliPolicyStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledHTTPBrotliPolicy 查找启用中的条目
|
||||||
|
func (this *HTTPBrotliPolicyDAO) FindEnabledHTTPBrotliPolicy(tx *dbs.Tx, id int64) (*HTTPBrotliPolicy, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", HTTPBrotliPolicyStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*HTTPBrotliPolicy), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ComposeBrotliConfig 组合配置
|
||||||
|
func (this *HTTPBrotliPolicyDAO) ComposeBrotliConfig(tx *dbs.Tx, policyId int64) (*serverconfigs.HTTPBrotliCompressionConfig, error) {
|
||||||
|
policy, err := this.FindEnabledHTTPBrotliPolicy(tx, policyId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if policy == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
config := &serverconfigs.HTTPBrotliCompressionConfig{}
|
||||||
|
config.Id = int64(policy.Id)
|
||||||
|
config.IsOn = policy.IsOn == 1
|
||||||
|
if IsNotNull(policy.MinLength) {
|
||||||
|
minLengthConfig := &shared.SizeCapacity{}
|
||||||
|
err = json.Unmarshal([]byte(policy.MinLength), minLengthConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.MinLength = minLengthConfig
|
||||||
|
}
|
||||||
|
if IsNotNull(policy.MaxLength) {
|
||||||
|
maxLengthConfig := &shared.SizeCapacity{}
|
||||||
|
err = json.Unmarshal([]byte(policy.MaxLength), maxLengthConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.MaxLength = maxLengthConfig
|
||||||
|
}
|
||||||
|
config.Level = types.Int8(policy.Level)
|
||||||
|
|
||||||
|
if IsNotNull(policy.Conds) {
|
||||||
|
condsConfig := &shared.HTTPRequestCondsConfig{}
|
||||||
|
err = json.Unmarshal([]byte(policy.Conds), condsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Conds = condsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreatePolicy 创建策略
|
||||||
|
func (this *HTTPBrotliPolicyDAO) CreatePolicy(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
||||||
|
op := NewHTTPBrotliPolicyOperator()
|
||||||
|
op.State = HTTPBrotliPolicyStateEnabled
|
||||||
|
op.IsOn = true
|
||||||
|
op.Level = level
|
||||||
|
if len(minLengthJSON) > 0 {
|
||||||
|
op.MinLength = JSONBytes(minLengthJSON)
|
||||||
|
}
|
||||||
|
if len(maxLengthJSON) > 0 {
|
||||||
|
op.MaxLength = JSONBytes(maxLengthJSON)
|
||||||
|
}
|
||||||
|
if len(condsJSON) > 0 {
|
||||||
|
op.Conds = JSONBytes(condsJSON)
|
||||||
|
}
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return types.Int64(op.Id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatePolicy 修改Policy
|
||||||
|
func (this *HTTPBrotliPolicyDAO) UpdatePolicy(tx *dbs.Tx, policyId int64, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) error {
|
||||||
|
if policyId <= 0 {
|
||||||
|
return errors.New("invalid policyId")
|
||||||
|
}
|
||||||
|
op := NewHTTPBrotliPolicyOperator()
|
||||||
|
op.Id = policyId
|
||||||
|
op.Level = level
|
||||||
|
if len(minLengthJSON) > 0 {
|
||||||
|
op.MinLength = JSONBytes(minLengthJSON)
|
||||||
|
}
|
||||||
|
if len(maxLengthJSON) > 0 {
|
||||||
|
op.MaxLength = JSONBytes(maxLengthJSON)
|
||||||
|
}
|
||||||
|
if len(condsJSON) > 0 {
|
||||||
|
op.Conds = JSONBytes(condsJSON)
|
||||||
|
}
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, policyId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更新
|
||||||
|
func (this *HTTPBrotliPolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
||||||
|
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithBrotliPolicyId(tx, policyId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if webId > 0 {
|
||||||
|
return SharedHTTPWebDAO.NotifyUpdate(tx, webId)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
6
internal/db/models/http_brotli_policy_dao_test.go
Normal file
6
internal/db/models/http_brotli_policy_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
32
internal/db/models/http_brotli_policy_model.go
Normal file
32
internal/db/models/http_brotli_policy_model.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
// HTTPBrotliPolicy Gzip配置
|
||||||
|
type HTTPBrotliPolicy 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"` // 创建时间
|
||||||
|
Conds string `field:"conds"` // 条件
|
||||||
|
}
|
||||||
|
|
||||||
|
type HTTPBrotliPolicyOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
AdminId interface{} // 管理员ID
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Level interface{} // 压缩级别
|
||||||
|
MinLength interface{} // 可压缩最小值
|
||||||
|
MaxLength interface{} // 可压缩最大值
|
||||||
|
State interface{} // 状态
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
Conds interface{} // 条件
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPBrotliPolicyOperator() *HTTPBrotliPolicyOperator {
|
||||||
|
return &HTTPBrotliPolicyOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/http_brotli_policy_model_ext.go
Normal file
1
internal/db/models/http_brotli_policy_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
170
internal/db/models/http_deflate_policy_dao.go
Normal file
170
internal/db/models/http_deflate_policy_dao.go
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/iwind/TeaGo/Tea"
|
||||||
|
"github.com/iwind/TeaGo/dbs"
|
||||||
|
"github.com/iwind/TeaGo/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTPDeflatePolicyStateEnabled = 1 // 已启用
|
||||||
|
HTTPDeflatePolicyStateDisabled = 0 // 已禁用
|
||||||
|
)
|
||||||
|
|
||||||
|
type HTTPDeflatePolicyDAO dbs.DAO
|
||||||
|
|
||||||
|
func NewHTTPDeflatePolicyDAO() *HTTPDeflatePolicyDAO {
|
||||||
|
return dbs.NewDAO(&HTTPDeflatePolicyDAO{
|
||||||
|
DAOObject: dbs.DAOObject{
|
||||||
|
DB: Tea.Env,
|
||||||
|
Table: "edgeHTTPDeflatePolicies",
|
||||||
|
Model: new(HTTPDeflatePolicy),
|
||||||
|
PkName: "id",
|
||||||
|
},
|
||||||
|
}).(*HTTPDeflatePolicyDAO)
|
||||||
|
}
|
||||||
|
|
||||||
|
var SharedHTTPDeflatePolicyDAO *HTTPDeflatePolicyDAO
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
dbs.OnReady(func() {
|
||||||
|
SharedHTTPDeflatePolicyDAO = NewHTTPDeflatePolicyDAO()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableHTTPDeflatePolicy 启用条目
|
||||||
|
func (this *HTTPDeflatePolicyDAO) EnableHTTPDeflatePolicy(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", HTTPDeflatePolicyStateEnabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableHTTPDeflatePolicy 禁用条目
|
||||||
|
func (this *HTTPDeflatePolicyDAO) DisableHTTPDeflatePolicy(tx *dbs.Tx, id int64) error {
|
||||||
|
_, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Set("state", HTTPDeflatePolicyStateDisabled).
|
||||||
|
Update()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledHTTPDeflatePolicy 查找启用中的条目
|
||||||
|
func (this *HTTPDeflatePolicyDAO) FindEnabledHTTPDeflatePolicy(tx *dbs.Tx, id int64) (*HTTPDeflatePolicy, error) {
|
||||||
|
result, err := this.Query(tx).
|
||||||
|
Pk(id).
|
||||||
|
Attr("state", HTTPDeflatePolicyStateEnabled).
|
||||||
|
Find()
|
||||||
|
if result == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result.(*HTTPDeflatePolicy), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ComposeDeflateConfig 组合配置
|
||||||
|
func (this *HTTPDeflatePolicyDAO) ComposeDeflateConfig(tx *dbs.Tx, policyId int64) (*serverconfigs.HTTPDeflateCompressionConfig, error) {
|
||||||
|
policy, err := this.FindEnabledHTTPDeflatePolicy(tx, policyId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if policy == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
config := &serverconfigs.HTTPDeflateCompressionConfig{}
|
||||||
|
config.Id = int64(policy.Id)
|
||||||
|
config.IsOn = policy.IsOn == 1
|
||||||
|
if IsNotNull(policy.MinLength) {
|
||||||
|
minLengthConfig := &shared.SizeCapacity{}
|
||||||
|
err = json.Unmarshal([]byte(policy.MinLength), minLengthConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.MinLength = minLengthConfig
|
||||||
|
}
|
||||||
|
if IsNotNull(policy.MaxLength) {
|
||||||
|
maxLengthConfig := &shared.SizeCapacity{}
|
||||||
|
err = json.Unmarshal([]byte(policy.MaxLength), maxLengthConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.MaxLength = maxLengthConfig
|
||||||
|
}
|
||||||
|
config.Level = types.Int8(policy.Level)
|
||||||
|
|
||||||
|
if IsNotNull(policy.Conds) {
|
||||||
|
condsConfig := &shared.HTTPRequestCondsConfig{}
|
||||||
|
err = json.Unmarshal([]byte(policy.Conds), condsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Conds = condsConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreatePolicy 创建策略
|
||||||
|
func (this *HTTPDeflatePolicyDAO) CreatePolicy(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
||||||
|
op := NewHTTPDeflatePolicyOperator()
|
||||||
|
op.State = HTTPDeflatePolicyStateEnabled
|
||||||
|
op.IsOn = true
|
||||||
|
op.Level = level
|
||||||
|
if len(minLengthJSON) > 0 {
|
||||||
|
op.MinLength = JSONBytes(minLengthJSON)
|
||||||
|
}
|
||||||
|
if len(maxLengthJSON) > 0 {
|
||||||
|
op.MaxLength = JSONBytes(maxLengthJSON)
|
||||||
|
}
|
||||||
|
if len(condsJSON) > 0 {
|
||||||
|
op.Conds = JSONBytes(condsJSON)
|
||||||
|
}
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return types.Int64(op.Id), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatePolicy 修改Policy
|
||||||
|
func (this *HTTPDeflatePolicyDAO) UpdatePolicy(tx *dbs.Tx, policyId int64, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) error {
|
||||||
|
if policyId <= 0 {
|
||||||
|
return errors.New("invalid policyId")
|
||||||
|
}
|
||||||
|
op := NewHTTPDeflatePolicyOperator()
|
||||||
|
op.Id = policyId
|
||||||
|
op.Level = level
|
||||||
|
if len(minLengthJSON) > 0 {
|
||||||
|
op.MinLength = JSONBytes(minLengthJSON)
|
||||||
|
}
|
||||||
|
if len(maxLengthJSON) > 0 {
|
||||||
|
op.MaxLength = JSONBytes(maxLengthJSON)
|
||||||
|
}
|
||||||
|
if len(condsJSON) > 0 {
|
||||||
|
op.Conds = JSONBytes(condsJSON)
|
||||||
|
}
|
||||||
|
err := this.Save(tx, op)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return this.NotifyUpdate(tx, policyId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyUpdate 通知更新
|
||||||
|
func (this *HTTPDeflatePolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
||||||
|
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithDeflatePolicyId(tx, policyId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if webId > 0 {
|
||||||
|
return SharedHTTPWebDAO.NotifyUpdate(tx, webId)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
6
internal/db/models/http_deflate_policy_dao_test.go
Normal file
6
internal/db/models/http_deflate_policy_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
_ "github.com/iwind/TeaGo/bootstrap"
|
||||||
|
)
|
||||||
32
internal/db/models/http_deflate_policy_model.go
Normal file
32
internal/db/models/http_deflate_policy_model.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
// HTTPDeflatePolicy Gzip配置
|
||||||
|
type HTTPDeflatePolicy 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"` // 创建时间
|
||||||
|
Conds string `field:"conds"` // 条件
|
||||||
|
}
|
||||||
|
|
||||||
|
type HTTPDeflatePolicyOperator struct {
|
||||||
|
Id interface{} // ID
|
||||||
|
AdminId interface{} // 管理员ID
|
||||||
|
UserId interface{} // 用户ID
|
||||||
|
IsOn interface{} // 是否启用
|
||||||
|
Level interface{} // 压缩级别
|
||||||
|
MinLength interface{} // 可压缩最小值
|
||||||
|
MaxLength interface{} // 可压缩最大值
|
||||||
|
State interface{} // 状态
|
||||||
|
CreatedAt interface{} // 创建时间
|
||||||
|
Conds interface{} // 条件
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPDeflatePolicyOperator() *HTTPDeflatePolicyOperator {
|
||||||
|
return &HTTPDeflatePolicyOperator{}
|
||||||
|
}
|
||||||
1
internal/db/models/http_deflate_policy_model_ext.go
Normal file
1
internal/db/models/http_deflate_policy_model_ext.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package models
|
||||||
@@ -37,12 +37,12 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化
|
// Init 初始化
|
||||||
func (this *HTTPGzipDAO) Init() {
|
func (this *HTTPGzipDAO) Init() {
|
||||||
_ = this.DAOObject.Init()
|
_ = this.DAOObject.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 启用条目
|
// EnableHTTPGzip 启用条目
|
||||||
func (this *HTTPGzipDAO) EnableHTTPGzip(tx *dbs.Tx, id int64) error {
|
func (this *HTTPGzipDAO) EnableHTTPGzip(tx *dbs.Tx, id int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -51,7 +51,7 @@ func (this *HTTPGzipDAO) EnableHTTPGzip(tx *dbs.Tx, id int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 禁用条目
|
// DisableHTTPGzip 禁用条目
|
||||||
func (this *HTTPGzipDAO) DisableHTTPGzip(tx *dbs.Tx, gzipId int64) error {
|
func (this *HTTPGzipDAO) DisableHTTPGzip(tx *dbs.Tx, gzipId int64) error {
|
||||||
_, err := this.Query(tx).
|
_, err := this.Query(tx).
|
||||||
Pk(gzipId).
|
Pk(gzipId).
|
||||||
@@ -63,7 +63,7 @@ func (this *HTTPGzipDAO) DisableHTTPGzip(tx *dbs.Tx, gzipId int64) error {
|
|||||||
return this.NotifyUpdate(tx, gzipId)
|
return this.NotifyUpdate(tx, gzipId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找启用中的条目
|
// FindEnabledHTTPGzip 查找启用中的条目
|
||||||
func (this *HTTPGzipDAO) FindEnabledHTTPGzip(tx *dbs.Tx, id int64) (*HTTPGzip, error) {
|
func (this *HTTPGzipDAO) FindEnabledHTTPGzip(tx *dbs.Tx, id int64) (*HTTPGzip, error) {
|
||||||
result, err := this.Query(tx).
|
result, err := this.Query(tx).
|
||||||
Pk(id).
|
Pk(id).
|
||||||
@@ -75,8 +75,8 @@ func (this *HTTPGzipDAO) FindEnabledHTTPGzip(tx *dbs.Tx, id int64) (*HTTPGzip, e
|
|||||||
return result.(*HTTPGzip), err
|
return result.(*HTTPGzip), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组合配置
|
// ComposeGzipConfig 组合配置
|
||||||
func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*serverconfigs.HTTPGzipConfig, error) {
|
func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*serverconfigs.HTTPGzipCompressionConfig, error) {
|
||||||
gzip, err := this.FindEnabledHTTPGzip(tx, gzipId)
|
gzip, err := this.FindEnabledHTTPGzip(tx, gzipId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -86,7 +86,7 @@ func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*servercon
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &serverconfigs.HTTPGzipConfig{}
|
config := &serverconfigs.HTTPGzipCompressionConfig{}
|
||||||
config.Id = int64(gzip.Id)
|
config.Id = int64(gzip.Id)
|
||||||
config.IsOn = gzip.IsOn == 1
|
config.IsOn = gzip.IsOn == 1
|
||||||
if IsNotNull(gzip.MinLength) {
|
if IsNotNull(gzip.MinLength) {
|
||||||
@@ -119,7 +119,7 @@ func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*servercon
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建Gzip
|
// CreateGzip 创建Gzip
|
||||||
func (this *HTTPGzipDAO) CreateGzip(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
func (this *HTTPGzipDAO) CreateGzip(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) {
|
||||||
op := NewHTTPGzipOperator()
|
op := NewHTTPGzipOperator()
|
||||||
op.State = HTTPGzipStateEnabled
|
op.State = HTTPGzipStateEnabled
|
||||||
@@ -141,7 +141,7 @@ func (this *HTTPGzipDAO) CreateGzip(tx *dbs.Tx, level int, minLengthJSON []byte,
|
|||||||
return types.Int64(op.Id), nil
|
return types.Int64(op.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改Gzip
|
// UpdateGzip 修改Gzip
|
||||||
func (this *HTTPGzipDAO) UpdateGzip(tx *dbs.Tx, gzipId int64, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) error {
|
func (this *HTTPGzipDAO) UpdateGzip(tx *dbs.Tx, 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")
|
||||||
@@ -165,7 +165,7 @@ func (this *HTTPGzipDAO) UpdateGzip(tx *dbs.Tx, gzipId int64, level int, minLeng
|
|||||||
return this.NotifyUpdate(tx, gzipId)
|
return this.NotifyUpdate(tx, gzipId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通知更新
|
// NotifyUpdate 通知更新
|
||||||
func (this *HTTPGzipDAO) NotifyUpdate(tx *dbs.Tx, gzipId int64) error {
|
func (this *HTTPGzipDAO) NotifyUpdate(tx *dbs.Tx, gzipId int64) error {
|
||||||
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithGzipId(tx, gzipId)
|
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithGzipId(tx, gzipId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -107,20 +107,41 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps.
|
|||||||
config.Root = rootConfig
|
config.Root = rootConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// gzip
|
// compression
|
||||||
if IsNotNull(web.Gzip) {
|
if IsNotNull(web.Compression) {
|
||||||
gzipRef := &serverconfigs.HTTPGzipRef{}
|
compression := &serverconfigs.HTTPCompressionConfig{}
|
||||||
err = json.Unmarshal([]byte(web.Gzip), gzipRef)
|
err = json.Unmarshal([]byte(web.Compression), compression)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.GzipRef = gzipRef
|
config.Compression = compression
|
||||||
|
|
||||||
gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(tx, gzipRef.GzipId)
|
// gzip
|
||||||
|
if compression.GzipRef != nil && compression.GzipRef.Id > 0 {
|
||||||
|
gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(tx, compression.GzipRef.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.Gzip = gzipConfig
|
compression.Gzip = gzipConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// brotli
|
||||||
|
if compression.BrotliRef != nil && compression.BrotliRef.Id > 0 {
|
||||||
|
brotliConfig, err := SharedHTTPBrotliPolicyDAO.ComposeBrotliConfig(tx, compression.BrotliRef.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
compression.Brotli = brotliConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// deflate
|
||||||
|
if compression.DeflateRef != nil && compression.DeflateRef.Id > 0 {
|
||||||
|
deflateConfig, err := SharedHTTPDeflatePolicyDAO.ComposeDeflateConfig(tx, compression.DeflateRef.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
compression.Deflate = deflateConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// charset
|
// charset
|
||||||
@@ -414,14 +435,14 @@ func (this *HTTPWebDAO) UpdateWeb(tx *dbs.Tx, webId int64, rootJSON []byte) erro
|
|||||||
return this.NotifyUpdate(tx, webId)
|
return this.NotifyUpdate(tx, webId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateWebGzip 修改Gzip配置
|
// UpdateWebCompression 修改压缩配置
|
||||||
func (this *HTTPWebDAO) UpdateWebGzip(tx *dbs.Tx, webId int64, gzipJSON []byte) error {
|
func (this *HTTPWebDAO) UpdateWebCompression(tx *dbs.Tx, webId int64, compressionConfig []byte) error {
|
||||||
if webId <= 0 {
|
if webId <= 0 {
|
||||||
return errors.New("invalid webId")
|
return errors.New("invalid webId")
|
||||||
}
|
}
|
||||||
op := NewHTTPWebOperator()
|
op := NewHTTPWebOperator()
|
||||||
op.Id = webId
|
op.Id = webId
|
||||||
op.Gzip = JSONBytes(gzipJSON)
|
op.Compression = JSONBytes(compressionConfig)
|
||||||
err := this.Save(tx, op)
|
err := this.Save(tx, op)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -806,8 +827,28 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithGzipId(tx *dbs.Tx, gzipId int64) (we
|
|||||||
return this.Query(tx).
|
return this.Query(tx).
|
||||||
State(HTTPWebStateEnabled).
|
State(HTTPWebStateEnabled).
|
||||||
ResultPk().
|
ResultPk().
|
||||||
Where("JSON_CONTAINS(gzip, :jsonQuery)").
|
Where("JSON_CONTAINS(compression, :jsonQuery, '$.gzipRef')").
|
||||||
Param("jsonQuery", maps.Map{"gzipId": gzipId}.AsJSON()).
|
Param("jsonQuery", maps.Map{"id": gzipId}.AsJSON()).
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledWebIdWithBrotliPolicyId 查找包含某个Brotli配置的Web
|
||||||
|
func (this *HTTPWebDAO) FindEnabledWebIdWithBrotliPolicyId(tx *dbs.Tx, brotliPolicyId int64) (webId int64, err error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
State(HTTPWebStateEnabled).
|
||||||
|
ResultPk().
|
||||||
|
Where("JSON_CONTAINS(compression, :jsonQuery, '$.brotliRef')").
|
||||||
|
Param("jsonQuery", maps.Map{"id": brotliPolicyId}.AsJSON()).
|
||||||
|
FindInt64Col(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindEnabledWebIdWithDeflatePolicyId 查找包含某个Deflate配置的Web
|
||||||
|
func (this *HTTPWebDAO) FindEnabledWebIdWithDeflatePolicyId(tx *dbs.Tx, deflatePolicyId int64) (webId int64, err error) {
|
||||||
|
return this.Query(tx).
|
||||||
|
State(HTTPWebStateEnabled).
|
||||||
|
ResultPk().
|
||||||
|
Where("JSON_CONTAINS(compression, :jsonQuery, '$.deflateRef')").
|
||||||
|
Param("jsonQuery", maps.Map{"id": deflatePolicyId}.AsJSON()).
|
||||||
FindInt64Col(0)
|
FindInt64Col(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ type HTTPWeb struct {
|
|||||||
ResponseHeader string `field:"responseHeader"` // 响应Header配置
|
ResponseHeader string `field:"responseHeader"` // 响应Header配置
|
||||||
AccessLog string `field:"accessLog"` // 访问日志配置
|
AccessLog string `field:"accessLog"` // 访问日志配置
|
||||||
Stat string `field:"stat"` // 统计配置
|
Stat string `field:"stat"` // 统计配置
|
||||||
Gzip string `field:"gzip"` // Gzip配置
|
Gzip string `field:"gzip"` // Gzip配置(v0.3.2启用)
|
||||||
|
Compression string `field:"compression"` // 压缩配置
|
||||||
Cache string `field:"cache"` // 缓存配置
|
Cache string `field:"cache"` // 缓存配置
|
||||||
Firewall string `field:"firewall"` // 防火墙设置
|
Firewall string `field:"firewall"` // 防火墙设置
|
||||||
Locations string `field:"locations"` // 路由规则配置
|
Locations string `field:"locations"` // 路由规则配置
|
||||||
@@ -50,7 +51,8 @@ type HTTPWebOperator struct {
|
|||||||
ResponseHeader interface{} // 响应Header配置
|
ResponseHeader interface{} // 响应Header配置
|
||||||
AccessLog interface{} // 访问日志配置
|
AccessLog interface{} // 访问日志配置
|
||||||
Stat interface{} // 统计配置
|
Stat interface{} // 统计配置
|
||||||
Gzip interface{} // Gzip配置
|
Gzip interface{} // Gzip配置(v0.3.2弃用)
|
||||||
|
Compression interface{} // 压缩配置
|
||||||
Cache interface{} // 缓存配置
|
Cache interface{} // 缓存配置
|
||||||
Firewall interface{} // 防火墙设置
|
Firewall interface{} // 防火墙设置
|
||||||
Locations interface{} // 路由规则配置
|
Locations interface{} // 路由规则配置
|
||||||
|
|||||||
@@ -121,8 +121,8 @@ func (this *HTTPWebService) UpdateHTTPWeb(ctx context.Context, req *pb.UpdateHTT
|
|||||||
return this.Success()
|
return this.Success()
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateHTTPWebGzip 修改Gzip配置
|
// UpdateHTTPWebCompression 修改压缩配置
|
||||||
func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.UpdateHTTPWebGzipRequest) (*pb.RPCSuccess, error) {
|
func (this *HTTPWebService) UpdateHTTPWebCompression(ctx context.Context, req *pb.UpdateHTTPWebCompressionRequest) (*pb.RPCSuccess, error) {
|
||||||
// 校验请求
|
// 校验请求
|
||||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -139,7 +139,7 @@ func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.Updat
|
|||||||
|
|
||||||
tx := this.NullTx()
|
tx := this.NullTx()
|
||||||
|
|
||||||
err = models.SharedHTTPWebDAO.UpdateWebGzip(tx, req.WebId, req.GzipJSON)
|
err = models.SharedHTTPWebDAO.UpdateWebCompression(tx, req.WebId, req.CompressionJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user