From 6f39a9f4ef20b78bc9b467e0f9f32ea0f784a01e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sat, 3 Feb 2024 20:00:29 +0100 Subject: [PATCH] Handle 405 --- main.go | 15 +++++++++++++++ templates/405.tmpl | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 templates/405.tmpl diff --git a/main.go b/main.go index b477889..2a52fce 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ func main() { r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) r.NotFound(NotFoundHandler) + r.MethodNotAllowed(MethodNotAllowedHandler) log.Println("Listening on http://localhost:8000/ to stop") http.ListenAndServe("127.0.0.1:8000", r) @@ -85,6 +86,20 @@ func NotFoundHandler(w http.ResponseWriter, r *http.Request) { t.ExecuteTemplate(w, "base", data) } +func MethodNotAllowedHandler(w http.ResponseWriter, r *http.Request) { + data := Data{ + Request: r, + } + data.GetDatabaseSizeAndFeedCount() + t, err := template.ParseFiles("templates/base.tmpl", "templates/405.tmpl") + if err != nil { + http.Error(w, fmt.Sprintf("Internal Server Error: %v", err), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusMethodNotAllowed) + t.ExecuteTemplate(w, "base", data) +} + func IndexHandler(w http.ResponseWriter, _ *http.Request) { renderPage(w, "FeedVault", "FeedVault - A feed archive", "RSS, Atom, Feed, Archive", "TheLovinator", "http://localhost:8000/", "index") } diff --git a/templates/405.tmpl b/templates/405.tmpl new file mode 100644 index 0000000..3278c61 --- /dev/null +++ b/templates/405.tmpl @@ -0,0 +1,19 @@ +{{ define "content" }} + +

405 - Method Not Allowed

+

The method ({{ .Request.Method }}) is not allowed for the requested URL.

+
+ +

+Extra information: +

+ +{{ end }}