work around path.Join removing trailing slashes from routes

This commit is contained in:
Lucas Clemente 2014-07-08 00:28:39 +02:00
parent c224bf8211
commit 6c31570472
1 changed files with 12 additions and 2 deletions

14
gin.go
View File

@ -191,10 +191,20 @@ func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
group.Handlers = append(group.Handlers, middlewares...) group.Handlers = append(group.Handlers, middlewares...)
} }
func joinGroupPath(elems ...string) string {
joined := path.Join(elems...)
lastComponent := elems[len(elems)-1]
// Append a '/' if the last component had one, but only if it's not there already
if len(lastComponent) > 0 && lastComponent[len(lastComponent)-1] == '/' && joined[len(joined)-1] != '/' {
return joined + "/"
}
return joined
}
// Creates a new router group. You should add all the routes that have common middlwares or the same path prefix. // Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
// For example, all the routes that use a common middlware for authorization could be grouped. // For example, all the routes that use a common middlware for authorization could be grouped.
func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup { func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup {
prefix := path.Join(group.prefix, component) prefix := joinGroupPath(group.prefix, component)
return &RouterGroup{ return &RouterGroup{
Handlers: group.combineHandlers(handlers), Handlers: group.combineHandlers(handlers),
parent: group, parent: group,
@ -214,7 +224,7 @@ func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *Rout
// frequently used, non-standardized or custom methods (e.g. for internal // frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy). // communication with a proxy).
func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) { func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) {
p = path.Join(group.prefix, p) p = joinGroupPath(group.prefix, p)
handlers = group.combineHandlers(handlers) handlers = group.combineHandlers(handlers)
group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
c := group.engine.createContext(w, req, params, handlers) c := group.engine.createContext(w, req, params, handlers)