mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-25 14:36:35 +08:00
阶段性提交
This commit is contained in:
102
internal/db/models/http_web_dao.go
Normal file
102
internal/db/models/http_web_dao.go
Normal file
@@ -0,0 +1,102 @@
|
||||
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.WebConfig, error) {
|
||||
web, err := SharedHTTPWebDAO.FindEnabledHTTPWeb(webId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if web == nil {
|
||||
return nil, nil
|
||||
}
|
||||
config := &serverconfigs.WebConfig{}
|
||||
config.IsOn = web.IsOn == 1
|
||||
config.Root = web.Root
|
||||
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)
|
||||
|
||||
// TODO 更新所有使用此Web配置的服务
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user