|
|
|
@ -10,8 +10,6 @@ import ( |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"path" |
|
|
|
"path" |
|
|
|
"regexp" |
|
|
|
"regexp" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/context" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// NewRouter returns a new router instance.
|
|
|
|
// NewRouter returns a new router instance.
|
|
|
|
@ -50,7 +48,9 @@ type Router struct { |
|
|
|
strictSlash bool |
|
|
|
strictSlash bool |
|
|
|
// See Router.SkipClean(). This defines the flag for new routes.
|
|
|
|
// See Router.SkipClean(). This defines the flag for new routes.
|
|
|
|
skipClean bool |
|
|
|
skipClean bool |
|
|
|
// If true, do not clear the request context after handling the request
|
|
|
|
// If true, do not clear the request context after handling the request.
|
|
|
|
|
|
|
|
// This has no effect when go1.7+ is used, since the context is stored
|
|
|
|
|
|
|
|
// on the request itself.
|
|
|
|
KeepContext bool |
|
|
|
KeepContext bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -95,14 +95,14 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
|
|
|
var handler http.Handler |
|
|
|
var handler http.Handler |
|
|
|
if r.Match(req, &match) { |
|
|
|
if r.Match(req, &match) { |
|
|
|
handler = match.Handler |
|
|
|
handler = match.Handler |
|
|
|
setVars(req, match.Vars) |
|
|
|
req = setVars(req, match.Vars) |
|
|
|
setCurrentRoute(req, match.Route) |
|
|
|
req = setCurrentRoute(req, match.Route) |
|
|
|
} |
|
|
|
} |
|
|
|
if handler == nil { |
|
|
|
if handler == nil { |
|
|
|
handler = http.NotFoundHandler() |
|
|
|
handler = http.NotFoundHandler() |
|
|
|
} |
|
|
|
} |
|
|
|
if !r.KeepContext { |
|
|
|
if !r.KeepContext { |
|
|
|
defer context.Clear(req) |
|
|
|
defer contextClear(req) |
|
|
|
} |
|
|
|
} |
|
|
|
handler.ServeHTTP(w, req) |
|
|
|
handler.ServeHTTP(w, req) |
|
|
|
} |
|
|
|
} |
|
|
|
@ -325,7 +325,7 @@ const ( |
|
|
|
|
|
|
|
|
|
|
|
// Vars returns the route variables for the current request, if any.
|
|
|
|
// Vars returns the route variables for the current request, if any.
|
|
|
|
func Vars(r *http.Request) map[string]string { |
|
|
|
func Vars(r *http.Request) map[string]string { |
|
|
|
if rv := context.Get(r, varsKey); rv != nil { |
|
|
|
if rv := contextGet(r, varsKey); rv != nil { |
|
|
|
return rv.(map[string]string) |
|
|
|
return rv.(map[string]string) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
@ -337,22 +337,18 @@ func Vars(r *http.Request) map[string]string { |
|
|
|
// after the handler returns, unless the KeepContext option is set on the
|
|
|
|
// after the handler returns, unless the KeepContext option is set on the
|
|
|
|
// Router.
|
|
|
|
// Router.
|
|
|
|
func CurrentRoute(r *http.Request) *Route { |
|
|
|
func CurrentRoute(r *http.Request) *Route { |
|
|
|
if rv := context.Get(r, routeKey); rv != nil { |
|
|
|
if rv := contextGet(r, routeKey); rv != nil { |
|
|
|
return rv.(*Route) |
|
|
|
return rv.(*Route) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func setVars(r *http.Request, val interface{}) { |
|
|
|
func setVars(r *http.Request, val interface{}) *http.Request { |
|
|
|
if val != nil { |
|
|
|
return contextSet(r, varsKey, val) |
|
|
|
context.Set(r, varsKey, val) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func setCurrentRoute(r *http.Request, val interface{}) { |
|
|
|
func setCurrentRoute(r *http.Request, val interface{}) *http.Request { |
|
|
|
if val != nil { |
|
|
|
return contextSet(r, routeKey, val) |
|
|
|
context.Set(r, routeKey, val) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|