1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-26 20:54:00 +02:00

HTML5 mode in static middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-05-22 10:21:19 -07:00
parent c24f06e7cb
commit ecb021f830

View File

@ -20,7 +20,10 @@ type (
// Optional. Default value "index.html". // Optional. Default value "index.html".
Index string `json:"index"` Index string `json:"index"`
// Enable/disable directory browsing. // Enable HTML5 mode by forwarding all not-found routes to root.
HTML5 bool `json:"html5"`
// Enable directory browsing.
// Optional. Default value false. // Optional. Default value false.
Browse bool `json:"browse"` Browse bool `json:"browse"`
} }
@ -30,6 +33,7 @@ var (
// DefaultStaticConfig is the default static middleware config. // DefaultStaticConfig is the default static middleware config.
DefaultStaticConfig = StaticConfig{ DefaultStaticConfig = StaticConfig{
Index: "index.html", Index: "index.html",
HTML5: true,
Browse: false, Browse: false,
} }
) )
@ -60,7 +64,18 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
file := path.Clean(p) file := path.Clean(p)
f, err := fs.Open(file) f, err := fs.Open(file)
if err != nil { if err != nil {
return next(c) // HTML5 mode
err = next(c)
if he, ok := err.(*echo.HTTPError); ok {
if config.HTML5 && he.Code == http.StatusNotFound {
file = ""
f, err = fs.Open(file)
} else {
return err
}
} else {
return err
}
} }
defer f.Close() defer f.Close()
fi, err := f.Stat() fi, err := f.Stat()