mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-26 00:40:25 +08:00
节点中实现重写规则
This commit is contained in:
@@ -29,6 +29,20 @@ func NewHTTPRewriteRuleDAO() *HTTPRewriteRuleDAO {
|
||||
|
||||
var SharedHTTPRewriteRuleDAO = NewHTTPRewriteRuleDAO()
|
||||
|
||||
// 初始化
|
||||
func (this *HTTPRewriteRuleDAO) Init() {
|
||||
this.DAOObject.Init()
|
||||
this.DAOObject.OnUpdate(func() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
})
|
||||
this.DAOObject.OnInsert(func() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
})
|
||||
this.DAOObject.OnDelete(func() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *HTTPRewriteRuleDAO) EnableHTTPRewriteRule(id int64) error {
|
||||
_, err := this.Query().
|
||||
@@ -78,11 +92,12 @@ func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(rewriteRuleId int64) (*server
|
||||
config.RedirectStatus = types.Int(rule.RedirectStatus)
|
||||
config.ProxyHost = rule.ProxyHost
|
||||
config.IsBreak = rule.IsBreak == 1
|
||||
config.WithQuery = rule.WithQuery == 1
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// 创建规则
|
||||
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, isOn bool) (int64, error) {
|
||||
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool) (int64, error) {
|
||||
op := NewHTTPRewriteRuleOperator()
|
||||
op.State = HTTPRewriteRuleStateEnabled
|
||||
op.IsOn = isOn
|
||||
@@ -92,13 +107,14 @@ func (this *HTTPRewriteRuleDAO) CreateRewriteRule(pattern string, replace string
|
||||
op.Mode = mode
|
||||
op.RedirectStatus = redirectStatus
|
||||
op.IsBreak = isBreak
|
||||
op.WithQuery = withQuery
|
||||
op.ProxyHost = proxyHost
|
||||
_, err := this.Save(op)
|
||||
return types.Int64(op.Id), err
|
||||
}
|
||||
|
||||
// 修改规则
|
||||
func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(rewriteRuleId int64, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, isOn bool) error {
|
||||
func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(rewriteRuleId int64, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool) error {
|
||||
if rewriteRuleId <= 0 {
|
||||
return errors.New("invalid rewriteRuleId")
|
||||
}
|
||||
@@ -110,6 +126,7 @@ func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(rewriteRuleId int64, pattern s
|
||||
op.Mode = mode
|
||||
op.RedirectStatus = redirectStatus
|
||||
op.IsBreak = isBreak
|
||||
op.WithQuery = withQuery
|
||||
op.ProxyHost = proxyHost
|
||||
_, err := this.Save(op)
|
||||
return err
|
||||
|
||||
@@ -15,6 +15,7 @@ type HTTPRewriteRule struct {
|
||||
RedirectStatus uint32 `field:"redirectStatus"` // 跳转的状态码
|
||||
ProxyHost string `field:"proxyHost"` // 代理的主机名
|
||||
IsBreak uint8 `field:"isBreak"` // 是否终止解析
|
||||
WithQuery uint8 `field:"withQuery"` // 是否保留URI参数
|
||||
}
|
||||
|
||||
type HTTPRewriteRuleOperator struct {
|
||||
@@ -31,6 +32,7 @@ type HTTPRewriteRuleOperator struct {
|
||||
RedirectStatus interface{} // 跳转的状态码
|
||||
ProxyHost interface{} // 代理的主机名
|
||||
IsBreak interface{} // 是否终止解析
|
||||
WithQuery interface{} // 是否保留URI参数
|
||||
}
|
||||
|
||||
func NewHTTPRewriteRuleOperator() *HTTPRewriteRuleOperator {
|
||||
|
||||
@@ -19,7 +19,7 @@ func (this *HTTPRewriteRuleService) CreateHTTPRewriteRule(ctx context.Context, r
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rewriteRuleId, err := models.SharedHTTPRewriteRuleDAO.CreateRewriteRule(req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.IsOn)
|
||||
rewriteRuleId, err := models.SharedHTTPRewriteRuleDAO.CreateRewriteRule(req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.WithQuery, req.IsOn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +35,7 @@ func (this *HTTPRewriteRuleService) UpdateHTTPRewriteRule(ctx context.Context, r
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedHTTPRewriteRuleDAO.UpdateRewriteRule(req.RewriteRuleId, req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.IsOn)
|
||||
err = models.SharedHTTPRewriteRuleDAO.UpdateRewriteRule(req.RewriteRuleId, req.Pattern, req.Replace, req.Mode, types.Int(req.RedirectStatus), req.IsBreak, req.ProxyHost, req.WithQuery, req.IsOn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user