mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 06:40:26 +08:00
147 lines
2.8 KiB
Go
147 lines
2.8 KiB
Go
package models
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"github.com/iwind/TeaGo/types"
|
|
)
|
|
|
|
const (
|
|
HTTPWebStateEnabled = 1 // 已启用
|
|
HTTPWebStateDisabled = 0 // 已禁用
|
|
)
|
|
|
|
type HTTPWebDAO dbs.DAO
|
|
|
|
func NewHTTPWebDAO() *HTTPWebDAO {
|
|
return dbs.NewDAO(&HTTPWebDAO{
|
|
DAOObject: dbs.DAOObject{
|
|
DB: Tea.Env,
|
|
Table: "edgeHTTPWebs",
|
|
Model: new(HTTPWeb),
|
|
PkName: "id",
|
|
},
|
|
}).(*HTTPWebDAO)
|
|
}
|
|
|
|
var SharedHTTPWebDAO = NewHTTPWebDAO()
|
|
|
|
// 启用条目
|
|
func (this *HTTPWebDAO) EnableHTTPWeb(id int64) error {
|
|
_, err := this.Query().
|
|
Pk(id).
|
|
Set("state", HTTPWebStateEnabled).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
// 禁用条目
|
|
func (this *HTTPWebDAO) DisableHTTPWeb(id int64) error {
|
|
_, err := this.Query().
|
|
Pk(id).
|
|
Set("state", HTTPWebStateDisabled).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
// 查找启用中的条目
|
|
func (this *HTTPWebDAO) FindEnabledHTTPWeb(id int64) (*HTTPWeb, error) {
|
|
result, err := this.Query().
|
|
Pk(id).
|
|
Attr("state", HTTPWebStateEnabled).
|
|
Find()
|
|
if result == nil {
|
|
return nil, err
|
|
}
|
|
return result.(*HTTPWeb), err
|
|
}
|
|
|
|
// 组合配置
|
|
func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebConfig, error) {
|
|
web, err := SharedHTTPWebDAO.FindEnabledHTTPWeb(webId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if web == nil {
|
|
return nil, nil
|
|
}
|
|
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
|
|
}
|
|
|
|
// 创建Web配置
|
|
func (this *HTTPWebDAO) CreateWeb(root string) (int64, error) {
|
|
op := NewHTTPWebOperator()
|
|
op.State = HTTPWebStateEnabled
|
|
op.Root = root
|
|
_, err := this.Save(op)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return types.Int64(op.Id), nil
|
|
}
|
|
|
|
// 修改Web配置
|
|
func (this *HTTPWebDAO) UpdateWeb(webId int64, root string) error {
|
|
if webId <= 0 {
|
|
return errors.New("invalid webId")
|
|
}
|
|
op := NewHTTPWebOperator()
|
|
op.Id = webId
|
|
op.Root = root
|
|
_, err := this.Save(op)
|
|
if err != nil {
|
|
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
|
|
}
|