diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-08-21 00:46:31 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-08-21 00:46:31 +0200 |
commit | a8295dedfa4ed943a9356fd19d0a06647d075f23 (patch) | |
tree | 7821e348dd739a49eff93c13d26cdea8bee50652 | |
parent | 846aab9ffc5d19672faf6992375512d44da880c7 (diff) |
Add a 404 page
-rw-r--r-- | main.go | 53 | ||||
-rw-r--r-- | template/404.templ | 16 |
2 files changed, 43 insertions, 26 deletions
@@ -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) }} + <header> + @navbar() + <h1>{ p.T("Page Not Found") }</h1> + </header> + <main> + <p> + @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)) + </p> + </main> +} |