diff --git a/go.sum b/go.sum index d360b62e..5ba27955 100644 --- a/go.sum +++ b/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/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-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/go.mod h1:KU4mS7QNiZ7QWEuDBk1zw0/Q2LrAPZv3tycEFBsuUwc= github.com/iwind/gosock v0.0.0-20210722083328-12b2d66abec3 h1:aBSonas7vFcgTj9u96/bWGILGv1ZbUSTLiOzcI1ZT6c= diff --git a/internal/db/models/http_brotli_policy_dao.go b/internal/db/models/http_brotli_policy_dao.go new file mode 100644 index 00000000..a9e51be3 --- /dev/null +++ b/internal/db/models/http_brotli_policy_dao.go @@ -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 +} diff --git a/internal/db/models/http_brotli_policy_dao_test.go b/internal/db/models/http_brotli_policy_dao_test.go new file mode 100644 index 00000000..224e9db7 --- /dev/null +++ b/internal/db/models/http_brotli_policy_dao_test.go @@ -0,0 +1,6 @@ +package models + +import ( + _ "github.com/go-sql-driver/mysql" + _ "github.com/iwind/TeaGo/bootstrap" +) diff --git a/internal/db/models/http_brotli_policy_model.go b/internal/db/models/http_brotli_policy_model.go new file mode 100644 index 00000000..253e9541 --- /dev/null +++ b/internal/db/models/http_brotli_policy_model.go @@ -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{} +} diff --git a/internal/db/models/http_brotli_policy_model_ext.go b/internal/db/models/http_brotli_policy_model_ext.go new file mode 100644 index 00000000..2640e7f9 --- /dev/null +++ b/internal/db/models/http_brotli_policy_model_ext.go @@ -0,0 +1 @@ +package models diff --git a/internal/db/models/http_deflate_policy_dao.go b/internal/db/models/http_deflate_policy_dao.go new file mode 100644 index 00000000..85676d2d --- /dev/null +++ b/internal/db/models/http_deflate_policy_dao.go @@ -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 +} diff --git a/internal/db/models/http_deflate_policy_dao_test.go b/internal/db/models/http_deflate_policy_dao_test.go new file mode 100644 index 00000000..224e9db7 --- /dev/null +++ b/internal/db/models/http_deflate_policy_dao_test.go @@ -0,0 +1,6 @@ +package models + +import ( + _ "github.com/go-sql-driver/mysql" + _ "github.com/iwind/TeaGo/bootstrap" +) diff --git a/internal/db/models/http_deflate_policy_model.go b/internal/db/models/http_deflate_policy_model.go new file mode 100644 index 00000000..7843a0a4 --- /dev/null +++ b/internal/db/models/http_deflate_policy_model.go @@ -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{} +} diff --git a/internal/db/models/http_deflate_policy_model_ext.go b/internal/db/models/http_deflate_policy_model_ext.go new file mode 100644 index 00000000..2640e7f9 --- /dev/null +++ b/internal/db/models/http_deflate_policy_model_ext.go @@ -0,0 +1 @@ +package models diff --git a/internal/db/models/http_gzip_dao.go b/internal/db/models/http_gzip_dao.go index f2a68c8a..36b2506c 100644 --- a/internal/db/models/http_gzip_dao.go +++ b/internal/db/models/http_gzip_dao.go @@ -37,12 +37,12 @@ func init() { }) } -// 初始化 +// Init 初始化 func (this *HTTPGzipDAO) Init() { _ = this.DAOObject.Init() } -// 启用条目 +// EnableHTTPGzip 启用条目 func (this *HTTPGzipDAO) EnableHTTPGzip(tx *dbs.Tx, id int64) error { _, err := this.Query(tx). Pk(id). @@ -51,7 +51,7 @@ func (this *HTTPGzipDAO) EnableHTTPGzip(tx *dbs.Tx, id int64) error { return err } -// 禁用条目 +// DisableHTTPGzip 禁用条目 func (this *HTTPGzipDAO) DisableHTTPGzip(tx *dbs.Tx, gzipId int64) error { _, err := this.Query(tx). Pk(gzipId). @@ -63,7 +63,7 @@ func (this *HTTPGzipDAO) DisableHTTPGzip(tx *dbs.Tx, gzipId int64) error { return this.NotifyUpdate(tx, gzipId) } -// 查找启用中的条目 +// FindEnabledHTTPGzip 查找启用中的条目 func (this *HTTPGzipDAO) FindEnabledHTTPGzip(tx *dbs.Tx, id int64) (*HTTPGzip, error) { result, err := this.Query(tx). Pk(id). @@ -75,8 +75,8 @@ func (this *HTTPGzipDAO) FindEnabledHTTPGzip(tx *dbs.Tx, id int64) (*HTTPGzip, e return result.(*HTTPGzip), err } -// 组合配置 -func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*serverconfigs.HTTPGzipConfig, error) { +// ComposeGzipConfig 组合配置 +func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*serverconfigs.HTTPGzipCompressionConfig, error) { gzip, err := this.FindEnabledHTTPGzip(tx, gzipId) if err != nil { return nil, err @@ -86,7 +86,7 @@ func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*servercon return nil, nil } - config := &serverconfigs.HTTPGzipConfig{} + config := &serverconfigs.HTTPGzipCompressionConfig{} config.Id = int64(gzip.Id) config.IsOn = gzip.IsOn == 1 if IsNotNull(gzip.MinLength) { @@ -119,7 +119,7 @@ func (this *HTTPGzipDAO) ComposeGzipConfig(tx *dbs.Tx, gzipId int64) (*servercon return config, nil } -// 创建Gzip +// CreateGzip 创建Gzip func (this *HTTPGzipDAO) CreateGzip(tx *dbs.Tx, level int, minLengthJSON []byte, maxLengthJSON []byte, condsJSON []byte) (int64, error) { op := NewHTTPGzipOperator() op.State = HTTPGzipStateEnabled @@ -141,7 +141,7 @@ func (this *HTTPGzipDAO) CreateGzip(tx *dbs.Tx, level int, minLengthJSON []byte, 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 { if gzipId <= 0 { 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) } -// 通知更新 +// NotifyUpdate 通知更新 func (this *HTTPGzipDAO) NotifyUpdate(tx *dbs.Tx, gzipId int64) error { webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithGzipId(tx, gzipId) if err != nil { diff --git a/internal/db/models/http_web_dao.go b/internal/db/models/http_web_dao.go index 1cdbbbc2..0ff30d27 100644 --- a/internal/db/models/http_web_dao.go +++ b/internal/db/models/http_web_dao.go @@ -107,20 +107,41 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap maps. config.Root = rootConfig } - // gzip - if IsNotNull(web.Gzip) { - gzipRef := &serverconfigs.HTTPGzipRef{} - err = json.Unmarshal([]byte(web.Gzip), gzipRef) + // compression + if IsNotNull(web.Compression) { + compression := &serverconfigs.HTTPCompressionConfig{} + err = json.Unmarshal([]byte(web.Compression), compression) if err != nil { return nil, err } - config.GzipRef = gzipRef + config.Compression = compression - gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(tx, gzipRef.GzipId) - if err != nil { - return nil, err + // gzip + if compression.GzipRef != nil && compression.GzipRef.Id > 0 { + gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(tx, compression.GzipRef.Id) + if err != nil { + return nil, err + } + 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 } - config.Gzip = gzipConfig } // charset @@ -414,14 +435,14 @@ func (this *HTTPWebDAO) UpdateWeb(tx *dbs.Tx, webId int64, rootJSON []byte) erro return this.NotifyUpdate(tx, webId) } -// UpdateWebGzip 修改Gzip配置 -func (this *HTTPWebDAO) UpdateWebGzip(tx *dbs.Tx, webId int64, gzipJSON []byte) error { +// UpdateWebCompression 修改压缩配置 +func (this *HTTPWebDAO) UpdateWebCompression(tx *dbs.Tx, webId int64, compressionConfig []byte) error { if webId <= 0 { return errors.New("invalid webId") } op := NewHTTPWebOperator() op.Id = webId - op.Gzip = JSONBytes(gzipJSON) + op.Compression = JSONBytes(compressionConfig) err := this.Save(tx, op) if err != nil { return err @@ -806,8 +827,28 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithGzipId(tx *dbs.Tx, gzipId int64) (we return this.Query(tx). State(HTTPWebStateEnabled). ResultPk(). - Where("JSON_CONTAINS(gzip, :jsonQuery)"). - Param("jsonQuery", maps.Map{"gzipId": gzipId}.AsJSON()). + Where("JSON_CONTAINS(compression, :jsonQuery, '$.gzipRef')"). + 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) } diff --git a/internal/db/models/http_web_model.go b/internal/db/models/http_web_model.go index 7249ed72..3dfa342c 100644 --- a/internal/db/models/http_web_model.go +++ b/internal/db/models/http_web_model.go @@ -20,7 +20,8 @@ type HTTPWeb struct { ResponseHeader string `field:"responseHeader"` // 响应Header配置 AccessLog string `field:"accessLog"` // 访问日志配置 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"` // 缓存配置 Firewall string `field:"firewall"` // 防火墙设置 Locations string `field:"locations"` // 路由规则配置 @@ -50,7 +51,8 @@ type HTTPWebOperator struct { ResponseHeader interface{} // 响应Header配置 AccessLog interface{} // 访问日志配置 Stat interface{} // 统计配置 - Gzip interface{} // Gzip配置 + Gzip interface{} // Gzip配置(v0.3.2弃用) + Compression interface{} // 压缩配置 Cache interface{} // 缓存配置 Firewall interface{} // 防火墙设置 Locations interface{} // 路由规则配置 diff --git a/internal/rpc/services/service_http_web.go b/internal/rpc/services/service_http_web.go index 5d6f3387..b4159351 100644 --- a/internal/rpc/services/service_http_web.go +++ b/internal/rpc/services/service_http_web.go @@ -121,8 +121,8 @@ func (this *HTTPWebService) UpdateHTTPWeb(ctx context.Context, req *pb.UpdateHTT return this.Success() } -// UpdateHTTPWebGzip 修改Gzip配置 -func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.UpdateHTTPWebGzipRequest) (*pb.RPCSuccess, error) { +// UpdateHTTPWebCompression 修改压缩配置 +func (this *HTTPWebService) UpdateHTTPWebCompression(ctx context.Context, req *pb.UpdateHTTPWebCompressionRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) if err != nil { @@ -139,7 +139,7 @@ func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.Updat tx := this.NullTx() - err = models.SharedHTTPWebDAO.UpdateWebGzip(tx, req.WebId, req.GzipJSON) + err = models.SharedHTTPWebDAO.UpdateWebCompression(tx, req.WebId, req.CompressionJSON) if err != nil { return nil, err }