if non wildcard is nil, try wildcard route

master
Bel LaPointe 2021-03-11 16:47:12 -06:00
parent 32326de6b2
commit 5deb0e5f0d
4 changed files with 24 additions and 4 deletions

0
router.go Normal file → Executable file
View File

0
router_test.go Normal file → Executable file
View File

28
tree.go Normal file → Executable file
View File

@ -24,12 +24,24 @@ func newTree() *tree {
func (t *tree) Lookup(path string) http.HandlerFunc {
if path == "/" || path == "" {
return t.handler
if t.handler != nil {
return t.handler
}
if n, ok := t.next[Wildcard+Wildcard]; t.handler == nil && ok {
foo := n.handler
return foo
}
return nil
}
key, following := nextPathSegment(path)
if n, ok := t.next[key]; ok {
return n.Lookup(following)
} else if n, ok := t.next[Wildcard]; ok {
n, ok := t.next[key]
if ok {
n2 := n.Lookup(following)
if n2 != nil {
return n2
}
}
if n, ok := t.next[Wildcard]; ok {
foo := n.Lookup(following)
if foo != nil {
return func(w http.ResponseWriter, r *http.Request) {
@ -37,6 +49,14 @@ func (t *tree) Lookup(path string) http.HandlerFunc {
foo(w, r)
}
}
} else if n, ok := t.next[Wildcard+Wildcard]; ok {
foo := n.handler
if foo != nil {
return func(w http.ResponseWriter, r *http.Request) {
r.Header.Add(WildcardHeader, key)
foo(w, r)
}
}
}
return nil
}

0
tree_test.go Normal file → Executable file
View File