Browse Source

Allow regexps to take advantage of SubRouter

Go's regular expressions don't allow you to create a capturing group
named "*", which previously made using SubRouter with regular expression
patterns impossible. This change introduces the alternate key "_", which
happens to be a legal capturing group name.

Fixes #98.
Carl Jackson 11 years ago
parent
commit
863ba3029d
1 changed files with 10 additions and 1 deletions
  1. +10
    -1
      web/middleware/subrouter.go

+ 10
- 1
web/middleware/subrouter.go View File

@ -13,7 +13,11 @@ type subrouter struct {
func (s subrouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s subrouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.c.URLParams != nil { if s.c.URLParams != nil {
if path, ok := s.c.URLParams["*"]; ok {
path, ok := s.c.URLParams["*"]
if !ok {
path, ok = s.c.URLParams["_"]
}
if ok {
oldpath := r.URL.Path oldpath := r.URL.Path
r.URL.Path = path r.URL.Path = path
defer func() { defer func() {
@ -31,6 +35,11 @@ func (s subrouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// middleware will help you set the request URL's Path to this unmatched suffix, // middleware will help you set the request URL's Path to this unmatched suffix,
// allowing you to write sub-routers with no knowledge of what routes the parent // allowing you to write sub-routers with no knowledge of what routes the parent
// router matches. // router matches.
//
// Since Go's regular expressions do not allow you to create a capturing group
// named "*", SubRouter also accepts the string "_". For instance, to duplicate
// the semantics of the string pattern "/foo/*", you might use the regular
// expression "^/foo(?P<_>/.*)$".
func SubRouter(c *web.C, h http.Handler) http.Handler { func SubRouter(c *web.C, h http.Handler) http.Handler {
return subrouter{c, h} return subrouter{c, h}
} }

Loading…
Cancel
Save