forked from mirror/gin
Using absolutePath in static properly
This commit is contained in:
parent
07a3961941
commit
030706c39a
|
@ -146,7 +146,7 @@ func TestHandleStaticFile(t *testing.T) {
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
if w.Code != 200 {
|
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" {
|
if w.Body.String() != "Gin Web Framework" {
|
||||||
t.Errorf("Response should be test, was: %s", w.Body.String())
|
t.Errorf("Response should be test, was: %s", w.Body.String())
|
||||||
|
@ -168,7 +168,7 @@ func TestHandleStaticDir(t *testing.T) {
|
||||||
// TEST
|
// TEST
|
||||||
bodyAsString := w.Body.String()
|
bodyAsString := w.Body.String()
|
||||||
if w.Code != 200 {
|
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 {
|
if len(bodyAsString) == 0 {
|
||||||
t.Errorf("Got empty body instead of file tree")
|
t.Errorf("Got empty body instead of file tree")
|
||||||
|
|
|
@ -102,14 +102,14 @@ func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) {
|
||||||
// use :
|
// use :
|
||||||
// router.Static("/static", "/var/www")
|
// router.Static("/static", "/var/www")
|
||||||
func (group *RouterGroup) Static(relativePath, root string) {
|
func (group *RouterGroup) Static(relativePath, root string) {
|
||||||
handler := group.createStaticHandler(relativePath, root)
|
absolutePath := group.calculateAbsolutePath(relativePath)
|
||||||
group.GET(relativePath, handler)
|
handler := group.createStaticHandler(absolutePath, root)
|
||||||
group.HEAD(relativePath, handler)
|
absolutePath = path.Join(absolutePath, "/*filepath")
|
||||||
|
group.GET(absolutePath, handler)
|
||||||
|
group.HEAD(absolutePath, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (group *RouterGroup) createStaticHandler(relativePath, root string) func(*Context) {
|
func (group *RouterGroup) createStaticHandler(absolutePath, root string) func(*Context) {
|
||||||
absolutePath := group.calculateAbsolutePath(relativePath)
|
|
||||||
absolutePath = path.Join(absolutePath, "/*filepath")
|
|
||||||
fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root)))
|
fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root)))
|
||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
fileServer.ServeHTTP(c.Writer, c.Request)
|
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||||
|
|
Loading…
Reference in New Issue