make redirects more ergonomic
2 files changed, 18 insertions(+), 4 deletions(-)
A internal/http/redirect.go
@@ -0,0 +1,14 @@ +package http + +import ( + "net/http" + "net/url" +) + +func Redirect(w http.ResponseWriter, r *http.Request, url *url.URL, code int) { + http.Redirect(w, r, url.String(), code) +} + +func RedirectHandler(url *url.URL, code int) http.Handler { + return http.RedirectHandler(url.String(), code) +}
M internal/website/mux.go → internal/website/mux.go
@@ -212,7 +212,7 @@ return nil }, ) const oidcPath = "/.well-known/openid-configuration" - mux.Handle(oidcPath, http.RedirectHandler(cfg.OIDCHost.JoinPath(oidcPath).String(), 302)) + mux.Handle(oidcPath, ihttp.RedirectHandler(cfg.OIDCHost.JoinPath(oidcPath), 302)) website.config = cfg website.App.Handler = mux@@ -231,16 +231,16 @@ mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { switch { case slices.Contains(website.config.Domains, r.Host): path, _ := website.reader.CanonicalisePath(r.URL.Path) - http.Redirect( + ihttp.Redirect( w, r, - website.config.BaseURL.JoinPath(path).String(), + website.config.BaseURL.JoinPath(path), http.StatusMovedPermanently, ) case re.MatchString(r.Host): url := website.config.BaseURL.JoinPath() url.Host = re.ReplaceAllString(r.Host, replace) - http.Redirect(w, r, url.String(), http.StatusTemporaryRedirect) + ihttp.Redirect(w, r, url, http.StatusTemporaryRedirect) case true: http.NotFound(w, r) }