From f4a7265be6b4b9c4eedad561a8ed26cd8b7003b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Fri, 3 Oct 2025 18:55:14 +0200 Subject: feat(backend): handle 404 --- backend/data.go | 4 ++++ backend/home.go | 15 +++++++++++++-- backend/logs.go | 2 +- backend/router.go | 2 +- backend/templates/404.html | 15 +++++++++++++++ frontend/index.ts | 8 +++++++- main.go | 1 + 7 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 backend/templates/404.html diff --git a/backend/data.go b/backend/data.go index cc61941..fe5aa96 100644 --- a/backend/data.go +++ b/backend/data.go @@ -30,6 +30,10 @@ type data struct { Quote string } +func (d *data) SetData(data *data) { + *d = *data +} + func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string, custom dataUsable) { cfg := r.Context().Value(configKey).(*Config) if d.Domain == "" { diff --git a/backend/home.go b/backend/home.go index b2a6dfa..69bb156 100644 --- a/backend/home.go +++ b/backend/home.go @@ -41,6 +41,17 @@ func HandleHome(r *chi.Mux) { }) } +func Handle404(r *chi.Mux) { + r.NotFound(notFound) +} + +func notFound(w http.ResponseWriter, r *http.Request) { + d := new(data) + d.title = "404" + w.WriteHeader(http.StatusNotFound) + d.handleGeneric(w, r, "404", d) +} + type rootData struct { *data Content template.HTML @@ -71,7 +82,7 @@ func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) { b, err := os.ReadFile(path) if err != nil { if os.IsNotExist(err) { - http.NotFoundHandler().ServeHTTP(w, r) + notFound(w, r) return } panic(err) @@ -109,7 +120,7 @@ func handleGenericLogsDisplay(w http.ResponseWriter, r *http.Request) *homeData d.CurrentPage = page d.PagesNumber = len(sortedLogs)/maxLogsPerPage + 1 if d.PagesNumber < page { - http.NotFoundHandler().ServeHTTP(w, r) + notFound(w, r) return nil } d.Logs = sortedLogs[(page-1)*maxLogsPerPage : min(page*maxLogsPerPage, len(sortedLogs))] diff --git a/backend/logs.go b/backend/logs.go index c663ffb..3b5bd70 100644 --- a/backend/logs.go +++ b/backend/logs.go @@ -137,7 +137,7 @@ func handleLog(w http.ResponseWriter, r *http.Request) { d = new(logData) d.data = new(data) if ok = parseLog(d, path, slug); !ok { - http.NotFoundHandler().ServeHTTP(w, r) + notFound(w, r) return } } diff --git a/backend/router.go b/backend/router.go index 22c77a4..adc28fc 100644 --- a/backend/router.go +++ b/backend/router.go @@ -16,7 +16,7 @@ import ( ) const ( - Version = "0.1.0" + Version = "0.2.0" configKey = "config" isUpdateKey = "is_update" ) diff --git a/backend/templates/404.html b/backend/templates/404.html new file mode 100644 index 0000000..0655652 --- /dev/null +++ b/backend/templates/404.html @@ -0,0 +1,15 @@ +{{ define "body" }} +
+

Oh non, je crois que tu t'es perdu :(

+

+ Tu as des liens en haut pour retrouver ton chemin :3 +

+

+ Si tu penses que c'est un bug, hésite pas à ouvrir une issue sur + GitHub :D +

+

+ (enfin, sauf si tu as un compte sur ma forge, mais ça m'étonnerait...) +

+
+{{ end }} \ No newline at end of file diff --git a/frontend/index.ts b/frontend/index.ts index 316caaf..c337e43 100644 --- a/frontend/index.ts +++ b/frontend/index.ts @@ -18,9 +18,15 @@ function setupAnchors() { // updating history and window title document.addEventListener("htmx:afterSettle", e => { const title = e.detail.xhr.getResponseHeader("Updated-Title") - if (title?.length != 0) document.title = title + if (title?.length !== 0) document.title = title window.history.pushState({}, "", e.detail.pathInfo.finalRequestPath) setupAnchors() }) +document.body.addEventListener('htmx:beforeSwap', function(e) { + if(e.detail.xhr.status !== 404) return + e.detail.shouldSwap = true; + e.detail.isError = false; +}) + setupAnchors() diff --git a/main.go b/main.go index 3fba254..47a9e22 100644 --- a/main.go +++ b/main.go @@ -74,6 +74,7 @@ func main() { backend.HandleHome(r) backend.HandleRoot(r, cfg) backend.HandleLogs(r) + backend.Handle404(r) if dev { backend.HandleStaticFiles(r, "/assets", os.DirFS("dist")) -- cgit v1.2.3