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"
@@ -18,11 +17,7 @@ func NewDraw(db *sql.DB) http.HandlerFunc {
context["Page"] = "new_draw"
context["Data"] = nil
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
"templates/layout.html",
"templates/topbar.html",
"templates/new_draw.html",
))
tmpl := helpers.LoadTemplateFiles("new_draw.html", "templates/new_draw.html") // ToDo: may need removing or moving add draw should be admin functionality and only when manually required. Potential live drawing of numbers in the future.
err := tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {
@@ -48,7 +43,6 @@ func Submit(w http.ResponseWriter, r *http.Request) {
Thunderball: helpers.Atoi(r.FormValue("thunderball")),
}
// For now you're appending to memory - can replace with DB insert later
Draws = append(Draws, draw)
log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",