From 6fe6c856cd3f8b21e4421d963139620ccd81e61b Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 12 Aug 2023 23:51:26 +0400 Subject: [PATCH] Show Go back on error page, need to pass a referer URL for it. --- cmd/dwelling-home/main.go | 4 ++-- internal/http/api_guestbook_handlers.go | 9 ++++++--- internal/http/api_mindflow_handlers.go | 2 +- internal/http/web_handlers.go | 10 +++++----- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cmd/dwelling-home/main.go b/cmd/dwelling-home/main.go index 60a87b6..dcd2baa 100644 --- a/cmd/dwelling-home/main.go +++ b/cmd/dwelling-home/main.go @@ -74,8 +74,8 @@ func main() { r := httpr.New() - r.NotFoundHandler = func(w http.ResponseWriter, _ *http.Request) { - web.ErrorXXX("/ Not Found", "", "", http.StatusNotFound, w) + r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) { + web.ErrorXXX("/ Not Found", "", "", r.Referer(), http.StatusNotFound, w) } r.ServeStatic("/assets/*filepath", web.Assets()) diff --git a/internal/http/api_guestbook_handlers.go b/internal/http/api_guestbook_handlers.go index 1461594..c49c2c9 100644 --- a/internal/http/api_guestbook_handlers.go +++ b/internal/http/api_guestbook_handlers.go @@ -29,7 +29,8 @@ func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) { r.ParseForm() if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) { - Error(w, http.StatusForbidden, "Wrong answer given.", "Here's your message:"+r.FormValue("message")) + Error(w, http.StatusForbidden, "Wrong answer given.", + "Here's your message:"+r.FormValue("message"), r.Referer()) return } @@ -42,13 +43,15 @@ func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) { entry, err = justguestbook.NewEntry(r.FormValue("name"), message, r.FormValue("website"), r.FormValue("hide_website") != "") if err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "Here's your message:"+r.FormValue("message")) + Error(w, http.StatusInternalServerError, err.Error(), + "Here's your message:"+r.FormValue("message"), r.Referer()) return } } if err = h.db.NewEntry(entry); err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "Here's your message:"+r.FormValue("message")) + Error(w, http.StatusInternalServerError, err.Error(), + "Here's your message:"+r.FormValue("message"), r.Referer()) return } diff --git a/internal/http/api_mindflow_handlers.go b/internal/http/api_mindflow_handlers.go index b44b45d..23342f8 100644 --- a/internal/http/api_mindflow_handlers.go +++ b/internal/http/api_mindflow_handlers.go @@ -51,7 +51,7 @@ func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) { if err = h.db.NewPost(post); err != nil { msg := strings.Join([]string{"Title:", r.FormValue("title"), "| Body:", r.FormValue("body")}, " ") - Error(w, http.StatusInternalServerError, err.Error(), msg) + Error(w, http.StatusInternalServerError, err.Error(), msg, r.Referer()) return } diff --git a/internal/http/web_handlers.go b/internal/http/web_handlers.go index 1e5ffdb..d8be2f0 100644 --- a/internal/http/web_handlers.go +++ b/internal/http/web_handlers.go @@ -70,13 +70,13 @@ func (h *Handlers) Mindflow(w http.ResponseWriter, r *http.Request) { func (h *Handlers) MindflowAdmin(w http.ResponseWriter, r *http.Request) { posts, err := h.mindflowDB.Posts() if err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "failed to load posts") + Error(w, http.StatusInternalServerError, err.Error(), "failed to load posts", r.Referer()) return } categories, err := h.mindflowDB.Categories() if err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "failed to load categories") + Error(w, http.StatusInternalServerError, err.Error(), "failed to load categories", r.Referer()) return } @@ -153,7 +153,7 @@ func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) { entriesCount, _ := h.guestbookDB.Count() entries, err := h.guestbookDB.Entries(1, entriesCount) if err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "cannot load gb") + Error(w, http.StatusInternalServerError, err.Error(), "cannot load gb", r.Referer()) return } for _, entry := range entries { @@ -187,8 +187,8 @@ func SitemapXml(w http.ResponseWriter, r *http.Request) { w.Write(data) } -func Error(w http.ResponseWriter, code int, reason, message string) { - web.ErrorXXX("/ "+http.StatusText(code), reason, message, code, w) +func Error(w http.ResponseWriter, code int, reason, message, referer string) { + web.ErrorXXX("/ "+http.StatusText(code), reason, message, referer, code, w) } func cleanupNewlines(text string) (out string) {