diff --git a/router/http-router/config.go b/router/http-router/config.go index 987230c5..64cf9af9 100644 --- a/router/http-router/config.go +++ b/router/http-router/config.go @@ -7,10 +7,10 @@ import ( ) type DriverConfig struct { - //ID string `json:"id"` - //Name string `json:"name" yaml:"name"` + Driver string `json:"driver" yaml:"driver"` Listen int `json:"listen" yaml:"listen"` + Method []string `json:"method" yaml:"method"` Host []string `json:"host" yaml:"host"` Rules []DriverRule `json:"rules" yaml:"rules"` diff --git a/router/http-router/router.go b/router/http-router/router.go index fabd3508..b4db9a84 100644 --- a/router/http-router/router.go +++ b/router/http-router/router.go @@ -78,20 +78,33 @@ func getConfig(target service.IService, cf *DriverConfig) *router_http.Config { } 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{ //Id: cf.ID, //Name: cf.Name, - Hosts: cf.Host, + Methods: methods, + Hosts: hosts, Target: target, Rules: rules, } } func NewRouter(id, name string, c *DriverConfig, target service.IService) *Router { + conf:= getConfig(target, c) + conf.Id = id + conf.Name = name + return &Router{ id: id, name: name, port: c.Listen, - conf: getConfig(target, c), + conf: conf, } } diff --git a/router/router-http/cmd.go b/router/router-http/cmd.go index b906d657..46079eef 100644 --- a/router/router-http/cmd.go +++ b/router/router-http/cmd.go @@ -19,8 +19,12 @@ const ( cmdHeader = "HEADER" cmdQuery = "QUERY" cmdHost = "HOST" + cmdMethod = "METHOD" ) +func toMethod()string { + return cmdMethod +} func toLocation() string { return cmdLocation } @@ -54,3 +58,6 @@ func isHost(cmd string) bool { return cmd == cmdHost } +func isMethod(cmd string)bool { + return cmd == cmdMethod +} diff --git a/router/router-http/config.go b/router/router-http/config.go index d2522168..a8e78328 100644 --- a/router/router-http/config.go +++ b/router/router-http/config.go @@ -26,6 +26,7 @@ type Config struct { Id string Name string Hosts []string + Methods []string Target service.IService Rules []Rule } diff --git a/router/router-http/match.go b/router/router-http/match.go index 7fa8a961..d3ab2569 100644 --- a/router/router-http/match.go +++ b/router/router-http/match.go @@ -43,12 +43,16 @@ func newHttpSources(req *http.Request) *HttpSources { } func (h *HttpSources) Get(cmd string) (string, bool) { + if isHost(cmd) { return h.req.Host, true } + if isMethod(cmd){ + return h.req.Method,true + } if isLocation(cmd) { - return h.req.RequestURI, true + return h.req.URL.Path, true } if hn, yes := headerName(cmd); yes { if vs, has := h.req.Header[hn]; has { diff --git a/router/router-http/parse.go b/router/router-http/parse.go index e7cfbe09..54bf8e07 100644 --- a/router/router-http/parse.go +++ b/router/router-http/parse.go @@ -10,13 +10,14 @@ func parse(cs []*Config) (IMatcher, error) { count:=0 for i:=range cs{ - hsize := len(cs[i].Hosts) - if hsize <1{ - hsize = 1 - } - count += len(cs[i].Rules)*hsize + hSize := len(cs[i].Hosts) + mSize := len(cs[i].Methods) + + count += len(cs[i].Rules)*hSize*mSize } + + rules :=make([]router.Rule,0,count) targets :=make(map[string]service.IService) @@ -34,6 +35,17 @@ func parse(cs []*Config) (IMatcher, error) { 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 for _,r:=range c.Rules{ @@ -41,20 +53,15 @@ func parse(cs []*Config) (IMatcher, error) { if err!= nil{ return nil,err } - if len(hosts) >0{ - for _,hp:=range hosts{ - pathWithHost := append(make([]router.RulePath,0,len(path)+1),hp) + for _,hp:=range hosts{ + for _,mp:=range methods{ + pathWithHost := append(make([]router.RulePath,0,len(path)+2),hp,mp) pathWithHost = append(pathWithHost,path...) rules = append(rules,router.Rule{ Path:pathWithHost, Target:c.Id, } ) } - }else{ - rules = append(rules, router.Rule{ - Path:path, - Target:c.Id, - } ) } } }