Skip to content

Commit 4159cef

Browse files
committed
Merge pull request #85 from labstack/static-content
Better implementation for serving static files.
2 parents e119cbc + ac527ab commit 4159cef

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

echo.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"strings"
1414
"sync"
1515

16+
"path/filepath"
17+
1618
"github.com/bradfitz/http2"
1719
"github.com/mattn/go-colorable"
1820
"golang.org/x/net/websocket"
@@ -157,7 +159,7 @@ func New() (e *Echo) {
157159
code = he.code
158160
msg = he.message
159161
}
160-
if e.Debug() {
162+
if e.debug {
161163
msg = err.Error()
162164
}
163165
http.Error(c.response, msg, code)
@@ -299,23 +301,45 @@ func (e *Echo) Favicon(file string) {
299301
e.ServeFile("/favicon.ico", file)
300302
}
301303

302-
// Static serves static files.
303-
func (e *Echo) Static(path, root string) {
304-
fs := http.StripPrefix(path, http.FileServer(http.Dir(root)))
304+
// Static serves static files from a directory. It's an alias for `Echo.ServeDir`
305+
func (e *Echo) Static(path, dir string) {
306+
e.ServeDir(path, dir)
307+
}
308+
309+
// ServeDir serves files from a directory.
310+
func (e *Echo) ServeDir(path, dir string) {
305311
e.Get(path+"*", func(c *Context) error {
306-
fs.ServeHTTP(c.response, c.request)
307-
return nil
312+
return serveFile(dir, c.P(0), c) // Param `_name`
308313
})
309314
}
310315

311316
// ServeFile serves a file.
312317
func (e *Echo) ServeFile(path, file string) {
313318
e.Get(path, func(c *Context) error {
314-
http.ServeFile(c.response, c.request, file)
315-
return nil
319+
dir, file := filepath.Split(file)
320+
return serveFile(dir, file, c)
316321
})
317322
}
318323

324+
func serveFile(dir, file string, c *Context) error {
325+
fs := http.Dir(dir)
326+
f, err := fs.Open(file)
327+
if err != nil {
328+
return NewHTTPError(http.StatusNotFound)
329+
}
330+
331+
fi, err := f.Stat()
332+
if err != nil {
333+
return NewHTTPError(http.StatusNotFound)
334+
}
335+
if fi.IsDir() {
336+
return NewHTTPError(http.StatusForbidden)
337+
}
338+
339+
http.ServeContent(c.response, c.request, fi.Name(), fi.ModTime(), f)
340+
return nil
341+
}
342+
319343
// URI generates a URI from handler.
320344
func (e *Echo) URI(h Handler, params ...interface{}) string {
321345
uri := new(bytes.Buffer)

group.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ func (g *Group) WebSocket(path string, h HandlerFunc) {
5252
g.echo.WebSocket(path, h)
5353
}
5454

55+
func (g *Group) Static(path, root string) {
56+
g.echo.Static(path, root)
57+
}
58+
59+
func (g *Group) ServeDir(path, root string) {
60+
g.echo.ServeDir(path, root)
61+
}
62+
63+
func (g *Group) ServeFile(path, file string) {
64+
g.echo.ServeFile(path, file)
65+
}
66+
5567
func (g *Group) Group(prefix string, m ...Middleware) *Group {
5668
return g.echo.Group(prefix, m...)
5769
}

0 commit comments

Comments
 (0)