2016-03-31 21:17:18 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2016-04-13 07:39:29 +02:00
|
|
|
type (
|
|
|
|
// TrailingSlashConfig defines the config for TrailingSlash middleware.
|
|
|
|
TrailingSlashConfig struct {
|
|
|
|
// RedirectCode is the status code used when redirecting the request.
|
2016-04-20 16:32:51 +02:00
|
|
|
// Optional, but when provided the request is redirected using this code.
|
2016-04-13 07:39:29 +02:00
|
|
|
RedirectCode int
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-31 21:17:18 +02:00
|
|
|
// AddTrailingSlash returns a root level (before router) middleware which adds a
|
|
|
|
// trailing slash to the request `URL#Path`.
|
|
|
|
//
|
|
|
|
// Usage `Echo#Pre(AddTrailingSlash())`
|
|
|
|
func AddTrailingSlash() echo.MiddlewareFunc {
|
2016-04-13 07:39:29 +02:00
|
|
|
return AddTrailingSlashWithConfig(TrailingSlashConfig{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTrailingSlashWithConfig returns a AddTrailingSlash middleware from config.
|
|
|
|
// See `AddTrailingSlash()`.
|
|
|
|
func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc {
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-04-13 07:39:29 +02:00
|
|
|
rq := c.Request()
|
|
|
|
url := rq.URL()
|
2016-03-31 21:17:18 +02:00
|
|
|
path := url.Path()
|
2016-04-13 22:48:33 +02:00
|
|
|
qs := url.QueryString()
|
2016-03-31 21:17:18 +02:00
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
2016-04-13 07:39:29 +02:00
|
|
|
path += "/"
|
2016-04-13 22:48:33 +02:00
|
|
|
uri := path
|
|
|
|
if qs != "" {
|
|
|
|
uri += "?" + qs
|
|
|
|
}
|
2016-04-13 07:39:29 +02:00
|
|
|
if config.RedirectCode != 0 {
|
|
|
|
return c.Redirect(config.RedirectCode, uri)
|
|
|
|
}
|
|
|
|
rq.SetURI(uri)
|
|
|
|
url.SetPath(path)
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTrailingSlash returns a root level (before router) middleware which removes
|
|
|
|
// a trailing slash from the request URI.
|
|
|
|
//
|
|
|
|
// Usage `Echo#Pre(RemoveTrailingSlash())`
|
|
|
|
func RemoveTrailingSlash() echo.MiddlewareFunc {
|
2016-04-13 07:39:29 +02:00
|
|
|
return RemoveTrailingSlashWithConfig(TrailingSlashConfig{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTrailingSlashWithConfig returns a RemoveTrailingSlash middleware from config.
|
|
|
|
// See `RemoveTrailingSlash()`.
|
|
|
|
func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc {
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-04-13 07:39:29 +02:00
|
|
|
rq := c.Request()
|
|
|
|
url := rq.URL()
|
2016-03-31 21:17:18 +02:00
|
|
|
path := url.Path()
|
2016-04-13 22:48:33 +02:00
|
|
|
qs := url.QueryString()
|
2016-03-31 21:17:18 +02:00
|
|
|
l := len(path) - 1
|
|
|
|
if path != "/" && path[l] == '/' {
|
2016-04-13 07:39:29 +02:00
|
|
|
path = path[:l]
|
2016-04-13 22:48:33 +02:00
|
|
|
uri := path
|
|
|
|
if qs != "" {
|
|
|
|
uri += "?" + qs
|
|
|
|
}
|
2016-04-13 07:39:29 +02:00
|
|
|
if config.RedirectCode != 0 {
|
|
|
|
return c.Redirect(config.RedirectCode, uri)
|
|
|
|
}
|
|
|
|
rq.SetURI(uri)
|
|
|
|
url.SetPath(path)
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|
2016-04-02 23:19:39 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|
|
|
|
}
|