路由规则增加专属域名设置

This commit is contained in:
刘祥超
2021-12-12 16:38:31 +08:00
parent c67fbfa849
commit 3ce1aa14b5
3 changed files with 38 additions and 6 deletions

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -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
}