From a2f7fd3070513941dded47a7ceb8afce75f337ce Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 14 May 2023 22:24:58 -0500 Subject: add permanent redirects for endpoints --- main.go | 17 +++++++++++++++++ page/default.go | 4 ++-- proxy.go | 5 +++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index f9ae240..37c0fdc 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "fmt" "flag" "errors" + "strings" + "path/filepath" "net/http" "encoding/json" "sanine.net/git/phlox/config" @@ -30,6 +32,21 @@ func main() { Logout(w, r, sessions) }) + http.HandleFunc("/phlox/asset/", func(w http.ResponseWriter, r *http.Request) { + filename := strings.TrimPrefix(r.URL.Path, "/phlox/asset/") + path := filepath.Join(conf.AssetDirectory, filename) + http.ServeFile(w, r, path) + }) + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + w.Header().Add("Location", "/phlox/login") + w.WriteHeader(http.StatusTemporaryRedirect) + } else { + pages.ServeError404(w) + } + }) + // add reverse proxy endpoints for _, e := range conf.Endpoints { addEndpoint(sessions, pages, e) diff --git a/page/default.go b/page/default.go index ea51acc..d13689b 100644 --- a/page/default.go +++ b/page/default.go @@ -82,7 +82,7 @@ func DefaultError404() (string, error) { func DefaultError500() (string, error) { return buildPage(page{ - Title: "500 Not Found", - Body: "

Error 500: Page not found

", + Title: "500 Internal Server Error", + Body: "

Error 500: Internal server error

", }) } diff --git a/proxy.go b/proxy.go index 7be9419..4442fb9 100644 --- a/proxy.go +++ b/proxy.go @@ -31,6 +31,11 @@ func addEndpoint(s *auth.Sessions, pages page.Pages, e config.Endpoint) { Origin: origin, } + http.HandleFunc(e.Path, func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Location", e.Path + "/") + w.WriteHeader(http.StatusPermanentRedirect) + }) + http.HandleFunc(e.Path + "/", func(w http.ResponseWriter, r *http.Request) { log.Infof("REQ: %v", r.URL.Path) proxy(w, r, s, pages, end) -- cgit v1.2.1