33 lines
534 B
Go
33 lines
534 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type RedirectConfig struct {
|
|
Skipper Skipper
|
|
URL *url.URL
|
|
Code int
|
|
}
|
|
|
|
func Redirect(config RedirectConfig) Interceptor {
|
|
if config.Skipper == nil {
|
|
config.Skipper = DefaultSkipper
|
|
}
|
|
|
|
if config.Code == 0 {
|
|
config.Code = http.StatusMovedPermanently
|
|
}
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
if config.Skipper(r) {
|
|
next(w, r)
|
|
|
|
return
|
|
}
|
|
|
|
http.RedirectHandler(config.URL.String(), config.Code).ServeHTTP(w, r)
|
|
}
|
|
}
|