Refactor: Centralize template loading and improve error handling

- Introduced helpers.LoadTemplateFiles() for consistent layout + topbar rendering
- Replaced repeated template.ParseFiles() calls across handlers
- Created generic RenderError(w, r, statusCode) helper
- Replaced old Render403 with flexible RenderError
- Updated AdminOnly middleware to render 403 errors with context
- Added 500.html template for graceful panic fallback
- Prepared structure for future error codes (404, 429, etc.)
This commit is contained in:
2025-04-02 09:12:13 +01:00
parent f5653f737d
commit 2498b33a9c
16 changed files with 69 additions and 106 deletions

View File

@@ -5,7 +5,6 @@ import (
"log"
"net/http"
"strconv"
"text/template"
"synlotto-website/helpers"
"synlotto-website/storage"
@@ -16,13 +15,7 @@ func NotificationsHandler(db *sql.DB) http.HandlerFunc {
data := BuildTemplateData(db, w, r)
context := helpers.TemplateContext(w, r, data)
tmpl := template.Must(template.New("notifications.html").
Funcs(helpers.TemplateFuncs()).
ParseFiles(
"templates/layout.html",
"templates/topbar.html",
"templates/account/notifications.html",
))
tmpl := helpers.LoadTemplateFiles("notifications.html", "templates/account/notifications.html")
err := tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {
@@ -63,13 +56,7 @@ func MarkNotificationReadHandler(db *sql.DB) http.HandlerFunc {
context := helpers.TemplateContext(w, r, data)
context["Notification"] = notification
tmpl := template.Must(template.New("read.html").
Funcs(helpers.TemplateFuncs()).
ParseFiles(
"templates/layout.html",
"templates/topbar.html",
"templates/account/notifications/read.html",
))
tmpl := helpers.LoadTemplateFiles("read.html", "templates/account/notifications/read.html")
err = tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {