router 增加 method

This commit is contained in:
黄孟柱
2021-07-27 18:32:49 +08:00
parent 907417d5d3
commit 6f368b76ef
6 changed files with 50 additions and 18 deletions

View File

@@ -7,10 +7,10 @@ import (
) )
type DriverConfig struct { type DriverConfig struct {
//ID string `json:"id"`
//Name string `json:"name" yaml:"name"`
Driver string `json:"driver" yaml:"driver"` Driver string `json:"driver" yaml:"driver"`
Listen int `json:"listen" yaml:"listen"` Listen int `json:"listen" yaml:"listen"`
Method []string `json:"method" yaml:"method"`
Host []string `json:"host" yaml:"host"` Host []string `json:"host" yaml:"host"`
Rules []DriverRule `json:"rules" yaml:"rules"` Rules []DriverRule `json:"rules" yaml:"rules"`

View File

@@ -78,20 +78,33 @@ func getConfig(target service.IService, cf *DriverConfig) *router_http.Config {
} }
rules = append(rules, rr) rules = append(rules, rr)
} }
hosts := cf.Host
if len(hosts) == 0{
hosts = []string{"*"}
}
methods:= cf.Method
if len(methods) == 0{
methods = []string{"*"}
}
return &router_http.Config{ return &router_http.Config{
//Id: cf.ID, //Id: cf.ID,
//Name: cf.Name, //Name: cf.Name,
Hosts: cf.Host, Methods: methods,
Hosts: hosts,
Target: target, Target: target,
Rules: rules, Rules: rules,
} }
} }
func NewRouter(id, name string, c *DriverConfig, target service.IService) *Router { func NewRouter(id, name string, c *DriverConfig, target service.IService) *Router {
conf:= getConfig(target, c)
conf.Id = id
conf.Name = name
return &Router{ return &Router{
id: id, id: id,
name: name, name: name,
port: c.Listen, port: c.Listen,
conf: getConfig(target, c), conf: conf,
} }
} }

View File

@@ -19,8 +19,12 @@ const (
cmdHeader = "HEADER" cmdHeader = "HEADER"
cmdQuery = "QUERY" cmdQuery = "QUERY"
cmdHost = "HOST" cmdHost = "HOST"
cmdMethod = "METHOD"
) )
func toMethod()string {
return cmdMethod
}
func toLocation() string { func toLocation() string {
return cmdLocation return cmdLocation
} }
@@ -54,3 +58,6 @@ func isHost(cmd string) bool {
return cmd == cmdHost return cmd == cmdHost
} }
func isMethod(cmd string)bool {
return cmd == cmdMethod
}

View File

@@ -26,6 +26,7 @@ type Config struct {
Id string Id string
Name string Name string
Hosts []string Hosts []string
Methods []string
Target service.IService Target service.IService
Rules []Rule Rules []Rule
} }

View File

@@ -43,12 +43,16 @@ func newHttpSources(req *http.Request) *HttpSources {
} }
func (h *HttpSources) Get(cmd string) (string, bool) { func (h *HttpSources) Get(cmd string) (string, bool) {
if isHost(cmd) { if isHost(cmd) {
return h.req.Host, true return h.req.Host, true
} }
if isMethod(cmd){
return h.req.Method,true
}
if isLocation(cmd) { if isLocation(cmd) {
return h.req.RequestURI, true return h.req.URL.Path, true
} }
if hn, yes := headerName(cmd); yes { if hn, yes := headerName(cmd); yes {
if vs, has := h.req.Header[hn]; has { if vs, has := h.req.Header[hn]; has {

View File

@@ -10,13 +10,14 @@ func parse(cs []*Config) (IMatcher, error) {
count:=0 count:=0
for i:=range cs{ for i:=range cs{
hsize := len(cs[i].Hosts) hSize := len(cs[i].Hosts)
if hsize <1{ mSize := len(cs[i].Methods)
hsize = 1
} count += len(cs[i].Rules)*hSize*mSize
count += len(cs[i].Rules)*hsize
} }
rules :=make([]router.Rule,0,count) rules :=make([]router.Rule,0,count)
targets :=make(map[string]service.IService) targets :=make(map[string]service.IService)
@@ -34,6 +35,17 @@ func parse(cs []*Config) (IMatcher, error) {
Checker: hck, Checker: hck,
}) })
} }
methods:=make([]router.RulePath,0,len(c.Methods))
for _,m:=range c.Methods{
mck,e:= checker.Parse(m)
if e!= nil{
return nil,e
}
methods = append(methods, router.RulePath{
CMD: toMethod(),
Checker: mck,
})
}
targets[c.Id]=c.Target targets[c.Id]=c.Target
for _,r:=range c.Rules{ for _,r:=range c.Rules{
@@ -41,20 +53,15 @@ func parse(cs []*Config) (IMatcher, error) {
if err!= nil{ if err!= nil{
return nil,err return nil,err
} }
if len(hosts) >0{
for _,hp:=range hosts{ for _,hp:=range hosts{
pathWithHost := append(make([]router.RulePath,0,len(path)+1),hp) for _,mp:=range methods{
pathWithHost := append(make([]router.RulePath,0,len(path)+2),hp,mp)
pathWithHost = append(pathWithHost,path...) pathWithHost = append(pathWithHost,path...)
rules = append(rules,router.Rule{ rules = append(rules,router.Rule{
Path:pathWithHost, Path:pathWithHost,
Target:c.Id, Target:c.Id,
} ) } )
} }
}else{
rules = append(rules, router.Rule{
Path:path,
Target:c.Id,
} )
} }
} }
} }