mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-03-08 07:15:37 +08:00
阶段性提交
This commit is contained in:
133
internal/db/models/http_gzip_dao.go
Normal file
133
internal/db/models/http_gzip_dao.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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 (
|
||||
HTTPGzipStateEnabled = 1 // 已启用
|
||||
HTTPGzipStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type HTTPGzipDAO dbs.DAO
|
||||
|
||||
func NewHTTPGzipDAO() *HTTPGzipDAO {
|
||||
return dbs.NewDAO(&HTTPGzipDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeHTTPGzips",
|
||||
Model: new(HTTPGzip),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*HTTPGzipDAO)
|
||||
}
|
||||
|
||||
var SharedHTTPGzipDAO = NewHTTPGzipDAO()
|
||||
|
||||
// 启用条目
|
||||
func (this *HTTPGzipDAO) EnableHTTPGzip(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", HTTPGzipStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *HTTPGzipDAO) DisableHTTPGzip(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", HTTPGzipStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *HTTPGzipDAO) FindEnabledHTTPGzip(id int64) (*HTTPGzip, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", HTTPGzipStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*HTTPGzip), err
|
||||
}
|
||||
|
||||
// 组合配置
|
||||
func (this *HTTPGzipDAO) ComposeGzipConfig(gzipId int64) (*serverconfigs.HTTPGzipConfig, error) {
|
||||
gzip, err := this.FindEnabledHTTPGzip(gzipId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if gzip == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
config := &serverconfigs.HTTPGzipConfig{}
|
||||
config.Id = int64(gzip.Id)
|
||||
config.IsOn = gzip.IsOn == 1
|
||||
if len(gzip.MinLength) > 0 && gzip.MinLength != "null" {
|
||||
minLengthConfig := &shared.SizeCapacity{}
|
||||
err = json.Unmarshal([]byte(gzip.MinLength), minLengthConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.MinLength = minLengthConfig
|
||||
}
|
||||
if len(gzip.MaxLength) > 0 && gzip.MaxLength != "null" {
|
||||
maxLengthConfig := &shared.SizeCapacity{}
|
||||
err = json.Unmarshal([]byte(gzip.MaxLength), maxLengthConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.MaxLength = maxLengthConfig
|
||||
}
|
||||
config.Level = types.Int8(gzip.Level)
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// 创建Gzip
|
||||
func (this *HTTPGzipDAO) CreateGzip(level int, minLengthJSON []byte, maxLengthJSON []byte) (int64, error) {
|
||||
op := NewHTTPGzipOperator()
|
||||
op.State = HTTPGzipStateEnabled
|
||||
op.IsOn = true
|
||||
op.Level = level
|
||||
if len(minLengthJSON) > 0 {
|
||||
op.MinLength = string(minLengthJSON)
|
||||
}
|
||||
if len(maxLengthJSON) > 0 {
|
||||
op.MaxLength = string(maxLengthJSON)
|
||||
}
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改Gzip
|
||||
func (this *HTTPGzipDAO) UpdateGzip(gzipId int64, level int, minLengthJSON []byte, maxLengthJSON []byte) error {
|
||||
if gzipId <= 0 {
|
||||
return errors.New("invalid gzipId")
|
||||
}
|
||||
op := NewHTTPGzipOperator()
|
||||
op.Id = gzipId
|
||||
op.Level = level
|
||||
if len(minLengthJSON) > 0 {
|
||||
op.MinLength = string(minLengthJSON)
|
||||
}
|
||||
if len(maxLengthJSON) > 0 {
|
||||
op.MaxLength = string(maxLengthJSON)
|
||||
}
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
}
|
||||
5
internal/db/models/http_gzip_dao_test.go
Normal file
5
internal/db/models/http_gzip_dao_test.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
30
internal/db/models/http_gzip_model.go
Normal file
30
internal/db/models/http_gzip_model.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package models
|
||||
|
||||
// Gzip配置
|
||||
type HTTPGzip 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 uint32 `field:"createdAt"` // 创建时间
|
||||
}
|
||||
|
||||
type HTTPGzipOperator struct {
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
IsOn interface{} // 是否启用
|
||||
Level interface{} // 压缩级别
|
||||
MinLength interface{} // 可压缩最小值
|
||||
MaxLength interface{} // 可压缩最大值
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
}
|
||||
|
||||
func NewHTTPGzipOperator() *HTTPGzipOperator {
|
||||
return &HTTPGzipOperator{}
|
||||
}
|
||||
26
internal/db/models/http_gzip_model_ext.go
Normal file
26
internal/db/models/http_gzip_model_ext.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
)
|
||||
|
||||
// 解析最小长度
|
||||
func (this *HTTPGzip) DecodeMinLength() (*shared.SizeCapacity, error) {
|
||||
if len(this.MinLength) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
capacity := &shared.SizeCapacity{}
|
||||
err := json.Unmarshal([]byte(this.MinLength), capacity)
|
||||
return capacity, err
|
||||
}
|
||||
|
||||
// 解析最大长度
|
||||
func (this *HTTPGzip) DecodeMaxLength() (*shared.SizeCapacity, error) {
|
||||
if len(this.MaxLength) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
capacity := &shared.SizeCapacity{}
|
||||
err := json.Unmarshal([]byte(this.MaxLength), capacity)
|
||||
return capacity, err
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (this *HTTPWebDAO) FindEnabledHTTPWeb(id int64) (*HTTPWeb, error) {
|
||||
}
|
||||
|
||||
// 组合配置
|
||||
func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.WebConfig, error) {
|
||||
func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebConfig, error) {
|
||||
web, err := SharedHTTPWebDAO.FindEnabledHTTPWeb(webId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -68,9 +68,24 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.WebConfig,
|
||||
if web == nil {
|
||||
return nil, nil
|
||||
}
|
||||
config := &serverconfigs.WebConfig{}
|
||||
config := &serverconfigs.HTTPWebConfig{}
|
||||
config.Id = webId
|
||||
config.IsOn = web.IsOn == 1
|
||||
config.Root = web.Root
|
||||
|
||||
// gzip
|
||||
if web.GzipId > 0 {
|
||||
gzipConfig, err := SharedHTTPGzipDAO.ComposeGzipConfig(int64(web.GzipId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Gzip = gzipConfig
|
||||
}
|
||||
|
||||
// TODO charset
|
||||
|
||||
// TODO 更多配置
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
@@ -95,8 +110,37 @@ func (this *HTTPWebDAO) UpdateWeb(webId int64, root string) error {
|
||||
op.Id = webId
|
||||
op.Root = root
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO 更新所有使用此Web配置的服务
|
||||
|
||||
return err
|
||||
return this.NotifyUpdating(webId)
|
||||
}
|
||||
|
||||
// 修改Gzip配置
|
||||
func (this *HTTPWebDAO) UpdateWebGzip(webId int64, gzipId int64) error {
|
||||
if webId <= 0 {
|
||||
return errors.New("invalid webId")
|
||||
}
|
||||
op := NewHTTPWebOperator()
|
||||
op.Id = webId
|
||||
op.GzipId = gzipId
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdating(webId)
|
||||
}
|
||||
|
||||
// 通知更新
|
||||
func (this *HTTPWebDAO) NotifyUpdating(webId int64) error {
|
||||
err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO 更新所有使用此Web配置的Location所在服务
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ type HTTPWeb struct {
|
||||
State uint8 `field:"state"` // 状态
|
||||
CreatedAt uint32 `field:"createdAt"` // 创建时间
|
||||
Root string `field:"root"` // 资源根目录
|
||||
GzipId uint32 `field:"gzipId"` // Gzip配置
|
||||
}
|
||||
|
||||
type HTTPWebOperator struct {
|
||||
@@ -21,6 +22,7 @@ type HTTPWebOperator struct {
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
Root interface{} // 资源根目录
|
||||
GzipId interface{} // Gzip配置
|
||||
}
|
||||
|
||||
func NewHTTPWebOperator() *HTTPWebOperator {
|
||||
|
||||
@@ -118,7 +118,7 @@ func (this *OriginServerDAO) ComposeOriginConfig(originId int64) (*serverconfigs
|
||||
}
|
||||
}
|
||||
|
||||
connTimeout := shared.TimeDuration{}
|
||||
connTimeout := &shared.TimeDuration{}
|
||||
if len(origin.ConnTimeout) > 0 && origin.ConnTimeout != "null" {
|
||||
err = json.Unmarshal([]byte(origin.ConnTimeout), &connTimeout)
|
||||
if err != nil {
|
||||
@@ -126,7 +126,7 @@ func (this *OriginServerDAO) ComposeOriginConfig(originId int64) (*serverconfigs
|
||||
}
|
||||
}
|
||||
|
||||
readTimeout := shared.TimeDuration{}
|
||||
readTimeout := &shared.TimeDuration{}
|
||||
if len(origin.ReadTimeout) > 0 && origin.ReadTimeout != "null" {
|
||||
err = json.Unmarshal([]byte(origin.ReadTimeout), &readTimeout)
|
||||
if err != nil {
|
||||
@@ -134,7 +134,7 @@ func (this *OriginServerDAO) ComposeOriginConfig(originId int64) (*serverconfigs
|
||||
}
|
||||
}
|
||||
|
||||
idleTimeout := shared.TimeDuration{}
|
||||
idleTimeout := &shared.TimeDuration{}
|
||||
if len(origin.IdleTimeout) > 0 && origin.IdleTimeout != "null" {
|
||||
err = json.Unmarshal([]byte(origin.IdleTimeout), &idleTimeout)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,4 +2,13 @@ package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOriginServerDAO_ComposeOriginConfig(t *testing.T) {
|
||||
config, err := SharedOriginServerDAO.ComposeOriginConfig(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(config)
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ func (this *ReverseProxyDAO) ComposeReverseProxyConfig(reverseProxyId int64) (*s
|
||||
return nil, err
|
||||
}
|
||||
for _, originConfig := range originConfigs {
|
||||
newOriginConfig, err := SharedOriginServerDAO.ComposeOriginConfig(int64(originConfig.Id))
|
||||
newOriginConfig, err := SharedOriginServerDAO.ComposeOriginConfig(originConfig.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func (this *ReverseProxyDAO) ComposeReverseProxyConfig(reverseProxyId int64) (*s
|
||||
}
|
||||
|
||||
if len(reverseProxy.BackupOrigins) > 0 && reverseProxy.BackupOrigins != "null" {
|
||||
originConfigs := []*OriginServer{}
|
||||
originConfigs := []*serverconfigs.OriginServerConfig{}
|
||||
err = json.Unmarshal([]byte(reverseProxy.BackupOrigins), &originConfigs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -195,3 +195,12 @@ func (this *ReverseProxyDAO) UpdateReverseProxyBackupOrigins(reverseProxyId int6
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改是否启用
|
||||
func (this *ReverseProxyDAO) UpdateReverseProxyIsOn(reverseProxyId int64, isOn bool) error {
|
||||
_, err := this.Query().
|
||||
Pk(reverseProxyId).
|
||||
Set("isOn", isOn).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -283,6 +283,28 @@ func (this *ServerDAO) UpdateServerWeb(serverId int64, webId int64) error {
|
||||
return this.RenewServerConfig(serverId)
|
||||
}
|
||||
|
||||
// 初始化Web配置
|
||||
func (this *ServerDAO) InitServerWeb(serverId int64) (int64, error) {
|
||||
if serverId <= 0 {
|
||||
return 0, errors.New("serverId should not be smaller than 0")
|
||||
}
|
||||
|
||||
webId, err := SharedHTTPWebDAO.CreateWeb("")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
_, err = this.Query().
|
||||
Pk(serverId).
|
||||
Set("webId", webId).
|
||||
Update()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return webId, nil
|
||||
}
|
||||
|
||||
// 修改ServerNames配置
|
||||
func (this *ServerDAO) UpdateServerNames(serverId int64, config []byte) error {
|
||||
if serverId <= 0 {
|
||||
@@ -503,6 +525,40 @@ func (this *ServerDAO) FindReverseProxyConfig(serverId int64) (*serverconfigs.Re
|
||||
return config, err
|
||||
}
|
||||
|
||||
// 查找需要更新的Server
|
||||
func (this *ServerDAO) FindUpdatingServerIds() (serverIds []int64, err error) {
|
||||
ones, err := this.Query().
|
||||
State(ServerStateEnabled).
|
||||
Attr("isUpdating", true).
|
||||
ResultPk().
|
||||
FindAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, one := range ones {
|
||||
serverIds = append(serverIds, int64(one.(*Server).Id))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 修改服务是否需要更新
|
||||
func (this *ServerDAO) UpdateServerIsUpdating(serverId int64, isUpdating bool) error {
|
||||
_, err := this.Query().
|
||||
Pk(serverId).
|
||||
Set("isUpdating", isUpdating).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新所有Web相关的处于更新状态
|
||||
func (this *ServerDAO) UpdateServerIsUpdatingWithWebId(webId int64) error {
|
||||
_, err := this.Query().
|
||||
Attr("webId", webId).
|
||||
Set("isUpdating", true).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
func (this *ServerDAO) genUniqueId() (string, error) {
|
||||
for {
|
||||
|
||||
@@ -26,6 +26,7 @@ type Server struct {
|
||||
ExcludeNodes string `field:"excludeNodes"` // 节点排除条件
|
||||
Version uint32 `field:"version"` // 版本号
|
||||
CreatedAt uint32 `field:"createdAt"` // 创建时间
|
||||
IsUpdating uint8 `field:"isUpdating"` // 是否正在更新
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
@@ -54,6 +55,7 @@ type ServerOperator struct {
|
||||
ExcludeNodes interface{} // 节点排除条件
|
||||
Version interface{} // 版本号
|
||||
CreatedAt interface{} // 创建时间
|
||||
IsUpdating interface{} // 是否正在更新
|
||||
State interface{} // 状态
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user