route wildcards suppoort substr

master
Bel LaPointe 2022-06-17 14:22:51 -06:00
parent 643defd2f3
commit 75cb4b469b
2 changed files with 26 additions and 0 deletions

14
tree.go
View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"net/http" "net/http"
"path" "path"
"regexp"
"strings" "strings"
) )
@ -57,6 +58,19 @@ func (t *tree) Lookup(path string) http.HandlerFunc {
foo(w, r) foo(w, r)
} }
} }
} else {
for k, n := range t.next {
if !strings.Contains(k, Wildcard) {
continue
}
re := regexp.MustCompile(strings.ReplaceAll(k, Wildcard, ".*"))
if re.MatchString(key) && n.handler != nil {
return func(w http.ResponseWriter, r *http.Request) {
r.Header.Add(WildcardHeader, key)
n.handler(w, r)
}
}
}
} }
return nil return nil
} }

View File

@ -82,6 +82,18 @@ func TestTreeInsertLookup(t *testing.T) {
} }
} }
func TestTreeWildcardSubstr(t *testing.T) {
tree := newTree()
foo := func(w http.ResponseWriter, r *http.Request) {
}
if err := tree.Insert("/prefix-"+Wildcard, foo); err != nil {
t.Fatal(err)
}
if foo := tree.Lookup("/prefix-suffix"); foo == nil {
t.Error(foo)
}
}
func TestTreeWildcard(t *testing.T) { func TestTreeWildcard(t *testing.T) {
tree := newTree() tree := newTree()
checked := false checked := false