1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2017-02-22 13:04:08 -08:00
parent 7a858a7385
commit 91d2727522
2 changed files with 16 additions and 4 deletions

View File

@ -14,6 +14,11 @@ type (
// Use implements `Echo#Use()` for sub-routes within the Group.
func (g *Group) Use(middleware ...MiddlewareFunc) {
g.middleware = append(g.middleware, middleware...)
// Allow all requests to reach the group as they might get dropped if router
// doesn't find a match, making none of the group middleware process.
g.echo.Any(g.prefix+"/*", func(c Context) error {
return ErrNotFound
}, g.middleware...)
}
// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.

View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/labstack/echo"
)
@ -53,6 +54,9 @@ func Static(root string) echo.MiddlewareFunc {
// See `Static()`.
func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
// Defaults
if config.Root == "" {
config.Root = "." // For security we want to restrict to CWD.
}
if config.Skipper == nil {
config.Skipper = DefaultStaticConfig.Skipper
}
@ -62,16 +66,19 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
p := c.Param("*")
name := filepath.Join(config.Root, p)
fi, err := os.Stat(name)
path := c.Request().URL.Path
if strings.HasSuffix(c.Path(), "*") { // When serving from a group, e.g. `/static*`.
path = c.Param("*")
}
name := filepath.Join(config.Root, path)
fi, err := os.Stat(name)
if err != nil {
if os.IsNotExist(err) {
if config.HTML5 {
return c.File(filepath.Join(config.Root, config.Index))
}
return echo.ErrNotFound
return next(c)
}
return err
}