2016-04-11 05:56:10 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-07-27 18:34:44 +02:00
|
|
|
// StaticConfig defines the config for Static middleware.
|
2016-04-11 05:56:10 +02:00
|
|
|
StaticConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-10-23 00:12:12 +02:00
|
|
|
// Prefix to strip from the request URL path.
|
|
|
|
// Required.
|
|
|
|
Prefix string `json:"root"`
|
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// Root directory from where the static content is served.
|
2016-04-11 05:56:10 +02:00
|
|
|
// Required.
|
|
|
|
Root string `json:"root"`
|
|
|
|
|
2016-05-10 20:52:04 +02:00
|
|
|
// Index file for serving a directory.
|
|
|
|
// Optional. Default value "index.html".
|
|
|
|
Index string `json:"index"`
|
2016-04-11 05:56:10 +02:00
|
|
|
|
2016-05-23 20:23:15 +02:00
|
|
|
// Enable HTML5 mode by forwarding all not-found requests to root so that
|
|
|
|
// SPA (single-page application) can handle the routing.
|
2016-05-23 20:31:25 +02:00
|
|
|
// Optional. Default value false.
|
2016-05-22 19:21:19 +02:00
|
|
|
HTML5 bool `json:"html5"`
|
|
|
|
|
|
|
|
// Enable directory browsing.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value false.
|
2016-04-11 05:56:10 +02:00
|
|
|
Browse bool `json:"browse"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-07-27 18:34:44 +02:00
|
|
|
// DefaultStaticConfig is the default Static middleware config.
|
2016-04-11 05:56:10 +02:00
|
|
|
DefaultStaticConfig = StaticConfig{
|
2016-07-27 18:34:44 +02:00
|
|
|
Skipper: defaultSkipper,
|
|
|
|
Index: "index.html",
|
2016-04-11 05:56:10 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
// Static returns a Static middleware to serves static content from the provided
|
2016-04-11 05:56:10 +02:00
|
|
|
// root directory.
|
2016-10-23 00:12:12 +02:00
|
|
|
func Static(prefix, root string) echo.MiddlewareFunc {
|
2016-04-11 05:56:10 +02:00
|
|
|
c := DefaultStaticConfig
|
2016-10-23 00:12:12 +02:00
|
|
|
c.Prefix = prefix
|
2016-04-11 05:56:10 +02:00
|
|
|
c.Root = root
|
|
|
|
return StaticWithConfig(c)
|
|
|
|
}
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
// StaticWithConfig returns a Static middleware with config.
|
2016-04-11 05:56:10 +02:00
|
|
|
// See `Static()`.
|
|
|
|
func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
|
|
|
// Defaults
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultStaticConfig.Skipper
|
|
|
|
}
|
2016-05-10 20:52:04 +02:00
|
|
|
if config.Index == "" {
|
2016-04-11 05:56:10 +02:00
|
|
|
config.Index = DefaultStaticConfig.Index
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2016-04-11 05:56:10 +02:00
|
|
|
fs := http.Dir(config.Root)
|
2016-10-23 00:12:12 +02:00
|
|
|
p := strings.TrimPrefix(c.Request().URL.Path, config.Prefix)
|
2016-04-11 05:56:10 +02:00
|
|
|
file := path.Clean(p)
|
|
|
|
f, err := fs.Open(file)
|
|
|
|
if err != nil {
|
2016-05-22 19:21:19 +02:00
|
|
|
// 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
|
|
|
|
}
|
2016-04-11 05:56:10 +02:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
/* NOTE:
|
|
|
|
Not checking the Last-Modified header as it caches the response `304` when
|
|
|
|
changing different directories for the same path.
|
|
|
|
*/
|
|
|
|
d := f
|
|
|
|
|
|
|
|
// Index file
|
2016-05-10 20:52:04 +02:00
|
|
|
file = path.Join(file, config.Index)
|
2016-04-11 05:56:10 +02:00
|
|
|
f, err = fs.Open(file)
|
2016-05-01 15:56:52 +02:00
|
|
|
if err == nil {
|
|
|
|
// Index file
|
|
|
|
if fi, err = f.Stat(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil && config.Browse {
|
2016-04-11 05:56:10 +02:00
|
|
|
dirs, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a directory index
|
2016-04-24 19:21:23 +02:00
|
|
|
res := c.Response()
|
|
|
|
res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
|
|
|
|
if _, err = fmt.Fprintf(res, "<pre>\n"); err != nil {
|
2016-04-11 05:56:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, d := range dirs {
|
|
|
|
name := d.Name()
|
|
|
|
color := "#212121"
|
|
|
|
if d.IsDir() {
|
|
|
|
color = "#e91e63"
|
|
|
|
name += "/"
|
|
|
|
}
|
2016-04-24 19:21:23 +02:00
|
|
|
if _, err = fmt.Fprintf(res, "<a href=\"%s\" style=\"color: %s;\">%s</a>\n", name, color, name); err != nil {
|
2016-04-11 05:56:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-04-24 19:21:23 +02:00
|
|
|
_, err = fmt.Fprintf(res, "</pre>\n")
|
2016-04-11 05:56:10 +02:00
|
|
|
return err
|
2016-05-01 15:56:52 +02:00
|
|
|
} else {
|
|
|
|
return next(c)
|
2016-04-11 05:56:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return c.ServeContent(f, fi.Name(), fi.ModTime())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|