Handle 405

This commit is contained in:
Joakim Hellsén 2024-02-03 20:00:29 +01:00
commit 6f39a9f4ef
2 changed files with 34 additions and 0 deletions

15
main.go
View file

@ -27,6 +27,7 @@ func main() {
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
r.NotFound(NotFoundHandler) r.NotFound(NotFoundHandler)
r.MethodNotAllowed(MethodNotAllowedHandler)
log.Println("Listening on http://localhost:8000/ <Ctrl-C> to stop") log.Println("Listening on http://localhost:8000/ <Ctrl-C> to stop")
http.ListenAndServe("127.0.0.1:8000", r) 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) 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) { func IndexHandler(w http.ResponseWriter, _ *http.Request) {
renderPage(w, "FeedVault", "FeedVault - A feed archive", "RSS, Atom, Feed, Archive", "TheLovinator", "http://localhost:8000/", "index") renderPage(w, "FeedVault", "FeedVault - A feed archive", "RSS, Atom, Feed, Archive", "TheLovinator", "http://localhost:8000/", "index")
} }

19
templates/405.tmpl Normal file
View file

@ -0,0 +1,19 @@
{{ define "content" }}
<span style="text-align: center;">
<h2>405 - Method Not Allowed</h2>
<p>The method ({{ .Request.Method }}) is not allowed for the requested URL.</p>
</span>
<p>
Extra information:
<ul>
<li>Host: {{ .Request.Host }}</li>
<li>Method: {{ .Request.Method }}</li>
<li>Proto: {{ .Request.Proto }}</li>
<li>RemoteAddr: {{ .Request.RemoteAddr }}</li>
<li>RequestURI: {{ .Request.RequestURI }}</li>
<li>URL: {{ .Request.URL }}</li>
<li>UserAgent: {{ .Request.UserAgent }}</li>
</ul>
{{ end }}