mirror of
https://github.com/labstack/echo.git
synced 2025-07-15 01:34:53 +02:00
Options for redirect middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -42,7 +42,7 @@ func BasicAuth(fn BasicAuthValidator) echo.MiddlewareFunc {
|
|||||||
return BasicAuthWithConfig(c)
|
return BasicAuthWithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BasicAuthWithConfig returns an BasicAuth middleware from config.
|
// BasicAuthWithConfig returns an BasicAuth middleware with config.
|
||||||
// See `BasicAuth()`.
|
// See `BasicAuth()`.
|
||||||
func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -50,7 +50,7 @@ func BodyLimit(limit string) echo.MiddlewareFunc {
|
|||||||
return BodyLimitWithConfig(c)
|
return BodyLimitWithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BodyLimitWithConfig returns a BodyLimit middleware from config.
|
// BodyLimitWithConfig returns a BodyLimit middleware with config.
|
||||||
// See: `BodyLimit()`.
|
// See: `BodyLimit()`.
|
||||||
func BodyLimitWithConfig(config BodyLimitConfig) echo.MiddlewareFunc {
|
func BodyLimitWithConfig(config BodyLimitConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -43,7 +43,7 @@ func Gzip() echo.MiddlewareFunc {
|
|||||||
return GzipWithConfig(DefaultGzipConfig)
|
return GzipWithConfig(DefaultGzipConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GzipWithConfig return Gzip middleware from config.
|
// GzipWithConfig return Gzip middleware with config.
|
||||||
// See: `Gzip()`.
|
// See: `Gzip()`.
|
||||||
func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
|
func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -62,7 +62,7 @@ func CORS() echo.MiddlewareFunc {
|
|||||||
return CORSWithConfig(DefaultCORSConfig)
|
return CORSWithConfig(DefaultCORSConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CORSWithConfig returns a CORS middleware from config.
|
// CORSWithConfig returns a CORS middleware with config.
|
||||||
// See: `CORS()`.
|
// See: `CORS()`.
|
||||||
func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
|
func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -83,7 +83,7 @@ func CSRF() echo.MiddlewareFunc {
|
|||||||
return CSRFWithConfig(c)
|
return CSRFWithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CSRFWithConfig returns a CSRF middleware from config.
|
// CSRFWithConfig returns a CSRF middleware with config.
|
||||||
// See `CSRF()`.
|
// See `CSRF()`.
|
||||||
func CSRFWithConfig(config CSRFConfig) echo.MiddlewareFunc {
|
func CSRFWithConfig(config CSRFConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -79,7 +79,7 @@ func JWT(key []byte) echo.MiddlewareFunc {
|
|||||||
return JWTWithConfig(c)
|
return JWTWithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// JWTWithConfig returns a JWT auth middleware from config.
|
// JWTWithConfig returns a JWT auth middleware with config.
|
||||||
// See: `JWT()`.
|
// See: `JWT()`.
|
||||||
func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
|
func JWTWithConfig(config JWTConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -70,7 +70,7 @@ func Logger() echo.MiddlewareFunc {
|
|||||||
return LoggerWithConfig(DefaultLoggerConfig)
|
return LoggerWithConfig(DefaultLoggerConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoggerWithConfig returns a Logger middleware from config.
|
// LoggerWithConfig returns a Logger middleware with config.
|
||||||
// See: `Logger()`.
|
// See: `Logger()`.
|
||||||
func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
|
func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -34,7 +34,7 @@ func MethodOverride() echo.MiddlewareFunc {
|
|||||||
return MethodOverrideWithConfig(DefaultMethodOverrideConfig)
|
return MethodOverrideWithConfig(DefaultMethodOverrideConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MethodOverrideWithConfig returns a MethodOverride middleware from config.
|
// MethodOverrideWithConfig returns a MethodOverride middleware with config.
|
||||||
// See: `MethodOverride()`.
|
// See: `MethodOverride()`.
|
||||||
func MethodOverrideWithConfig(config MethodOverrideConfig) echo.MiddlewareFunc {
|
func MethodOverrideWithConfig(config MethodOverrideConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -45,7 +45,7 @@ func Recover() echo.MiddlewareFunc {
|
|||||||
return RecoverWithConfig(DefaultRecoverConfig)
|
return RecoverWithConfig(DefaultRecoverConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RecoverWithConfig returns a Recover middleware from config.
|
// RecoverWithConfig returns a Recover middleware with config.
|
||||||
// See: `Recover()`.
|
// See: `Recover()`.
|
||||||
func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
|
func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -6,16 +6,56 @@ import (
|
|||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
// RedirectConfig defines the config for Redirect middleware.
|
||||||
|
RedirectConfig struct {
|
||||||
|
// Skipper defines a function to skip middleware.
|
||||||
|
Skipper Skipper
|
||||||
|
|
||||||
|
// Status code to be used when redirecting the request.
|
||||||
|
// Optional. Default value http.StatusMovedPermanently.
|
||||||
|
Code int `json:"code"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultRedirectConfig is the default Redirect middleware config.
|
||||||
|
DefaultRedirectConfig = RedirectConfig{
|
||||||
|
Skipper: defaultSkipper,
|
||||||
|
Code: http.StatusMovedPermanently,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// HTTPSRedirect redirects HTTP requests to HTTPS.
|
// HTTPSRedirect redirects HTTP requests to HTTPS.
|
||||||
// For example, http://labstack.com will be redirect to https://labstack.com.
|
// For example, http://labstack.com will be redirect to https://labstack.com.
|
||||||
|
//
|
||||||
|
// Usage `Echo#Pre(HTTPSRedirect())`
|
||||||
func HTTPSRedirect() echo.MiddlewareFunc {
|
func HTTPSRedirect() echo.MiddlewareFunc {
|
||||||
|
return HTTPSRedirectWithConfig(DefaultRedirectConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPSRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
||||||
|
// See `HTTPSRedirect()`.
|
||||||
|
func HTTPSRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
||||||
|
// Defaults
|
||||||
|
if config.Skipper == nil {
|
||||||
|
config.Skipper = DefaultTrailingSlashConfig.Skipper
|
||||||
|
}
|
||||||
|
if config.Code == 0 {
|
||||||
|
config.Code = DefaultRedirectConfig.Code
|
||||||
|
}
|
||||||
|
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
|
if config.Skipper(c) {
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
host := req.Host()
|
host := req.Host()
|
||||||
uri := req.URI()
|
uri := req.URI()
|
||||||
if !req.IsTLS() {
|
if !req.IsTLS() {
|
||||||
return c.Redirect(http.StatusMovedPermanently, "https://"+host+uri)
|
return c.Redirect(config.Code, "https://"+host+uri)
|
||||||
}
|
}
|
||||||
return next(c)
|
return next(c)
|
||||||
}
|
}
|
||||||
@ -24,9 +64,29 @@ func HTTPSRedirect() echo.MiddlewareFunc {
|
|||||||
|
|
||||||
// HTTPSWWWRedirect redirects HTTP requests to WWW HTTPS.
|
// HTTPSWWWRedirect redirects HTTP requests to WWW HTTPS.
|
||||||
// For example, http://labstack.com will be redirect to https://www.labstack.com.
|
// For example, http://labstack.com will be redirect to https://www.labstack.com.
|
||||||
|
//
|
||||||
|
// Usage `Echo#Pre(HTTPSWWWRedirect())`
|
||||||
func HTTPSWWWRedirect() echo.MiddlewareFunc {
|
func HTTPSWWWRedirect() echo.MiddlewareFunc {
|
||||||
|
return HTTPSWWWRedirectWithConfig(DefaultRedirectConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTPSWWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
||||||
|
// See `HTTPSWWWRedirect()`.
|
||||||
|
func HTTPSWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
||||||
|
// Defaults
|
||||||
|
if config.Skipper == nil {
|
||||||
|
config.Skipper = DefaultTrailingSlashConfig.Skipper
|
||||||
|
}
|
||||||
|
if config.Code == 0 {
|
||||||
|
config.Code = DefaultRedirectConfig.Code
|
||||||
|
}
|
||||||
|
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
|
if config.Skipper(c) {
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
host := req.Host()
|
host := req.Host()
|
||||||
uri := req.URI()
|
uri := req.URI()
|
||||||
@ -40,9 +100,29 @@ func HTTPSWWWRedirect() echo.MiddlewareFunc {
|
|||||||
|
|
||||||
// WWWRedirect redirects non WWW requests to WWW.
|
// WWWRedirect redirects non WWW requests to WWW.
|
||||||
// For example, http://labstack.com will be redirect to http://www.labstack.com.
|
// For example, http://labstack.com will be redirect to http://www.labstack.com.
|
||||||
|
//
|
||||||
|
// Usage `Echo#Pre(WWWRedirect())`
|
||||||
func WWWRedirect() echo.MiddlewareFunc {
|
func WWWRedirect() echo.MiddlewareFunc {
|
||||||
|
return WWWRedirectWithConfig(DefaultRedirectConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
||||||
|
// See `WWWRedirect()`.
|
||||||
|
func WWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
||||||
|
// Defaults
|
||||||
|
if config.Skipper == nil {
|
||||||
|
config.Skipper = DefaultTrailingSlashConfig.Skipper
|
||||||
|
}
|
||||||
|
if config.Code == 0 {
|
||||||
|
config.Code = DefaultRedirectConfig.Code
|
||||||
|
}
|
||||||
|
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
|
if config.Skipper(c) {
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
scheme := req.Scheme()
|
scheme := req.Scheme()
|
||||||
host := req.Host()
|
host := req.Host()
|
||||||
@ -57,9 +137,28 @@ func WWWRedirect() echo.MiddlewareFunc {
|
|||||||
|
|
||||||
// NonWWWRedirect redirects WWW request to non WWW.
|
// NonWWWRedirect redirects WWW request to non WWW.
|
||||||
// For example, http://www.labstack.com will be redirect to http://labstack.com.
|
// For example, http://www.labstack.com will be redirect to http://labstack.com.
|
||||||
|
//
|
||||||
|
// Usage `Echo#Pre(NonWWWRedirect())`
|
||||||
func NonWWWRedirect() echo.MiddlewareFunc {
|
func NonWWWRedirect() echo.MiddlewareFunc {
|
||||||
|
return NonWWWRedirectWithConfig(DefaultRedirectConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NonWWWRedirectWithConfig returns a HTTPSRedirect middleware with config.
|
||||||
|
// See `NonWWWRedirect()`.
|
||||||
|
func NonWWWRedirectWithConfig(config RedirectConfig) echo.MiddlewareFunc {
|
||||||
|
if config.Skipper == nil {
|
||||||
|
config.Skipper = DefaultTrailingSlashConfig.Skipper
|
||||||
|
}
|
||||||
|
if config.Code == 0 {
|
||||||
|
config.Code = DefaultRedirectConfig.Code
|
||||||
|
}
|
||||||
|
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
|
if config.Skipper(c) {
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
|
||||||
req := c.Request()
|
req := c.Request()
|
||||||
scheme := req.Scheme()
|
scheme := req.Scheme()
|
||||||
host := req.Host()
|
host := req.Host()
|
||||||
|
@ -74,7 +74,7 @@ func Secure() echo.MiddlewareFunc {
|
|||||||
return SecureWithConfig(DefaultSecureConfig)
|
return SecureWithConfig(DefaultSecureConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SecureWithConfig returns a Secure middleware from config.
|
// SecureWithConfig returns a Secure middleware with config.
|
||||||
// See: `Secure()`.
|
// See: `Secure()`.
|
||||||
func SecureWithConfig(config SecureConfig) echo.MiddlewareFunc {
|
func SecureWithConfig(config SecureConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -28,10 +28,10 @@ var (
|
|||||||
//
|
//
|
||||||
// Usage `Echo#Pre(AddTrailingSlash())`
|
// Usage `Echo#Pre(AddTrailingSlash())`
|
||||||
func AddTrailingSlash() echo.MiddlewareFunc {
|
func AddTrailingSlash() echo.MiddlewareFunc {
|
||||||
return AddTrailingSlashWithConfig(TrailingSlashConfig{})
|
return AddTrailingSlashWithConfig(DefaultTrailingSlashConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddTrailingSlashWithConfig returns a AddTrailingSlash middleware from config.
|
// AddTrailingSlashWithConfig returns a AddTrailingSlash middleware with config.
|
||||||
// See `AddTrailingSlash()`.
|
// See `AddTrailingSlash()`.
|
||||||
func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc {
|
func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
@ -78,7 +78,7 @@ func RemoveTrailingSlash() echo.MiddlewareFunc {
|
|||||||
return RemoveTrailingSlashWithConfig(TrailingSlashConfig{})
|
return RemoveTrailingSlashWithConfig(TrailingSlashConfig{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTrailingSlashWithConfig returns a RemoveTrailingSlash middleware from config.
|
// RemoveTrailingSlashWithConfig returns a RemoveTrailingSlash middleware with config.
|
||||||
// See `RemoveTrailingSlash()`.
|
// See `RemoveTrailingSlash()`.
|
||||||
func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc {
|
func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
@ -50,7 +50,7 @@ func Static(root string) echo.MiddlewareFunc {
|
|||||||
return StaticWithConfig(c)
|
return StaticWithConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StaticWithConfig returns a Static middleware from config.
|
// StaticWithConfig returns a Static middleware with config.
|
||||||
// See `Static()`.
|
// See `Static()`.
|
||||||
func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
||||||
// Defaults
|
// Defaults
|
||||||
|
Reference in New Issue
Block a user