🧑‍💻 Created basic redirect middleware

This commit is contained in:
Maxim Lebedev 2023-11-13 09:12:36 +06:00
parent f9ab36076a
commit 7f294a1f80
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 32 additions and 0 deletions

View File

@ -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)
}
}