Skip to content

Commit

Permalink
fixed labstack#684
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Oct 22, 2016
1 parent 12573cd commit 954efac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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("*")))
})
}

Expand Down
1 change: 0 additions & 1 deletion middleware/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 8 additions & 5 deletions middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("*"))
Expand Down

0 comments on commit 954efac

Please sign in to comment.