mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Enhanced static middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
a7d1a8e0d1
commit
ba46c48cba
4
echo.go
4
echo.go
@ -447,6 +447,10 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar
|
|||||||
func (e *Echo) Group(prefix string, m ...Middleware) (g *Group) {
|
func (e *Echo) Group(prefix string, m ...Middleware) (g *Group) {
|
||||||
g = &Group{prefix: prefix, echo: e}
|
g = &Group{prefix: prefix, echo: e}
|
||||||
g.Use(m...)
|
g.Use(m...)
|
||||||
|
// Dummy handler so group can be used with static middleware.
|
||||||
|
g.Get("", HandlerFunc(func(c Context) error {
|
||||||
|
return c.NoContent(http.StatusNotFound)
|
||||||
|
}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,9 +14,10 @@ type (
|
|||||||
// Root is the directory from where the static content is served.
|
// Root is the directory from where the static content is served.
|
||||||
Root string `json:"root"`
|
Root string `json:"root"`
|
||||||
|
|
||||||
// Index is the index file to be used while serving a directory.
|
// Index is the list of index files to be searched and used when serving
|
||||||
// Default is `index.html`.
|
// a directory.
|
||||||
Index string `json:"index"`
|
// Default value is `[]string{"index.html"}`.
|
||||||
|
Index []string `json:"index"`
|
||||||
|
|
||||||
// Browse is a flag to enable/disable directory browsing.
|
// Browse is a flag to enable/disable directory browsing.
|
||||||
Browse bool `json:"browse"`
|
Browse bool `json:"browse"`
|
||||||
@ -27,7 +28,7 @@ var (
|
|||||||
// DefaultStaticConfig is the default static middleware config.
|
// DefaultStaticConfig is the default static middleware config.
|
||||||
DefaultStaticConfig = StaticConfig{
|
DefaultStaticConfig = StaticConfig{
|
||||||
Root: "",
|
Root: "",
|
||||||
Index: "index.html",
|
Index: []string{"index.html"},
|
||||||
Browse: false,
|
Browse: false,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -46,7 +47,11 @@ func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
|
|||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) error {
|
||||||
fs := http.Dir(config.Root)
|
fs := http.Dir(config.Root)
|
||||||
file := path.Clean(c.Request().URL().Path())
|
p := c.Request().URL().Path()
|
||||||
|
if c.P(0) != "" { // If serving from `Group`, e.g. `/static/*`
|
||||||
|
p = c.P(0)
|
||||||
|
}
|
||||||
|
file := path.Clean(p)
|
||||||
f, err := fs.Open(file)
|
f, err := fs.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return next.Handle(c)
|
return next.Handle(c)
|
||||||
@ -66,7 +71,8 @@ func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
|
|||||||
d := f
|
d := f
|
||||||
|
|
||||||
// Index file
|
// Index file
|
||||||
file = path.Join(file, config.Index)
|
// TODO: search all files
|
||||||
|
file = path.Join(file, config.Index[0])
|
||||||
f, err = fs.Open(file)
|
f, err = fs.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if config.Browse {
|
if config.Browse {
|
||||||
|
Loading…
Reference in New Issue
Block a user