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

196 lines
4.3 KiB
Go
Raw Normal View History

2020-09-17 10:16:00 +08:00
package models
import (
"encoding/json"
"errors"
2021-11-11 14:16:42 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2020-09-17 10:16:00 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2021-10-10 10:34:56 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
2020-09-17 10:16:00 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
)
const (
HTTPPageStateEnabled = 1 // 已启用
HTTPPageStateDisabled = 0 // 已禁用
)
type HTTPPageDAO dbs.DAO
func NewHTTPPageDAO() *HTTPPageDAO {
return dbs.NewDAO(&HTTPPageDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeHTTPPages",
Model: new(HTTPPage),
PkName: "id",
},
}).(*HTTPPageDAO)
}
2020-10-13 20:05:13 +08:00
var SharedHTTPPageDAO *HTTPPageDAO
func init() {
dbs.OnReady(func() {
SharedHTTPPageDAO = NewHTTPPageDAO()
})
}
2020-09-17 10:16:00 +08:00
2021-08-22 11:35:33 +08:00
// Init 初始化
2020-09-26 08:06:40 +08:00
func (this *HTTPPageDAO) Init() {
_ = this.DAOObject.Init()
2020-09-26 08:06:40 +08:00
}
2021-08-22 11:35:33 +08:00
// EnableHTTPPage 启用条目
func (this *HTTPPageDAO) EnableHTTPPage(tx *dbs.Tx, pageId int64) error {
_, err := this.Query(tx).
Pk(pageId).
2020-09-17 10:16:00 +08:00
Set("state", HTTPPageStateEnabled).
Update()
if err != nil {
return err
}
return this.NotifyUpdate(tx, pageId)
2020-09-17 10:16:00 +08:00
}
2021-08-22 11:35:33 +08:00
// DisableHTTPPage 禁用条目
func (this *HTTPPageDAO) DisableHTTPPage(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
2020-09-17 10:16:00 +08:00
Pk(id).
Set("state", HTTPPageStateDisabled).
Update()
return err
}
2021-08-22 11:35:33 +08:00
// FindEnabledHTTPPage 查找启用中的条目
func (this *HTTPPageDAO) FindEnabledHTTPPage(tx *dbs.Tx, id int64) (*HTTPPage, error) {
result, err := this.Query(tx).
2020-09-17 10:16:00 +08:00
Pk(id).
Attr("state", HTTPPageStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*HTTPPage), err
}
2021-08-22 11:35:33 +08:00
// CreatePage 创建Page
2021-10-10 10:34:56 +08:00
func (this *HTTPPageDAO) CreatePage(tx *dbs.Tx, statusList []string, bodyType shared.BodyType, url string, body string, newStatus int) (pageId int64, err error) {
2020-09-17 10:16:00 +08:00
op := NewHTTPPageOperator()
op.IsOn = true
op.State = HTTPPageStateEnabled
if len(statusList) > 0 {
statusListJSON, err := json.Marshal(statusList)
if err != nil {
return 0, err
}
op.StatusList = string(statusListJSON)
}
2021-10-10 10:34:56 +08:00
op.BodyType = bodyType
2020-09-17 10:16:00 +08:00
op.Url = url
2021-10-10 10:34:56 +08:00
op.Body = body
2020-09-17 10:16:00 +08:00
op.NewStatus = newStatus
err = this.Save(tx, op)
2020-09-17 10:16:00 +08:00
if err != nil {
return 0, err
}
return types.Int64(op.Id), nil
}
2021-08-22 11:35:33 +08:00
// UpdatePage 修改Page
2021-10-10 10:34:56 +08:00
func (this *HTTPPageDAO) UpdatePage(tx *dbs.Tx, pageId int64, statusList []string, bodyType shared.BodyType, url string, body string, newStatus int) error {
2020-09-17 10:16:00 +08:00
if pageId <= 0 {
return errors.New("invalid pageId")
}
op := NewHTTPPageOperator()
op.Id = pageId
op.IsOn = true
op.State = HTTPPageStateEnabled
if statusList == nil {
statusList = []string{}
}
statusListJSON, err := json.Marshal(statusList)
if err != nil {
return err
}
op.StatusList = string(statusListJSON)
2021-10-10 10:34:56 +08:00
op.BodyType = bodyType
2020-09-17 10:16:00 +08:00
op.Url = url
2021-10-10 10:34:56 +08:00
op.Body = body
2020-09-17 10:16:00 +08:00
op.NewStatus = newStatus
err = this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, pageId)
2020-09-17 10:16:00 +08:00
}
2021-08-22 11:35:33 +08:00
// ComposePageConfig 组合配置
2021-11-11 14:16:42 +08:00
func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap *utils.CacheMap) (*serverconfigs.HTTPPageConfig, error) {
2021-08-22 11:35:33 +08:00
if cacheMap == nil {
2021-11-11 14:16:42 +08:00
cacheMap = utils.NewCacheMap()
2021-08-22 11:35:33 +08:00
}
var cacheKey = this.Table + ":config:" + types.String(pageId)
2021-11-11 14:16:42 +08:00
var cache, _ = cacheMap.Get(cacheKey)
2021-08-22 11:35:33 +08:00
if cache != nil {
return cache.(*serverconfigs.HTTPPageConfig), nil
}
page, err := this.FindEnabledHTTPPage(tx, pageId)
2020-09-17 10:16:00 +08:00
if err != nil {
return nil, err
}
if page == nil {
return nil, nil
}
config := &serverconfigs.HTTPPageConfig{}
config.Id = int64(page.Id)
config.IsOn = page.IsOn == 1
config.NewStatus = int(page.NewStatus)
config.URL = page.Url
2021-10-10 10:34:56 +08:00
config.Body = page.Body
config.BodyType = page.BodyType
if len(page.BodyType) == 0 {
page.BodyType = shared.BodyTypeURL
}
2020-09-17 10:16:00 +08:00
if len(page.StatusList) > 0 {
statusList := []string{}
2022-03-22 19:30:30 +08:00
err = json.Unmarshal(page.StatusList, &statusList)
2020-09-17 10:16:00 +08:00
if err != nil {
return nil, err
}
if len(statusList) > 0 {
config.Status = statusList
}
}
2021-11-11 14:16:42 +08:00
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
2021-08-22 11:35:33 +08:00
2020-09-17 10:16:00 +08:00
return config, nil
}
2021-08-22 11:35:33 +08:00
// NotifyUpdate 通知更新
func (this *HTTPPageDAO) NotifyUpdate(tx *dbs.Tx, pageId int64) error {
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithPageId(tx, pageId)
if err != nil {
return err
}
if webId > 0 {
return SharedHTTPWebDAO.NotifyUpdate(tx, webId)
}
return nil
}