route wildcards suppoort substr
This commit is contained in:
14
tree.go
14
tree.go
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -57,6 +58,19 @@ func (t *tree) Lookup(path string) http.HandlerFunc {
|
||||
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
|
||||
}
|
||||
|
||||
12
tree_test.go
12
tree_test.go
@@ -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) {
|
||||
tree := newTree()
|
||||
checked := false
|
||||
|
||||
Reference in New Issue
Block a user