Skip to content

Commit 12e2c74

Browse files
committed
Content type for static middleware
Signed-off-by: Vishal Rana <[email protected]>
1 parent 5ada9b5 commit 12e2c74

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

middleware/static.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package middleware
33
import (
44
"fmt"
55
"io"
6+
"mime"
67
"net/http"
78
"path"
9+
"path/filepath"
810

911
"github.com/labstack/echo"
1012
)
@@ -83,9 +85,14 @@ func Static(root string, options ...*StaticOptions) echo.MiddlewareFunc {
8385
}
8486
fi, _ = f.Stat() // Index file stat
8587
}
88+
ct := mime.TypeByExtension(filepath.Ext(fi.Name()))
89+
if ct == "" {
90+
ct = echo.OctetStream
91+
}
92+
c.Response().Header().Set(echo.ContentType, ct)
8693
c.Response().WriteHeader(http.StatusOK)
87-
io.Copy(c.Response(), f)
88-
return nil
94+
_, err = io.Copy(c.Response(), f)
95+
return err
8996
// TODO:
9097
// http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
9198
})

0 commit comments

Comments
 (0)