diff options
| author | William Hergès <william@herges.fr> | 2025-10-03 18:55:14 +0200 |
|---|---|---|
| committer | William Hergès <william@herges.fr> | 2025-10-03 18:55:14 +0200 |
| commit | f4a7265be6b4b9c4eedad561a8ed26cd8b7003b0 (patch) | |
| tree | 4320311f70902bb87f47e2272308fff4ae9e5a98 /backend | |
| parent | 3ba8390e4bc08f5bf6f674c308cecb72466b1140 (diff) | |
feat(backend): handle 404
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/data.go | 4 | ||||
| -rw-r--r-- | backend/home.go | 15 | ||||
| -rw-r--r-- | backend/logs.go | 2 | ||||
| -rw-r--r-- | backend/router.go | 2 | ||||
| -rw-r--r-- | backend/templates/404.html | 15 |
5 files changed, 34 insertions, 4 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 |
