ToMiddleware converts RedirectConfig to middleware or returns an error for invalid configuration
()
| 117 | |
| 118 | // ToMiddleware converts RedirectConfig to middleware or returns an error for invalid configuration |
| 119 | func (config RedirectConfig) ToMiddleware() (echo.MiddlewareFunc, error) { |
| 120 | if config.Skipper == nil { |
| 121 | config.Skipper = DefaultSkipper |
| 122 | } |
| 123 | if config.Code == 0 { |
| 124 | config.Code = http.StatusMovedPermanently |
| 125 | } |
| 126 | if config.redirect == nil { |
| 127 | return nil, errors.New("redirectConfig is missing redirect function") |
| 128 | } |
| 129 | |
| 130 | return func(next echo.HandlerFunc) echo.HandlerFunc { |
| 131 | return func(c *echo.Context) error { |
| 132 | if config.Skipper(c) { |
| 133 | return next(c) |
| 134 | } |
| 135 | |
| 136 | req, scheme := c.Request(), c.Scheme() |
| 137 | host := req.Host |
| 138 | if ok, url := config.redirect(scheme, host, req.RequestURI); ok { |
| 139 | return c.Redirect(config.Code, url) |
| 140 | } |
| 141 | |
| 142 | return next(c) |
| 143 | } |
| 144 | }, nil |
| 145 | } |
| 146 | |
| 147 | var redirectHTTPS = func(scheme, host, uri string) (bool, string) { |
| 148 | if scheme != "https" { |