From 030706c39aaa0e65c259a34d8eac354902eb1693 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 8 Oct 2014 21:49:08 +0200 Subject: [PATCH] Using absolutePath in static properly --- gin_test.go | 4 ++-- routergroup.go | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gin_test.go b/gin_test.go index 33979438..1368aa08 100644 --- a/gin_test.go +++ b/gin_test.go @@ -146,7 +146,7 @@ func TestHandleStaticFile(t *testing.T) { // TEST if w.Code != 200 { - t.Errorf("Response code should be Ok, was: %s", w.Code) + t.Errorf("Response code should be 200, was: %d", w.Code) } if w.Body.String() != "Gin Web Framework" { t.Errorf("Response should be test, was: %s", w.Body.String()) @@ -168,7 +168,7 @@ func TestHandleStaticDir(t *testing.T) { // TEST bodyAsString := w.Body.String() if w.Code != 200 { - t.Errorf("Response code should be Ok, was: %s", w.Code) + t.Errorf("Response code should be 200, was: %d", w.Code) } if len(bodyAsString) == 0 { t.Errorf("Got empty body instead of file tree") diff --git a/routergroup.go b/routergroup.go index 651bf931..8163e977 100644 --- a/routergroup.go +++ b/routergroup.go @@ -102,14 +102,14 @@ func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) { // use : // router.Static("/static", "/var/www") func (group *RouterGroup) Static(relativePath, root string) { - handler := group.createStaticHandler(relativePath, root) - group.GET(relativePath, handler) - group.HEAD(relativePath, handler) + absolutePath := group.calculateAbsolutePath(relativePath) + handler := group.createStaticHandler(absolutePath, root) + absolutePath = path.Join(absolutePath, "/*filepath") + group.GET(absolutePath, handler) + group.HEAD(absolutePath, handler) } -func (group *RouterGroup) createStaticHandler(relativePath, root string) func(*Context) { - absolutePath := group.calculateAbsolutePath(relativePath) - absolutePath = path.Join(absolutePath, "/*filepath") +func (group *RouterGroup) createStaticHandler(absolutePath, root string) func(*Context) { fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root))) return func(c *Context) { fileServer.ServeHTTP(c.Writer, c.Request)