aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Hergès <william@herges.fr>2025-10-03 18:55:14 +0200
committerWilliam Hergès <william@herges.fr>2025-10-03 18:55:14 +0200
commitf4a7265be6b4b9c4eedad561a8ed26cd8b7003b0 (patch)
tree4320311f70902bb87f47e2272308fff4ae9e5a98
parent3ba8390e4bc08f5bf6f674c308cecb72466b1140 (diff)
feat(backend): handle 404
-rw-r--r--backend/data.go4
-rw-r--r--backend/home.go15
-rw-r--r--backend/logs.go2
-rw-r--r--backend/router.go2
-rw-r--r--backend/templates/404.html15
-rw-r--r--frontend/index.ts8
-rw-r--r--main.go1
7 files changed, 42 insertions, 5 deletions
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" }}
+<main id="content">
+ <h1>Oh non, je crois que tu t'es perdu :(</h1>
+ <p>
+ Tu as des liens en haut pour retrouver ton chemin :3
+ </p>
+ <p>
+ Si tu penses que c'est un bug, hésite pas à ouvrir une issue sur
+ <a href="https://github.com/anhgelus/small-web/issues">GitHub</a> :D
+ </p>
+ <p>
+ (enfin, sauf si tu as un compte sur ma forge, mais ça m'étonnerait...)
+ </p>
+</main>
+{{ 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"))