mirror of https://github.com/mpolden/echoip
Only allow a single matcher per route
This commit is contained in:
parent
055496906d
commit
b01bddb63e
|
@ -13,8 +13,8 @@ type route struct {
|
||||||
method string
|
method string
|
||||||
path string
|
path string
|
||||||
prefix bool
|
prefix bool
|
||||||
matcherFuncs []func(*http.Request) bool
|
|
||||||
handler appHandler
|
handler appHandler
|
||||||
|
matcherFunc func(*http.Request) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouter() *router {
|
func NewRouter() *router {
|
||||||
|
@ -48,15 +48,14 @@ func (r *router) Handler() http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *route) Header(header, value string) *route {
|
func (r *route) Header(header, value string) {
|
||||||
return r.MatcherFunc(func(req *http.Request) bool {
|
r.MatcherFunc(func(req *http.Request) bool {
|
||||||
return req.Header.Get(header) == value
|
return req.Header.Get(header) == value
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *route) MatcherFunc(f func(*http.Request) bool) *route {
|
func (r *route) MatcherFunc(f func(*http.Request) bool) {
|
||||||
r.matcherFuncs = append(r.matcherFuncs, f)
|
r.matcherFunc = f
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *route) match(req *http.Request) bool {
|
func (r *route) match(req *http.Request) bool {
|
||||||
|
@ -70,11 +69,5 @@ func (r *route) match(req *http.Request) bool {
|
||||||
} else if r.path != req.URL.Path {
|
} else if r.path != req.URL.Path {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
match := len(r.matcherFuncs) == 0
|
return r.matcherFunc == nil || r.matcherFunc(req)
|
||||||
for _, f := range r.matcherFuncs {
|
|
||||||
if match = f(req); match {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return match
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue