Only allow a single matcher per route

This commit is contained in:
Martin Polden 2018-03-19 19:44:14 +01:00
parent 055496906d
commit b01bddb63e
1 changed files with 10 additions and 17 deletions

View File

@ -10,11 +10,11 @@ type router struct {
} }
type route struct { 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
} }