Skip to content

Commit 4fb032e

Browse files
authored
Merge pull request #8 from golobby/any-method
implement any method
2 parents d33f307 + 5c4c629 commit 4fb032e

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

radix.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ func (t *tree) searchByParts(parent *node, parts []string, position int, paramet
8181
return child
8282
}
8383

84-
return t.searchByParts(child, parts, position+1, parameters)
84+
if node := t.searchByParts(child, parts, position+1, parameters); node != nil {
85+
return node
86+
}
8587
}
8688
}
8789

@@ -106,16 +108,16 @@ func (t *tree) searchByName(node *node, name string) *node {
106108
}
107109

108110
// insert adds a new route to the radix tree by recursive traversing.
109-
func (t *tree) insert(parent, node *node, parts []string, index int) {
110-
isLeaf := index == len(parts)-1
111+
func (t *tree) insert(parent, node *node, parts []string, position int) {
112+
isLeaf := position == len(parts)-1
111113

112114
for i, child := range parent.Children {
113-
if child.content == parts[index] {
115+
if child.content == parts[position] {
114116
if isLeaf {
115117
node.Children = parent.Children[i].Children
116118
parent.Children[i] = node
117119
} else {
118-
t.insert(child, node, parts, index+1)
120+
t.insert(child, node, parts, position+1)
119121
}
120122
return
121123
}
@@ -124,9 +126,9 @@ func (t *tree) insert(parent, node *node, parts []string, index int) {
124126
if isLeaf {
125127
parent.Children = append(parent.Children, node)
126128
} else {
127-
newNode := newNode(nil, parts[index])
129+
newNode := newNode(nil, parts[position])
128130
parent.Children = append(parent.Children, newNode)
129-
t.insert(newNode, node, parts, index+1)
131+
t.insert(newNode, node, parts, position+1)
130132
}
131133
}
132134

router.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ func (r Router) Serve(rw http.ResponseWriter, request *http.Request) {
8686
r.director.ServeHTTP(rw, request)
8787
}
8888

89+
// Any maps a method-agnostic Route.
90+
func (r Router) Any(path string, handler Handler) *Route {
91+
return r.Map(":__METHOD__", path, handler)
92+
}
93+
8994
// GET maps a GET Route.
9095
func (r Router) GET(path string, handler Handler) *Route {
9196
return r.Map("GET", path, handler)

router_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,19 @@ func TestRouter_With_Different_HTTP_Methods(t *testing.T) {
9191
r.HEAD("/", handler)
9292
r.OPTIONS("/", handler)
9393
r.Map("CUSTOM", "/", handler)
94+
r.Any("/any", handler)
9495

9596
methods := []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "CUSTOM"}
9697
for _, m := range methods {
9798
rw := newResponse()
9899
r.Serve(rw, newRequest(m, "/"))
99100
assert.Equal(t, 200, rw.status)
100101
assert.Equal(t, m, rw.stringBody())
102+
103+
rw = newResponse()
104+
r.Serve(rw, newRequest(m, "/any"))
105+
assert.Equal(t, 200, rw.status)
106+
assert.Equal(t, m, rw.stringBody())
101107
}
102108

103109
rw := newResponse()

0 commit comments

Comments
 (0)