Refactor server routes

This commit is contained in:
Andrew-71 2024-10-23 14:11:02 +03:00
parent ca9b6e05b7
commit 5cbb20dcc4
21 changed files with 429 additions and 390 deletions

View file

@ -0,0 +1,32 @@
package routes
import (
"log/slog"
"net/http"
"git.a71.su/Andrew71/hibiscus-txt/internal/templates"
)
// NotFound returns a user-friendly 404 error page.
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
err := templates.Template404.Execute(w, nil)
if err != nil {
slog.Error("error rendering error 404 page", "error", err)
InternalError(w, r)
return
}
}
// InternalError returns a user-friendly 500 error page.
func InternalError(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
err := templates.Template500.Execute(w, nil)
if err != nil { // Well this is awkward
slog.Error("error rendering error 500 page", "error", err)
http.Error(w, "500. Something went *very* wrong.", http.StatusInternalServerError)
return
}
}