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 初始化
4041func (this * HTTPLocationDAO ) Init () {
4142 _ = this .DAOObject .Init ()
4243}
4344
44- // 启用条目
45+ // EnableHTTPLocation 启用条目
4546func (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 禁用条目
5455func (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 查找启用中的条目
6667func (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 根据主键查找名称
7879func (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 组合配置
122133func (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 查找反向代理设置
170191func (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 更改反向代理设置
190211func (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
205226func (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设置
214235func (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 转换引用为配置
229250func (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
247268func (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 通知更新
258279func (this * HTTPLocationDAO ) NotifyUpdate (tx * dbs.Tx , locationId int64 ) error {
259280 webId , err := SharedHTTPWebDAO .FindEnabledWebIdWithLocationId (tx , locationId )
260281 if err != nil {
0 commit comments