1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-10-22 15:12:12 -07:00
parent 12573cd76d
commit 954efac63a
4 changed files with 14 additions and 12 deletions

View File

@ -14,13 +14,13 @@ type (
)
// Use implements `Echo#Use()` for sub-routes within the Group.
func (g *Group) Use(m ...MiddlewareFunc) {
g.middleware = append(g.middleware, m...)
func (g *Group) Use(middleware ...MiddlewareFunc) {
g.middleware = append(g.middleware, middleware...)
// Allow requests `/prefix & /prefix/*` to reach the group as they might get
// dropped if router doesn't find a match, making none of the group middleware
// execute.
g.echo.Any(g.prefix, NotFoundHandler, g.middleware...)
g.echo.Any(g.prefix+"/*", NotFoundHandler, g.middleware...)
g.Any("", NotFoundHandler, g.middleware...)
g.Any("/*", NotFoundHandler, g.middleware...)
}
// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
@ -93,7 +93,7 @@ func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group {
// Static implements `Echo#Static()` for sub-routes within the Group.
func (g *Group) Static(prefix, root string) {
g.GET(g.prefix+prefix+"*", func(c Context) error {
return c.File(path.Join(root, c.Param("_*")))
return c.File(path.Join(root, c.Param("*")))
})
}

View File

@ -58,7 +58,6 @@ func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
req := c.Request()
host := req.Host
uri := req.RequestURI
println(uri)
if !c.IsTLS() {
return c.Redirect(config.Code, "https://"+host+uri)
}

View File

@ -15,6 +15,10 @@ type (
// Skipper defines a function to skip middleware.
Skipper Skipper
// Prefix to strip from the request URL path.
// Required.
Prefix string `json:"root"`
// Root directory from where the static content is served.
// Required.
Root string `json:"root"`
@ -44,8 +48,9 @@ var (
// Static returns a Static middleware to serves static content from the provided
// root directory.
func Static(root string) echo.MiddlewareFunc {
func Static(prefix, root string) echo.MiddlewareFunc {
c := DefaultStaticConfig
c.Prefix = prefix
c.Root = root
return StaticWithConfig(c)
}
@ -68,10 +73,8 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
}
fs := http.Dir(config.Root)
p := c.Request().URL.Path
if strings.Contains(c.Path(), "*") { // If serving from a group, e.g. `/static*`.
p = c.Param("*")
}
p := strings.TrimPrefix(c.Request().URL.Path, config.Prefix)
println(p)
file := path.Clean(p)
f, err := fs.Open(file)
if err != nil {

View File

@ -347,7 +347,7 @@ func TestRouterMatchAny(t *testing.T) {
c := e.NewContext(nil, nil).(*context)
r.Find(GET, "/", c)
assert.Equal(t, "", c.Param("_*"))
assert.Equal(t, "", c.Param("*"))
r.Find(GET, "/download", c)
assert.Equal(t, "download", c.Param("*"))