From a8295dedfa4ed943a9356fd19d0a06647d075f23 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 21 Aug 2024 00:46:31 +0200 Subject: Add a 404 page --- main.go | 53 +++++++++++++++++++++++++++-------------------------- template/404.templ | 16 ++++++++++++++++ 2 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 template/404.templ diff --git a/main.go b/main.go index 138f3cd..1b46513 100644 --- a/main.go +++ b/main.go @@ -24,15 +24,18 @@ import ( var emailDisabled bool -var components = map[string]templ.Component{ - "/": template.Root(), - "/about": template.About(), - "/coins": template.Coins(), - "/coins/designs": template.CoinsDesigns(), - "/coins/designs/nl": template.CoinsDesignsNl(), - "/coins/mintages": template.CoinsMintages(), - "/language": template.Language(), -} +var ( + notFound = template.NotFound() + components = map[string]templ.Component{ + "/": template.Root(), + "/about": template.About(), + "/coins": template.Coins(), + "/coins/designs": template.CoinsDesigns(), + "/coins/designs/nl": template.CoinsDesignsNl(), + "/coins/mintages": template.CoinsMintages(), + "/language": template.Language(), + } +) func main() { lib.InitPrinters() @@ -68,31 +71,29 @@ func main() { } func finalHandler(w http.ResponseWriter, r *http.Request) { - p := r.Context().Value("printer").(lib.Printer) - /* Strip trailing slash from the URL */ path := r.URL.Path if path != "/" && path[len(path)-1] == '/' { path = path[:len(path)-1] } - if c, ok := components[path]; !ok { + c, ok := components[path] + if !ok { w.WriteHeader(http.StatusNotFound) - /* TODO: Create a 404 page */ - fmt.Fprintln(w, p.T("Page not found")) - } else { - /* When a user clicks on the language button to be taken to the - language selection page, we need to set a redirect cookie so - that after selecting a language they are taken back to the - original page they came from. */ - if path == "/language" { - http.SetCookie(w, &http.Cookie{ - Name: "redirect", - Value: cmp.Or(r.Referer(), "/"), - }) - } - template.Base(c).Render(r.Context(), w) + c = notFound + } + + /* When a user clicks on the language button to be taken to the + language selection page, we need to set a redirect cookie so + that after selecting a language they are taken back to the + original page they came from. */ + if path == "/language" { + http.SetCookie(w, &http.Cookie{ + Name: "redirect", + Value: cmp.Or(r.Referer(), "/"), + }) } + template.Base(c).Render(r.Context(), w) } func i18nHandler(next http.Handler) http.Handler { diff --git a/template/404.templ b/template/404.templ new file mode 100644 index 0000000..9a03b43 --- /dev/null +++ b/template/404.templ @@ -0,0 +1,16 @@ +package template + +import "git.thomasvoss.com/euro-cash.eu/lib" + +templ NotFound() { + {{ p := ctx.Value("printer").(lib.Printer) }} +
+ @navbar() +

{ p.T("Page Not Found") }

+
+
+

+ @templ.Raw(p.T("The page you were looking for does not exist. If you believe this is a mistake then don’t hesitate to contact @onetruemangoman on Discord or to email us at %s.", contactEmail)) +

+
+} -- cgit v1.2.3