summaryrefslogtreecommitdiffhomepage
path: root/main.go
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-08-21 00:46:31 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-08-21 00:46:31 +0200
commita8295dedfa4ed943a9356fd19d0a06647d075f23 (patch)
tree7821e348dd739a49eff93c13d26cdea8bee50652 /main.go
parent846aab9ffc5d19672faf6992375512d44da880c7 (diff)
Add a 404 page
Diffstat (limited to 'main.go')
-rw-r--r--main.go53
1 files changed, 27 insertions, 26 deletions
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 {