From dda70bf38282ba97e33bad083de607355ec6065b Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Thu, 17 Jul 2014 02:02:09 +0200 Subject: [PATCH] Refactors Static() file serving --- gin.go | 21 ++++++++++++++------- utils.go | 11 ----------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/gin.go b/gin.go index 095c018..cab00e9 100644 --- a/gin.go +++ b/gin.go @@ -123,7 +123,8 @@ func (group *RouterGroup) Use(middlewares ...HandlerFunc) { // 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 := joinGroupPath(group.prefix, component) + prefix := group.pathFor(component) + return &RouterGroup{ Handlers: group.combineHandlers(handlers), parent: group, @@ -132,6 +133,15 @@ func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *Rout } } +func (group *RouterGroup) pathFor(p string) string { + joined := path.Join(group.prefix, p) + // Append a '/' if the last component had one, but only if it's not there already + if len(p) > 0 && p[len(p)-1] == '/' && joined[len(p)-1] != '/' { + return joined + "/" + } + return joined +} + // Handle registers a new request handle and middlewares with the given path and method. // The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes. // See the example code in github. @@ -143,7 +153,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 = joinGroupPath(group.prefix, p) + p = group.pathFor(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) @@ -194,14 +204,11 @@ func (group *RouterGroup) HEAD(path string, handlers ...HandlerFunc) { // use : // router.Static("/static", "/var/www") func (group *RouterGroup) Static(p, root string) { + prefix := group.pathFor(p) p = path.Join(p, "/*filepath") - fileServer := http.FileServer(http.Dir(root)) - + fileServer := http.StripPrefix(prefix, http.FileServer(http.Dir(root))) group.GET(p, func(c *Context) { - original := c.Request.URL.Path - c.Request.URL.Path = c.Params.ByName("filepath") fileServer.ServeHTTP(c.Writer, c.Request) - c.Request.URL.Path = original }) } diff --git a/utils.go b/utils.go index f6e3046..90cca1b 100644 --- a/utils.go +++ b/utils.go @@ -2,7 +2,6 @@ package gin import ( "encoding/xml" - "path" ) type H map[string]interface{} @@ -31,16 +30,6 @@ func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error { return nil } -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 -} - func filterFlags(content string) string { for i, a := range content { if a == ' ' || a == ';' {