From 597e1747470ffb2a7aeb7bc7a62d6b7546e96997 Mon Sep 17 00:00:00 2001 From: Yacine Kanzari Date: Sun, 2 Jun 2024 13:08:11 +0200 Subject: [PATCH] feat: better 404 page --- internal/assets/static/main.css | 14 ++++++++ internal/assets/templates.go | 1 + internal/assets/templates/404.html | 52 ++++++++++++++++++++++++++++++ internal/glance/glance.go | 15 +++++++-- 4 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 internal/assets/templates/404.html diff --git a/internal/assets/static/main.css b/internal/assets/static/main.css index 1e64def1..008c25c1 100644 --- a/internal/assets/static/main.css +++ b/internal/assets/static/main.css @@ -285,6 +285,20 @@ body { font-size: 2rem; } +.page-404-container { + margin: 50px auto; + width: fit-content; + font-size: 2rem; + text-align: center; +} + +.logo-404 { + height: 100%; + line-height: var(--header-height); + font-size: 2rem; + color: var(--color-text-highlight); +} + @keyframes loadingContainerEntrance { from { opacity: 0; diff --git a/internal/assets/templates.go b/internal/assets/templates.go index b8aa6aed..943b3761 100644 --- a/internal/assets/templates.go +++ b/internal/assets/templates.go @@ -13,6 +13,7 @@ import ( var ( PageTemplate = compileTemplate("page.html", "document.html", "page-style-overrides.gotmpl") + Page404Template = compileTemplate("404.html", "document.html", "page-style-overrides.gotmpl") PageContentTemplate = compileTemplate("content.html") CalendarTemplate = compileTemplate("calendar.html", "widget-base.html") BookmarksTemplate = compileTemplate("bookmarks.html", "widget-base.html") diff --git a/internal/assets/templates/404.html b/internal/assets/templates/404.html new file mode 100644 index 00000000..a45c152c --- /dev/null +++ b/internal/assets/templates/404.html @@ -0,0 +1,52 @@ +{{ template "document.html" . }} + +{{ define "document-title" }}{{ .Page.Title }} - Glance{{ end }} + +{{ define "document-head-before" }} + +{{ end }} + +{{ define "document-root-attrs" }}{{ if .App.Config.Theme.Light }}class="light-scheme"{{ end }}{{ end }} +{{ define "document-head-after" }} +{{ template "page-style-overrides.gotmpl" . }} +{{ if ne "" .App.Config.Theme.CustomCSSFile }} + +{{ end }} +{{ end }} + +{{ define "navigation-links" }} +{{ range .App.Config.Pages }} +{{ .Title }} +{{ end }} +{{ end }} + +{{ define "document-body" }} +
+
+
+ +
G
+
+ Page Not Found + +
+
+
+
+ + +{{ end }} diff --git a/internal/glance/glance.go b/internal/glance/glance.go index 653be75a..fe2521af 100644 --- a/internal/glance/glance.go +++ b/internal/glance/glance.go @@ -174,9 +174,18 @@ func (a *Application) HandlePageContentRequest(w http.ResponseWriter, r *http.Re } func (a *Application) HandleNotFound(w http.ResponseWriter, r *http.Request) { - // TODO: add proper not found page - w.WriteHeader(http.StatusNotFound) - w.Write([]byte("Page not found")) + pageData := templateData{ + App: a, + Page: &Page{ + Title: "Page Not Found", + Slug: "404", + }, + } + + var responseBytes bytes.Buffer + assets.Page404Template.Execute(&responseBytes, pageData) + + w.Write(responseBytes.Bytes()) } func FileServerWithCache(fs http.FileSystem, cacheDuration time.Duration) http.Handler {