2015-05-14 08:07:03 +02:00
|
|
|
package middleware
|
|
|
|
|
2015-05-15 01:25:49 +02:00
|
|
|
import (
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
RedirectToSlashOptions struct {
|
|
|
|
Code int
|
|
|
|
}
|
|
|
|
)
|
2015-05-14 08:07:03 +02:00
|
|
|
|
|
|
|
// StripTrailingSlash removes trailing slash from request path.
|
|
|
|
func StripTrailingSlash() echo.HandlerFunc {
|
|
|
|
return func(c *echo.Context) *echo.HTTPError {
|
|
|
|
p := c.Request.URL.Path
|
|
|
|
l := len(p)
|
|
|
|
if p[l-1] == '/' {
|
|
|
|
c.Request.URL.Path = p[:l-1]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RedirectToSlash redirects requests without trailing slash path to trailing slash
|
2015-05-15 01:25:49 +02:00
|
|
|
// path, with .
|
|
|
|
func RedirectToSlash(opts ...RedirectToSlashOptions) echo.HandlerFunc {
|
|
|
|
code := http.StatusMovedPermanently
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
if o.Code != 0 {
|
|
|
|
code = o.Code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 08:07:03 +02:00
|
|
|
return func(c *echo.Context) (he *echo.HTTPError) {
|
|
|
|
p := c.Request.URL.Path
|
|
|
|
l := len(p)
|
|
|
|
if p[l-1] != '/' {
|
|
|
|
c.Redirect(code, p+"/")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|