mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2026-02-11 11:45:36 +08:00
简化代码/增加HTTPPage服务
This commit is contained in:
144
internal/db/models/http_page_dao.go
Normal file
144
internal/db/models/http_page_dao.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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 (
|
||||
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)
|
||||
}
|
||||
|
||||
var SharedHTTPPageDAO = NewHTTPPageDAO()
|
||||
|
||||
// 启用条目
|
||||
func (this *HTTPPageDAO) EnableHTTPPage(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", HTTPPageStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *HTTPPageDAO) DisableHTTPPage(id int64) error {
|
||||
_, err := this.Query().
|
||||
Pk(id).
|
||||
Set("state", HTTPPageStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *HTTPPageDAO) FindEnabledHTTPPage(id int64) (*HTTPPage, error) {
|
||||
result, err := this.Query().
|
||||
Pk(id).
|
||||
Attr("state", HTTPPageStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*HTTPPage), err
|
||||
}
|
||||
|
||||
// 创建Page
|
||||
func (this *HTTPPageDAO) CreatePage(statusList []string, url string, newStatus int) (pageId int64, err error) {
|
||||
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)
|
||||
}
|
||||
op.Url = url
|
||||
op.NewStatus = newStatus
|
||||
_, err = this.Save(op)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改Page
|
||||
func (this *HTTPPageDAO) UpdatePage(pageId int64, statusList []string, url string, newStatus int) error {
|
||||
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)
|
||||
|
||||
op.Url = url
|
||||
op.NewStatus = newStatus
|
||||
_, err = this.Save(op)
|
||||
|
||||
// TODO 修改相关引用的对象
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// 组合配置
|
||||
func (this *HTTPPageDAO) ComposePageConfig(pageId int64) (*serverconfigs.HTTPPageConfig, error) {
|
||||
page, err := this.FindEnabledHTTPPage(pageId)
|
||||
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
|
||||
|
||||
if len(page.StatusList) > 0 {
|
||||
statusList := []string{}
|
||||
err = json.Unmarshal([]byte(page.StatusList), &statusList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(statusList) > 0 {
|
||||
config.Status = statusList
|
||||
}
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
5
internal/db/models/http_page_dao_test.go
Normal file
5
internal/db/models/http_page_dao_test.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
30
internal/db/models/http_page_model.go
Normal file
30
internal/db/models/http_page_model.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package models
|
||||
|
||||
//
|
||||
type HTTPPage struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
UserId uint32 `field:"userId"` // 用户ID
|
||||
IsOn uint8 `field:"isOn"` // 是否启用
|
||||
StatusList string `field:"statusList"` // 状态列表
|
||||
Url string `field:"url"` // 页面URL
|
||||
NewStatus int32 `field:"newStatus"` // 新状态码
|
||||
State uint8 `field:"state"` // 状态
|
||||
CreatedAt uint32 `field:"createdAt"` // 创建时间
|
||||
}
|
||||
|
||||
type HTTPPageOperator struct {
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
UserId interface{} // 用户ID
|
||||
IsOn interface{} // 是否启用
|
||||
StatusList interface{} // 状态列表
|
||||
Url interface{} // 页面URL
|
||||
NewStatus interface{} // 新状态码
|
||||
State interface{} // 状态
|
||||
CreatedAt interface{} // 创建时间
|
||||
}
|
||||
|
||||
func NewHTTPPageOperator() *HTTPPageOperator {
|
||||
return &HTTPPageOperator{}
|
||||
}
|
||||
1
internal/db/models/http_page_model_ext.go
Normal file
1
internal/db/models/http_page_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
@@ -106,6 +107,35 @@ func (this *HTTPWebDAO) ComposeWebConfig(webId int64) (*serverconfigs.HTTPWebCon
|
||||
}
|
||||
}
|
||||
|
||||
// shutdown
|
||||
if IsNotNull(web.Shutdown) {
|
||||
shutdownConfig := &serverconfigs.HTTPShutdownConfig{}
|
||||
err = json.Unmarshal([]byte(web.Shutdown), shutdownConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Shutdown = shutdownConfig
|
||||
}
|
||||
|
||||
// pages
|
||||
if IsNotNull(web.Pages) {
|
||||
pages := []*serverconfigs.HTTPPageConfig{}
|
||||
err = json.Unmarshal([]byte(web.Pages), &pages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for index, page := range pages {
|
||||
pageConfig, err := SharedHTTPPageDAO.ComposePageConfig(page.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pages[index] = pageConfig
|
||||
}
|
||||
if len(pages) > 0 {
|
||||
config.Pages = pages
|
||||
}
|
||||
}
|
||||
|
||||
// TODO 更多配置
|
||||
|
||||
return config, nil
|
||||
@@ -172,7 +202,7 @@ func (this *HTTPWebDAO) UpdateWebCharset(webId int64, charset string) error {
|
||||
}
|
||||
|
||||
// 更改请求Header策略
|
||||
func (this *HTTPWebDAO) UpdateHTTPWebRequestHeaderPolicy(webId int64, headerPolicyId int64) error {
|
||||
func (this *HTTPWebDAO) UpdateWebRequestHeaderPolicy(webId int64, headerPolicyId int64) error {
|
||||
if webId <= 0 {
|
||||
return errors.New("invalid webId")
|
||||
}
|
||||
@@ -188,7 +218,7 @@ func (this *HTTPWebDAO) UpdateHTTPWebRequestHeaderPolicy(webId int64, headerPoli
|
||||
}
|
||||
|
||||
// 更改响应Header策略
|
||||
func (this *HTTPWebDAO) UpdateHTTPWebResponseHeaderPolicy(webId int64, headerPolicyId int64) error {
|
||||
func (this *HTTPWebDAO) UpdateWebResponseHeaderPolicy(webId int64, headerPolicyId int64) error {
|
||||
if webId <= 0 {
|
||||
return errors.New("invalid webId")
|
||||
}
|
||||
@@ -203,6 +233,38 @@ func (this *HTTPWebDAO) UpdateHTTPWebResponseHeaderPolicy(webId int64, headerPol
|
||||
return this.NotifyUpdating(webId)
|
||||
}
|
||||
|
||||
// 更改特殊页面配置
|
||||
func (this *HTTPWebDAO) UpdateWebPages(webId int64, pagesJSON []byte) error {
|
||||
if webId <= 0 {
|
||||
return errors.New("invalid webId")
|
||||
}
|
||||
op := NewHTTPWebOperator()
|
||||
op.Id = webId
|
||||
op.Pages = JSONBytes(pagesJSON)
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdating(webId)
|
||||
}
|
||||
|
||||
// 更改Shutdown配置
|
||||
func (this *HTTPWebDAO) UpdateWebShutdown(webId int64, shutdownJSON []byte) error {
|
||||
if webId <= 0 {
|
||||
return errors.New("invalid webId")
|
||||
}
|
||||
op := NewHTTPWebOperator()
|
||||
op.Id = webId
|
||||
op.Shutdown = JSONBytes(shutdownJSON)
|
||||
_, err := this.Save(op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.NotifyUpdating(webId)
|
||||
}
|
||||
|
||||
// 通知更新
|
||||
func (this *HTTPWebDAO) NotifyUpdating(webId int64) error {
|
||||
err := SharedServerDAO.UpdateServerIsUpdatingWithWebId(webId)
|
||||
|
||||
@@ -2,4 +2,23 @@ package models
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHTTPWebDAO_UpdateWebShutdown(t *testing.T) {
|
||||
{
|
||||
err := SharedHTTPWebDAO.UpdateWebShutdown(1, []byte("{}"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
err := SharedHTTPWebDAO.UpdateWebShutdown(1, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
14
internal/db/models/utils.go
Normal file
14
internal/db/models/utils.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
// 处理JSON字节Slice
|
||||
func JSONBytes(data []byte) []byte {
|
||||
if len(data) == 0 {
|
||||
return []byte("null")
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// 判断JSON是否为空
|
||||
func IsNotNull(data string) bool {
|
||||
return len(data) > 0 && data != "null"
|
||||
}
|
||||
Reference in New Issue
Block a user