1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-30 08:46:41 +02:00

Better implementation for serving static files.

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-27 14:07:52 -07:00
parent e119cbc2ea
commit ac527ab4da
2 changed files with 44 additions and 8 deletions

40
echo.go
View File

@ -13,6 +13,8 @@ import (
"strings" "strings"
"sync" "sync"
"path/filepath"
"github.com/bradfitz/http2" "github.com/bradfitz/http2"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
@ -157,7 +159,7 @@ func New() (e *Echo) {
code = he.code code = he.code
msg = he.message msg = he.message
} }
if e.Debug() { if e.debug {
msg = err.Error() msg = err.Error()
} }
http.Error(c.response, msg, code) http.Error(c.response, msg, code)
@ -299,23 +301,45 @@ func (e *Echo) Favicon(file string) {
e.ServeFile("/favicon.ico", file) e.ServeFile("/favicon.ico", file)
} }
// Static serves static files. // Static serves static files from a directory. It's an alias for `Echo.ServeDir`
func (e *Echo) Static(path, root string) { func (e *Echo) Static(path, dir string) {
fs := http.StripPrefix(path, http.FileServer(http.Dir(root))) e.ServeDir(path, dir)
}
// ServeDir serves files from a directory.
func (e *Echo) ServeDir(path, dir string) {
e.Get(path+"*", func(c *Context) error { e.Get(path+"*", func(c *Context) error {
fs.ServeHTTP(c.response, c.request) return serveFile(dir, c.P(0), c) // Param `_name`
return nil
}) })
} }
// ServeFile serves a file. // ServeFile serves a file.
func (e *Echo) ServeFile(path, file string) { func (e *Echo) ServeFile(path, file string) {
e.Get(path, func(c *Context) error { e.Get(path, func(c *Context) error {
http.ServeFile(c.response, c.request, file) dir, file := filepath.Split(file)
return nil return serveFile(dir, file, c)
}) })
} }
func serveFile(dir, file string, c *Context) error {
fs := http.Dir(dir)
f, err := fs.Open(file)
if err != nil {
return NewHTTPError(http.StatusNotFound)
}
fi, err := f.Stat()
if err != nil {
return NewHTTPError(http.StatusNotFound)
}
if fi.IsDir() {
return NewHTTPError(http.StatusForbidden)
}
http.ServeContent(c.response, c.request, fi.Name(), fi.ModTime(), f)
return nil
}
// URI generates a URI from handler. // URI generates a URI from handler.
func (e *Echo) URI(h Handler, params ...interface{}) string { func (e *Echo) URI(h Handler, params ...interface{}) string {
uri := new(bytes.Buffer) uri := new(bytes.Buffer)

View File

@ -52,6 +52,18 @@ func (g *Group) WebSocket(path string, h HandlerFunc) {
g.echo.WebSocket(path, h) g.echo.WebSocket(path, h)
} }
func (g *Group) Static(path, root string) {
g.echo.Static(path, root)
}
func (g *Group) ServeDir(path, root string) {
g.echo.ServeDir(path, root)
}
func (g *Group) ServeFile(path, file string) {
g.echo.ServeFile(path, file)
}
func (g *Group) Group(prefix string, m ...Middleware) *Group { func (g *Group) Group(prefix string, m ...Middleware) *Group {
return g.echo.Group(prefix, m...) return g.echo.Group(prefix, m...)
} }