mirror of
https://github.com/eolinker/apinto
synced 2025-10-19 15:14:32 +08:00
router 增加 method
This commit is contained in:
@@ -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"`
|
||||
|
||||
|
@@ -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,
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ type Config struct {
|
||||
Id string
|
||||
Name string
|
||||
Hosts []string
|
||||
Methods []string
|
||||
Target service.IService
|
||||
Rules []Rule
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
@@ -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 _,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,
|
||||
} )
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user