From 6c31570472b3b50ae638ceb02a68b8b3123e3c47 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Tue, 8 Jul 2014 00:28:39 +0200 Subject: [PATCH] work around path.Join removing trailing slashes from routes --- gin.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/gin.go b/gin.go index a06c13a..712230a 100644 --- a/gin.go +++ b/gin.go @@ -191,10 +191,20 @@ func (group *RouterGroup) Use(middlewares ...HandlerFunc) { 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. // For example, all the routes that use a common middlware for authorization could be grouped. func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup { - prefix := path.Join(group.prefix, component) + prefix := joinGroupPath(group.prefix, component) return &RouterGroup{ Handlers: group.combineHandlers(handlers), 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 // communication with a proxy). func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) { - p = path.Join(group.prefix, p) + p = joinGroupPath(group.prefix, p) handlers = group.combineHandlers(handlers) group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { c := group.engine.createContext(w, req, params, handlers)