🧑💻 Created basic redirect middleware
This commit is contained in:
parent
f9ab36076a
commit
7f294a1f80
|
@ -0,0 +1,32 @@
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue