From 3ce1aa14b53e9bcd08b6f2339bf5436f0685b6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 12 Dec 2021 16:38:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=AF=E7=94=B1=E8=A7=84=E5=88=99=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=93=E5=B1=9E=E5=9F=9F=E5=90=8D=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/http_location_dao.go | 38 +++++++++++++++++-- internal/db/models/http_location_model.go | 2 + .../rpc/services/service_http_location.go | 4 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/internal/db/models/http_location_dao.go b/internal/db/models/http_location_dao.go index 401f92c9..79277554 100644 --- a/internal/db/models/http_location_dao.go +++ b/internal/db/models/http_location_dao.go @@ -86,7 +86,7 @@ func (this *HTTPLocationDAO) FindHTTPLocationName(tx *dbs.Tx, id int64) (string, } // CreateLocation 创建路由规则 -func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte) (int64, error) { +func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name string, pattern string, description string, isBreak bool, condsJSON []byte, domains []string) (int64, error) { op := NewHTTPLocationOperator() op.IsOn = true op.State = HTTPLocationStateEnabled @@ -100,7 +100,16 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str op.Conds = condsJSON } - err := this.Save(tx, op) + if domains == nil { + domains = []string{} + } + domainsJSON, err := json.Marshal(domains) + if err != nil { + return 0, err + } + op.Domains = domainsJSON + + err = this.Save(tx, op) if err != nil { return 0, err } @@ -108,7 +117,7 @@ func (this *HTTPLocationDAO) CreateLocation(tx *dbs.Tx, parentId int64, name str } // UpdateLocation 修改路由规则 -func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte) error { +func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name string, pattern string, description string, isOn bool, isBreak bool, condsJSON []byte, domains []string) error { if locationId <= 0 { return errors.New("invalid locationId") } @@ -124,7 +133,16 @@ func (this *HTTPLocationDAO) UpdateLocation(tx *dbs.Tx, locationId int64, name s op.Conds = condsJSON } - err := this.Save(tx, op) + if domains == nil { + domains = []string{} + } + domainsJSON, err := json.Marshal(domains) + if err != nil { + return err + } + op.Domains = domainsJSON + + err = this.Save(tx, op) if err != nil { return err } @@ -195,6 +213,18 @@ func (this *HTTPLocationDAO) ComposeLocationConfig(tx *dbs.Tx, locationId int64, config.Conds = conds } + // domains + if len(location.Domains) > 0 { + var domains = []string{} + err = json.Unmarshal([]byte(location.Domains), &domains) + if err != nil { + return nil, err + } + if len(domains) > 0 { + config.Domains = domains + } + } + if cacheMap != nil { cacheMap.Put(cacheKey, config) } diff --git a/internal/db/models/http_location_model.go b/internal/db/models/http_location_model.go index 22895524..c315476c 100644 --- a/internal/db/models/http_location_model.go +++ b/internal/db/models/http_location_model.go @@ -18,6 +18,7 @@ type HTTPLocation struct { UrlPrefix string `field:"urlPrefix"` // URL前缀 IsBreak uint8 `field:"isBreak"` // 是否终止匹配 Conds string `field:"conds"` // 匹配条件 + Domains string `field:"domains"` // 专属域名 } type HTTPLocationOperator struct { @@ -37,6 +38,7 @@ type HTTPLocationOperator struct { UrlPrefix interface{} // URL前缀 IsBreak interface{} // 是否终止匹配 Conds interface{} // 匹配条件 + Domains interface{} // 专属域名 } func NewHTTPLocationOperator() *HTTPLocationOperator { diff --git a/internal/rpc/services/service_http_location.go b/internal/rpc/services/service_http_location.go index 08be7cc2..ab3a11b7 100644 --- a/internal/rpc/services/service_http_location.go +++ b/internal/rpc/services/service_http_location.go @@ -24,7 +24,7 @@ func (this *HTTPLocationService) CreateHTTPLocation(ctx context.Context, req *pb tx := this.NullTx() - locationId, err := models.SharedHTTPLocationDAO.CreateLocation(tx, req.ParentId, req.Name, req.Pattern, req.Description, req.IsBreak, req.CondsJSON) + locationId, err := models.SharedHTTPLocationDAO.CreateLocation(tx, req.ParentId, req.Name, req.Pattern, req.Description, req.IsBreak, req.CondsJSON, req.Domains) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func (this *HTTPLocationService) UpdateHTTPLocation(ctx context.Context, req *pb tx := this.NullTx() - err = models.SharedHTTPLocationDAO.UpdateLocation(tx, req.LocationId, req.Name, req.Pattern, req.Description, req.IsOn, req.IsBreak, req.CondsJSON) + err = models.SharedHTTPLocationDAO.UpdateLocation(tx, req.LocationId, req.Name, req.Pattern, req.Description, req.IsOn, req.IsBreak, req.CondsJSON, req.Domains) if err != nil { return nil, err }