From af3b43dfc4ac678c36279eab024918ddc9b02018 Mon Sep 17 00:00:00 2001 From: Alexey Yerin Date: Mon, 7 Dec 2020 11:30:33 +0300 Subject: [PATCH] feat: 404 page --- main.go | 9 ++++++--- templates/not-found.html | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 templates/not-found.html diff --git a/main.go b/main.go index a009c0e..dbedeae 100644 --- a/main.go +++ b/main.go @@ -99,10 +99,8 @@ func main() { err = pool.QueryRow(context.Background(), "select contents from pastes where id=$1", id).Scan(&contents) if err != nil { - // TODO: Add proper 404 page w.WriteHeader(404) - - log.Printf("Paste #%d doesn't exist: %v\n", id, err) + tmpl.ExecuteTemplate(w, "not-found", nil) return } @@ -110,6 +108,11 @@ func main() { tmpl.ExecuteTemplate(w, "paste", Paste{id, contents}) }) + r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + w.WriteHeader(404) + tmpl.ExecuteTemplate(w, "not-found", nil) + }) + // Static files fs := http.FileServer(http.Dir("static/")) r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs)) diff --git a/templates/not-found.html b/templates/not-found.html new file mode 100644 index 0000000..fd492e9 --- /dev/null +++ b/templates/not-found.html @@ -0,0 +1,19 @@ +{{ define "not-found" }} + + + + {{ template "head" }} + + + {{ template "header" }} + +
+

Oops...

+

+ The page you're looking for doesn't exist. Would you like to go to + homepage instead? +

+
+ + +{{ end }} -- 2.34.2