Files
EdgeAPI/internal/db/models/http_brotli_policy_dao.go

171 lines
4.3 KiB
Go
Raw Normal View History

2021-09-29 19:32:25 +08:00
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)
2022-03-22 21:45:07 +08:00
config.IsOn = policy.IsOn
2021-09-29 19:32:25 +08:00
if IsNotNull(policy.MinLength) {
minLengthConfig := &shared.SizeCapacity{}
2022-03-22 19:30:30 +08:00
err = json.Unmarshal(policy.MinLength, minLengthConfig)
2021-09-29 19:32:25 +08:00
if err != nil {
return nil, err
}
config.MinLength = minLengthConfig
}
if IsNotNull(policy.MaxLength) {
maxLengthConfig := &shared.SizeCapacity{}
2022-03-22 19:30:30 +08:00
err = json.Unmarshal(policy.MaxLength, maxLengthConfig)
2021-09-29 19:32:25 +08:00
if err != nil {
return nil, err
}
config.MaxLength = maxLengthConfig
}
config.Level = types.Int8(policy.Level)
if IsNotNull(policy.Conds) {
condsConfig := &shared.HTTPRequestCondsConfig{}
2022-03-22 19:30:30 +08:00
err = json.Unmarshal(policy.Conds, condsConfig)
2021-09-29 19:32:25 +08:00
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) {
var op = NewHTTPBrotliPolicyOperator()
2021-09-29 19:32:25 +08:00
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")
}
var op = NewHTTPBrotliPolicyOperator()
2021-09-29 19:32:25 +08:00
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
}