Skip to content

Commit

Permalink
fix static router bug with trail slash
Browse files Browse the repository at this point in the history
  • Loading branch information
coldstar committed Dec 2, 2016
1 parent 73d616d commit a6c5976
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 1 addition & 3 deletions baa.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ func (b *Baa) Static(prefix string, dir string, index bool, h HandlerFunc) {
if dir == "" {
panic("baa.Static dir can not be empty")
}
staticHandler := newStatic(prefix, dir, index, h)
b.Get(prefix, staticHandler)
b.Get(prefix+"*", staticHandler)
b.Get(prefix+"*", newStatic(prefix, dir, index, h))
}

// StaticFile shortcut for serve file
Expand Down
22 changes: 13 additions & 9 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,19 @@ func (t *Tree) Match(method, pattern string, c *Context) []HandlerFunc {
}

if len(pattern) == 0 {
if current.handlers == nil {
if current.handlers != nil {
return current.handlers
}
if root.paramChild == nil && root.wideChild == nil {
return nil
}
return current.handlers
}

// children static route
if current == root {
if nl = root.children[pattern[0]]; nl != nil {
current = nl
continue
} else {
// children static route
if current == root {
if nl = root.children[pattern[0]]; nl != nil {
current = nl
continue
}
}
}

Expand Down Expand Up @@ -207,6 +209,8 @@ func (t *Tree) Add(method, pattern string, handlers []HandlerFunc) RouteNode {
if t.autoTrailingSlash && (len(pattern) > 1 || len(t.groups) > 0) {
if pattern[len(pattern)-1] == '/' {
t.add(method, pattern[:len(pattern)-1], handlers)
} else if pattern[len(pattern)-1] == '*' {
// wideChild not need trail slash
} else {
t.add(method, pattern+"/", handlers)
}
Expand Down

0 comments on commit a6c5976

Please sign in to comment.