mirror of
https://github.com/labstack/echo.git
synced 2025-07-15 01:34:53 +02:00
10
group.go
10
group.go
@ -14,13 +14,13 @@ type (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Use implements `Echo#Use()` for sub-routes within the Group.
|
// Use implements `Echo#Use()` for sub-routes within the Group.
|
||||||
func (g *Group) Use(m ...MiddlewareFunc) {
|
func (g *Group) Use(middleware ...MiddlewareFunc) {
|
||||||
g.middleware = append(g.middleware, m...)
|
g.middleware = append(g.middleware, middleware...)
|
||||||
// Allow requests `/prefix & /prefix/*` to reach the group as they might get
|
// 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
|
// dropped if router doesn't find a match, making none of the group middleware
|
||||||
// execute.
|
// execute.
|
||||||
g.echo.Any(g.prefix, NotFoundHandler, g.middleware...)
|
g.Any("", NotFoundHandler, g.middleware...)
|
||||||
g.echo.Any(g.prefix+"/*", NotFoundHandler, g.middleware...)
|
g.Any("/*", NotFoundHandler, g.middleware...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
|
// 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.
|
// Static implements `Echo#Static()` for sub-routes within the Group.
|
||||||
func (g *Group) Static(prefix, root string) {
|
func (g *Group) Static(prefix, root string) {
|
||||||
g.GET(g.prefix+prefix+"*", func(c Context) error {
|
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("*")))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,6 @@ func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
|||||||
req := c.Request()
|
req := c.Request()
|
||||||
host := req.Host
|
host := req.Host
|
||||||
uri := req.RequestURI
|
uri := req.RequestURI
|
||||||
println(uri)
|
|
||||||
if !c.IsTLS() {
|
if !c.IsTLS() {
|
||||||
return c.Redirect(config.Code, "https://"+host+uri)
|
return c.Redirect(config.Code, "https://"+host+uri)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,10 @@ type (
|
|||||||
// Skipper defines a function to skip middleware.
|
// Skipper defines a function to skip middleware.
|
||||||
Skipper Skipper
|
Skipper Skipper
|
||||||
|
|
||||||
|
// Prefix to strip from the request URL path.
|
||||||
|
// Required.
|
||||||
|
Prefix string `json:"root"`
|
||||||
|
|
||||||
// Root directory from where the static content is served.
|
// Root directory from where the static content is served.
|
||||||
// Required.
|
// Required.
|
||||||
Root string `json:"root"`
|
Root string `json:"root"`
|
||||||
@ -44,8 +48,9 @@ var (
|
|||||||
|
|
||||||
// Static returns a Static middleware to serves static content from the provided
|
// Static returns a Static middleware to serves static content from the provided
|
||||||
// root directory.
|
// root directory.
|
||||||
func Static(root string) echo.MiddlewareFunc {
|
func Static(prefix, root string) echo.MiddlewareFunc {
|
||||||
c := DefaultStaticConfig
|
c := DefaultStaticConfig
|
||||||
|
c.Prefix = prefix
|
||||||
c.Root = root
|
c.Root = root
|
||||||
return StaticWithConfig(c)
|
return StaticWithConfig(c)
|
||||||
}
|
}
|
||||||
@ -68,10 +73,8 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fs := http.Dir(config.Root)
|
fs := http.Dir(config.Root)
|
||||||
p := c.Request().URL.Path
|
p := strings.TrimPrefix(c.Request().URL.Path, config.Prefix)
|
||||||
if strings.Contains(c.Path(), "*") { // If serving from a group, e.g. `/static*`.
|
println(p)
|
||||||
p = c.Param("*")
|
|
||||||
}
|
|
||||||
file := path.Clean(p)
|
file := path.Clean(p)
|
||||||
f, err := fs.Open(file)
|
f, err := fs.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -347,7 +347,7 @@ func TestRouterMatchAny(t *testing.T) {
|
|||||||
c := e.NewContext(nil, nil).(*context)
|
c := e.NewContext(nil, nil).(*context)
|
||||||
|
|
||||||
r.Find(GET, "/", c)
|
r.Find(GET, "/", c)
|
||||||
assert.Equal(t, "", c.Param("_*"))
|
assert.Equal(t, "", c.Param("*"))
|
||||||
|
|
||||||
r.Find(GET, "/download", c)
|
r.Find(GET, "/download", c)
|
||||||
assert.Equal(t, "download", c.Param("*"))
|
assert.Equal(t, "download", c.Param("*"))
|
||||||
|
Reference in New Issue
Block a user