router 驱动

This commit is contained in:
黄孟柱
2021-07-23 19:57:32 +08:00
parent fd21628fa6
commit f8a7e0abe6
15 changed files with 259 additions and 92 deletions

View File

@@ -2,6 +2,7 @@ package router_http
import (
"github.com/eolinker/goku-eosc/router"
"github.com/eolinker/goku-eosc/router/checker"
"github.com/eolinker/goku-eosc/service"
)
@@ -9,16 +10,30 @@ func parse(cs []*Config) (IMatcher, error) {
count:=0
for i:=range cs{
count += len(cs[i].Rules)
hsize := len(cs[i].Hosts)
if hsize <1{
hsize = 1
}
count += len(cs[i].Rules)*len(cs[i].Hosts)
}
rules :=make([]router.Rule,0,count)
targets :=make(map[string]service.IService)
for _,c:=range cs{
hosts:=make([]router.RulePath,0,len(c.Hosts))
for _,h:=range c.Hosts{
hck,e:= checker.Parse(h)
if e!= nil{
return nil,e
}
hosts = append(hosts, router.RulePath{
CMD: toHost(),
Checker: hck,
})
}
targets[c.Id]=c.Target
for _,r:=range c.Rules{
@@ -26,10 +41,21 @@ func parse(cs []*Config) (IMatcher, error) {
if err!= nil{
return nil,err
}
rules = append(rules, router.Rule{
Path:path,
Target:c.Id,
} )
if len(hosts) >0{
for _,hp:=range hosts{
pathWidthHost := append(make([]router.RulePath,0,len(path)+1),hp)
pathWidthHost = append(pathWidthHost,path...)
rules = append(rules,router.Rule{
Path:path,
Target:c.Id,
} )
}
}else{
rules = append(rules, router.Rule{
Path:path,
Target:c.Id,
} )
}
}
}
r,err:=router.ParseRouter(rules,NewHttpRouterHelper())