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

@@ -2,7 +2,6 @@ package handlers
import (
"database/sql"
"html/template"
"log"
"net/http"
"synlotto-website/helpers"
@@ -46,10 +45,8 @@ func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc {
}
context["AuditLogs"] = logs
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/logs/access_log.html",
))
tmpl := helpers.LoadTemplateFiles("access_log.html", "templates/admin/logs/access_log.html")
_ = tmpl.ExecuteTemplate(w, "layout", context)
})
}
@@ -84,10 +81,7 @@ func AuditLogHandler(db *sql.DB) http.HandlerFunc {
context["AuditLogs"] = logs
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/logs/audit.html",
))
tmpl := helpers.LoadTemplateFiles("audit.html", "templates/admin/logs/audit.html")
err = tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {

View File

@@ -2,7 +2,6 @@ package handlers
import (
"database/sql"
"html/template"
"log"
"net/http"
@@ -55,10 +54,7 @@ func AdminDashboardHandler(db *sql.DB) http.HandlerFunc {
}
context["MatchLogs"] = logs
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/dashboard.html",
))
tmpl := helpers.LoadTemplateFiles("dashboard.html", "templates/admin/dashboard.html")
err = tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {

View File

@@ -2,7 +2,6 @@ package handlers
import (
"database/sql"
"html/template"
"log"
"net/http"
@@ -31,10 +30,8 @@ func NewDrawHandler(db *sql.DB) http.HandlerFunc {
return
}
tmpl := template.Must(template.New("new_draw").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/draws/new_draw.html",
))
tmpl := helpers.LoadTemplateFiles("new_draw", "templates/admin/draws/new_draw.html")
tmpl.ExecuteTemplate(w, "layout", context)
})
}
@@ -103,10 +100,8 @@ func ListDrawsHandler(db *sql.DB) http.HandlerFunc {
context["Draws"] = draws
tmpl := template.Must(template.New("draw_list").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/draws/list.html",
))
tmpl := helpers.LoadTemplateFiles("list.html", "templates/admin/draws/list.html")
tmpl.ExecuteTemplate(w, "layout", context)
})
}

View File

@@ -3,7 +3,6 @@ package handlers
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
"net/url"
@@ -18,7 +17,6 @@ func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
context := helpers.TemplateContext(w, r, models.TemplateData{})
// Inject flash message if available
if flash := r.URL.Query().Get("flash"); flash != "" {
context["Flash"] = flash
}
@@ -69,16 +67,11 @@ func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
flashMsg = "⚠️ Unknown action."
}
// Redirect back with flash message
http.Redirect(w, r, "/admin/triggers?flash="+url.QueryEscape(flashMsg), http.StatusSeeOther)
return
}
// Render the admin trigger page
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/triggers.html",
))
tmpl := helpers.LoadTemplateFiles("triggers.html", "templates/admin/triggers.html")
err := tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {

View File

@@ -3,7 +3,6 @@ package handlers
import (
"database/sql"
"fmt"
"html/template"
"net/http"
"strconv"
"synlotto-website/helpers"
@@ -13,10 +12,8 @@ import (
func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/draws/prizes/add_prizes.html",
))
tmpl := helpers.LoadTemplateFiles("add_prizes.html", "templates/admin/draws/prizes/add_prizes.html")
tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{}))
return
}
@@ -47,10 +44,8 @@ func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
func ModifyPrizesHandler(db *sql.DB) http.HandlerFunc {
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/admin/draws/prizes/modify_prizes.html",
))
tmpl := helpers.LoadTemplateFiles("modify_prizes.html", "templates/admin/draws/prizes/modify_prizes.html")
tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{}))
return
}