add RelativePath

This commit is contained in:
youngblood 2018-07-28 13:13:02 +08:00
parent 220e8d3453
commit ff434e2667
3 changed files with 13 additions and 2 deletions

View File

@ -57,6 +57,9 @@ type Context struct {
// Accepted defines a list of manually accepted formats for content negotiation.
Accepted []string
// save the route's relativePath
RelativePath string
}
/************************************/

3
gin.go
View File

@ -352,7 +352,8 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
if t[i].method == httpMethod {
root := t[i].root
// Find route in tree
handlers, params, tsr := root.getValue(path, c.Params, unescape)
handlers, params, relativePath, tsr := root.getValue(path, c.Params, unescape)
c.RelativePath = relativePath
if handlers != nil {
c.handlers = handlers
c.Params = params

View File

@ -362,8 +362,9 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
// made if a handle exists with an extra (without the) trailing slash for the
// given path.
func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, tsr bool) {
func (n *node) getValue(path string, po Params, unescape bool) (handlers HandlersChain, p Params, relativePath string, tsr bool) {
p = po
relativePath = path
walk: // Outer loop for walking the tree
for {
if len(path) > len(n.path) {
@ -415,6 +416,9 @@ walk: // Outer loop for walking the tree
p[i].Value = val
}
// replace p.value with p.key (pattern :)
relativePath = strings.Replace(relativePath, p[i].Value, ":"+p[i].Key, 1)
// we need to go deeper!
if end < len(path) {
if len(n.children) > 0 {
@ -457,6 +461,9 @@ walk: // Outer loop for walking the tree
p[i].Value = path
}
// replace p.value with p.key (pattern *)
relativePath = strings.Replace(relativePath, p[i].Value, "/*"+p[i].Key, 1)
handlers = n.handlers
return