Skip to content

Commit 9e74baf

Browse files
committed
路径规则、重写规则、URL跳转规则均支持匹配条件
1 parent b651d94 commit 9e74baf

4 files changed

Lines changed: 85 additions & 40 deletions

File tree

internal/db/models/http_location_dao.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
7+
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
78
_ "github.com/go-sql-driver/mysql"
89
"github.com/iwind/TeaGo/Tea"
910
"github.com/iwind/TeaGo/dbs"
@@ -36,12 +37,12 @@ func init() {
3637
})
3738
}
3839

39-
// 初始化
40+
// Init 初始化
4041
func (this *HTTPLocationDAO) Init() {
4142
_ = this.DAOObject.Init()
4243
}
4344

44-
// 启用条目
45+
// EnableHTTPLocation 启用条目
4546
func (this *HTTPLocationDAO) EnableHTTPLocation(tx *dbs.Tx, id int64) error {
4647
_, err := this.Query(tx).
4748
Pk(id).
@@ -50,7 +51,7 @@ func (this *HTTPLocationDAO) EnableHTTPLocation(tx *dbs.Tx, id int64) error {
5051
return err
5152
}
5253

53-
// 禁用条目
54+
// DisableHTTPLocation 禁用条目
5455
func (this *HTTPLocationDAO) DisableHTTPLocation(tx *dbs.Tx, locationId int64) error {
5556
_, err := this.Query(tx).
5657
Pk(locationId).
@@ -62,7 +63,7 @@ func (this *HTTPLocationDAO) DisableHTTPLocation(tx *dbs.Tx, locationId int64) e
6263
return this.NotifyUpdate(tx, locationId)
6364
}
6465

65-
// 查找启用中的条目
66+
// FindEnabledHTTPLocation 查找启用中的条目
6667
func (this *HTTPLocationDAO) FindEnabledHTTPLocation(tx *dbs.Tx, id int64) (*HTTPLocation, error) {
6768
result, err := this.Query(tx).
6869
Pk(id).
@@ -74,16 +75,16 @@ func (this *HTTPLocationDAO) FindEnabledHTTPLocation(tx *dbs.Tx, id int64) (*HTT
7475
return result.(*HTTPLocation), err
7576
}
7677

77-
// 根据主键查找名称
78+
// FindHTTPLocationName 根据主键查找名称
7879
func (this *HTTPLocationDAO) FindHTTPLocationName(tx *dbs.Tx, id int64) (string, error) {
7980
return this.Query(tx).
8081
Pk(id).
8182
Result("name").
8283
FindStringCol("")
8384
}
8485

85-
// 创建路径规则
86-
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool) (int64, error) {
86+
// CreateLocation 创建路径规则
87+
func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte) (int64, error) {
8788
op := NewHTTPLocationOperator()
8889
op.IsOn = true
8990
op.State = HTTPLocationStateEnabled
@@ -92,15 +93,20 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str
9293
op.Pattern = pattern
9394
op.Description = description
9495
op.IsBreak = isBreak
96+
97+
if len(condsJSON) > 0 {
98+
op.Conds = condsJSON
99+
}
100+
95101
err := this.Save(tx, op)
96102
if err != nil {
97103
return 0, err
98104
}
99105
return types.Int64(op.Id), nil
100106
}
101107

102-
// 修改路径规则
103-
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool) error {
108+
// UpdateLocation 修改路径规则
109+
func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte) error {
104110
if locationId <= 0 {
105111
return errors.New("invalid locationId")
106112
}
@@ -111,14 +117,19 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s
111117
op.Description = description
112118
op.IsOn = isOn
113119
op.IsBreak = isBreak
120+
121+
if len(condsJSON) > 0 {
122+
op.Conds = condsJSON
123+
}
124+
114125
err := this.Save(tx, op)
115126
if err != nil {
116127
return err
117128
}
118129
return this.NotifyUpdate(tx, locationId)
119130
}
120131

121-
// 组合配置
132+
// ComposeLocationConfig 组合配置
122133
func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64) (*serverconfigs.HTTPLocationConfig, error) {
123134
location, err := this.FindEnabledHTTPLocation(tx, locationId)
124135
if err != nil {
@@ -163,10 +174,20 @@ func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64)
163174
}
164175
}
165176

177+
// conds
178+
if len(location.Conds) > 0 {
179+
conds := &shared.HTTPRequestCondsConfig{}
180+
err = json.Unmarshal([]byte(location.Conds), conds)
181+
if err != nil {
182+
return nil, err
183+
}
184+
config.Conds = conds
185+
}
186+
166187
return config, nil
167188
}
168189

169-
// 查找反向代理设置
190+
// FindLocationReverseProxy 查找反向代理设置
170191
func (this *HTTPLocationDAO) FindLocationReverseProxy(tx *dbs.Tx, locationId int64) (*serverconfigs.ReverseProxyRef, error) {
171192
refString, err := this.Query(tx).
172193
Pk(locationId).
@@ -186,7 +207,7 @@ func (this *HTTPLocationDAO) FindLocationReverseProxy(tx *dbs.Tx, locationId int
186207
return nil, nil
187208
}
188209

189-
// 更改反向代理设置
210+
// UpdateLocationReverseProxy 更改反向代理设置
190211
func (this *HTTPLocationDAO) UpdateLocationReverseProxy(tx *dbs.Tx, locationId int64, reverseProxyJSON []byte) error {
191212
if locationId <= 0 {
192213
return errors.New("invalid locationId")
@@ -201,7 +222,7 @@ func (this *HTTPLocationDAO) UpdateLocationReverseProxy(tx *dbs.Tx, locationId i
201222
return this.NotifyUpdate(tx, locationId)
202223
}
203224

204-
// 查找WebId
225+
// FindLocationWebId 查找WebId
205226
func (this *HTTPLocationDAO) FindLocationWebId(tx *dbs.Tx, locationId int64) (int64, error) {
206227
webId, err := this.Query(tx).
207228
Pk(locationId).
@@ -210,7 +231,7 @@ func (this *HTTPLocationDAO) FindLocationWebId(tx *dbs.Tx, locationId int64) (in
210231
return int64(webId), err
211232
}
212233

213-
// 更改Web设置
234+
// UpdateLocationWeb 更改Web设置
214235
func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, webId int64) error {
215236
if locationId <= 0 {
216237
return errors.New("invalid locationId")
@@ -225,7 +246,7 @@ func (this *HTTPLocationDAO) UpdateLocationWeb(tx *dbs.Tx, locationId int64, web
225246
return this.NotifyUpdate(tx, locationId)
226247
}
227248

228-
// 转换引用为配置
249+
// ConvertLocationRefs 转换引用为配置
229250
func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfigs.HTTPLocationRef) (locations []*serverconfigs.HTTPLocationConfig, err error) {
230251
for _, ref := range refs {
231252
config, err := this.ComposeLocationConfig(tx, ref.LocationId)
@@ -243,7 +264,7 @@ func (this *HTTPLocationDAO) ConvertLocationRefs(tx *dbs.Tx, refs []*serverconfi
243264
return
244265
}
245266

246-
// 根据WebId查找LocationId
267+
// FindEnabledLocationIdWithWebId 根据WebId查找LocationId
247268
func (this *HTTPLocationDAO) FindEnabledLocationIdWithWebId(tx *dbs.Tx, webId int64) (locationId int64, err error) {
248269
if webId <= 0 {
249270
return
@@ -254,7 +275,7 @@ func (this *HTTPLocationDAO) FindEnabledLocationIdWithWebId(tx *dbs.Tx, webId in
254275
FindInt64Col(0)
255276
}
256277

257-
// 通知更新
278+
// NotifyUpdate 通知更新
258279
func (this *HTTPLocationDAO) NotifyUpdate(tx *dbs.Tx, locationId int64) error {
259280
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithLocationId(tx, locationId)
260281
if err != nil {

internal/db/models/http_rewrite_rule_dao.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package models
22

33
import (
4+
"encoding/json"
45
"errors"
56
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
7+
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
68
_ "github.com/go-sql-driver/mysql"
79
"github.com/iwind/TeaGo/Tea"
810
"github.com/iwind/TeaGo/dbs"
@@ -35,12 +37,12 @@ func init() {
3537
})
3638
}
3739

38-
// 初始化
40+
// Init 初始化
3941
func (this *HTTPRewriteRuleDAO) Init() {
4042
_ = this.DAOObject.Init()
4143
}
4244

43-
// 启用条目
45+
// EnableHTTPRewriteRule 启用条目
4446
func (this *HTTPRewriteRuleDAO) EnableHTTPRewriteRule(tx *dbs.Tx, id int64) error {
4547
_, err := this.Query(tx).
4648
Pk(id).
@@ -49,7 +51,7 @@ func (this *HTTPRewriteRuleDAO) EnableHTTPRewriteRule(tx *dbs.Tx, id int64) erro
4951
return err
5052
}
5153

52-
// 禁用条目
54+
// DisableHTTPRewriteRule 禁用条目
5355
func (this *HTTPRewriteRuleDAO) DisableHTTPRewriteRule(tx *dbs.Tx, rewriteRuleId int64) error {
5456
_, err := this.Query(tx).
5557
Pk(rewriteRuleId).
@@ -61,7 +63,7 @@ func (this *HTTPRewriteRuleDAO) DisableHTTPRewriteRule(tx *dbs.Tx, rewriteRuleId
6163
return this.NotifyUpdate(tx, rewriteRuleId)
6264
}
6365

64-
// 查找启用中的条目
66+
// FindEnabledHTTPRewriteRule 查找启用中的条目
6567
func (this *HTTPRewriteRuleDAO) FindEnabledHTTPRewriteRule(tx *dbs.Tx, id int64) (*HTTPRewriteRule, error) {
6668
result, err := this.Query(tx).
6769
Pk(id).
@@ -73,7 +75,7 @@ func (this *HTTPRewriteRuleDAO) FindEnabledHTTPRewriteRule(tx *dbs.Tx, id int64)
7375
return result.(*HTTPRewriteRule), err
7476
}
7577

76-
// 构造配置
78+
// ComposeRewriteRule 构造配置
7779
func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int64) (*serverconfigs.HTTPRewriteRule, error) {
7880
rule, err := this.FindEnabledHTTPRewriteRule(tx, rewriteRuleId)
7981
if err != nil {
@@ -93,11 +95,21 @@ func (this *HTTPRewriteRuleDAO) ComposeRewriteRule(tx *dbs.Tx, rewriteRuleId int
9395
config.ProxyHost = rule.ProxyHost
9496
config.IsBreak = rule.IsBreak == 1
9597
config.WithQuery = rule.WithQuery == 1
98+
99+
// conds
100+
if len(rule.Conds) > 0 {
101+
conds := &shared.HTTPRequestCondsConfig{}
102+
err = json.Unmarshal([]byte(rule.Conds), conds)
103+
if err != nil {
104+
return nil, err
105+
}
106+
config.Conds = conds
107+
}
96108
return config, nil
97109
}
98110

99-
// 创建规则
100-
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool) (int64, error) {
111+
// CreateRewriteRule 创建规则
112+
func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool, condsJSON []byte) (int64, error) {
101113
op := NewHTTPRewriteRuleOperator()
102114
op.State = HTTPRewriteRuleStateEnabled
103115
op.IsOn = isOn
@@ -109,12 +121,17 @@ func (this *HTTPRewriteRuleDAO) CreateRewriteRule(tx *dbs.Tx, pattern string, re
109121
op.IsBreak = isBreak
110122
op.WithQuery = withQuery
111123
op.ProxyHost = proxyHost
124+
125+
if len(condsJSON) > 0 {
126+
op.Conds = condsJSON
127+
}
128+
112129
err := this.Save(tx, op)
113130
return types.Int64(op.Id), err
114131
}
115132

116-
// 修改规则
117-
func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int64, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool) error {
133+
// UpdateRewriteRule 修改规则
134+
func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int64, pattern string, replace string, mode string, redirectStatus int, isBreak bool, proxyHost string, withQuery bool, isOn bool, condsJSON []byte) error {
118135
if rewriteRuleId <= 0 {
119136
return errors.New("invalid rewriteRuleId")
120137
}
@@ -128,14 +145,19 @@ func (this *HTTPRewriteRuleDAO) UpdateRewriteRule(tx *dbs.Tx, rewriteRuleId int6
128145
op.IsBreak = isBreak
129146
op.WithQuery = withQuery
130147
op.ProxyHost = proxyHost
148+
149+
if len(condsJSON) > 0 {
150+
op.Conds = condsJSON
151+
}
152+
131153
err := this.Save(tx, op)
132154
if err != nil {
133155
return err
134156
}
135157
return this.NotifyUpdate(tx, rewriteRuleId)
136158
}
137159

138-
// 通知更新
160+
// NotifyUpdate 通知更新
139161
func (this *HTTPRewriteRuleDAO) NotifyUpdate(tx *dbs.Tx, rewriteRuleId int64) error {
140162
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithRewriteRuleId(tx, rewriteRuleId)
141163
if err != nil {

internal/rpc/services/service_http_location.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
1010
)
1111

12+
// HTTPLocationService 路径规则相关服务
1213
type HTTPLocationService struct {
1314
BaseService
1415
}
1516

16-
// 创建路径规则
17+
// CreateHTTPLocation 创建路径规则
1718
func (this *HTTPLocationService) CreateHTTPLocation(ctx context.Context, req *pb.CreateHTTPLocationRequest) (*pb.CreateHTTPLocationResponse, error) {
1819
// 校验请求
1920
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -23,15 +24,15 @@ func (this *HTTPLocationService) CreateHTTPLocation(ctx context.Context, req *pb
2324

2425
tx := this.NullTx()
2526

26-
locationId, err := models.SharedHTTPLocationDAO.CreateLocation(tx, req.ParentId, req.Name, req.Pattern, req.Description, req.IsBreak)
27+
locationId, err := models.SharedHTTPLocationDAO.CreateLocation(tx, req.ParentId, req.Name, req.Pattern, req.Description, req.IsBreak, req.CondsJSON)
2728
if err != nil {
2829
return nil, err
2930
}
3031

3132
return &pb.CreateHTTPLocationResponse{LocationId: locationId}, nil
3233
}
3334

34-
// 修改路径规则
35+
// UpdateHTTPLocation 修改路径规则
3536
func (this *HTTPLocationService) UpdateHTTPLocation(ctx context.Context, req *pb.UpdateHTTPLocationRequest) (*pb.RPCSuccess, error) {
3637
// 校验请求
3738
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -41,15 +42,15 @@ func (this *HTTPLocationService) UpdateHTTPLocation(ctx context.Context, req *pb
4142

4243
tx := this.NullTx()
4344

44-
err = models.SharedHTTPLocationDAO.UpdateLocation(tx, req.LocationId, req.Name, req.Pattern, req.Description, req.IsOn, req.IsBreak)
45+
err = models.SharedHTTPLocationDAO.UpdateLocation(tx, req.LocationId, req.Name, req.Pattern, req.Description, req.IsOn, req.IsBreak, req.CondsJSON)
4546
if err != nil {
4647
return nil, err
4748
}
4849

4950
return this.Success()
5051
}
5152

52-
// 查找路径规则配置
53+
// FindEnabledHTTPLocationConfig 查找路径规则配置
5354
func (this *HTTPLocationService) FindEnabledHTTPLocationConfig(ctx context.Context, req *pb.FindEnabledHTTPLocationConfigRequest) (*pb.FindEnabledHTTPLocationConfigResponse, error) {
5455
// 校验请求
5556
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -70,7 +71,7 @@ func (this *HTTPLocationService) FindEnabledHTTPLocationConfig(ctx context.Conte
7071
return &pb.FindEnabledHTTPLocationConfigResponse{LocationJSON: configJSON}, nil
7172
}
7273

73-
// 删除路径规则
74+
// DeleteHTTPLocation 删除路径规则
7475
func (this *HTTPLocationService) DeleteHTTPLocation(ctx context.Context, req *pb.DeleteHTTPLocationRequest) (*pb.RPCSuccess, error) {
7576
// 校验请求
7677
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
@@ -87,7 +88,7 @@ func (this *HTTPLocationService) DeleteHTTPLocation(ctx context.Context, req *pb
8788
return this.Success()
8889
}
8990

90-
// 查找反向代理设置
91+
// FindAndInitHTTPLocationReverseProxyConfig 查找反向代理设置
9192
func (this *HTTPLocationService) FindAndInitHTTPLocationReverseProxyConfig(ctx context.Context, req *pb.FindAndInitHTTPLocationReverseProxyConfigRequest) (*pb.FindAndInitHTTPLocationReverseProxyConfigResponse, error) {
9293
// 校验请求
9394
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
@@ -140,7 +141,7 @@ func (this *HTTPLocationService) FindAndInitHTTPLocationReverseProxyConfig(ctx c
140141
}, nil
141142
}
142143

143-
// 初始化Web设置
144+
// FindAndInitHTTPLocationWebConfig 初始化Web设置
144145
func (this *HTTPLocationService) FindAndInitHTTPLocationWebConfig(ctx context.Context, req *pb.FindAndInitHTTPLocationWebConfigRequest) (*pb.FindAndInitHTTPLocationWebConfigResponse, error) {
145146
// 校验请求
146147
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
@@ -179,7 +180,7 @@ func (this *HTTPLocationService) FindAndInitHTTPLocationWebConfig(ctx context.Co
179180
}, nil
180181
}
181182

182-
// 修改反向代理设置
183+
// UpdateHTTPLocationReverseProxy 修改反向代理设置
183184
func (this *HTTPLocationService) UpdateHTTPLocationReverseProxy(ctx context.Context, req *pb.UpdateHTTPLocationReverseProxyRequest) (*pb.RPCSuccess, error) {
184185
// 校验请求
185186
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)

0 commit comments

Comments
 (0)