Refactor: Recover middleware now uses RenderError + add full notifications view

- Replaced http.Error with helpers.RenderError in Recover middleware
- Custom 500.html now rendered with layout and topbar on panic
- RenderError gracefully checks template existence and falls back to plain response
- Added /account/notifications full view page (index)
- Linked "Back to notifications" from notification read view
- Fixed typo in template path for notifications/index.html
- Improved layout consistency across error and account pages
This commit is contained in:
2025-04-02 09:54:20 +01:00
parent 2498b33a9c
commit ab1d9abc72
6 changed files with 49 additions and 22 deletions

View File

@@ -1,24 +1,40 @@
package helpers
// ToDo should be a handler?
import (
"fmt"
"log"
"net/http"
"os"
"synlotto-website/models"
)
func RenderError(w http.ResponseWriter, r *http.Request, statusCode int) {
log.Printf("⚙️ RenderError called with status: %d", statusCode)
context := TemplateContext(w, r, models.TemplateData{})
page := fmt.Sprintf("templates/error/%d.html", statusCode)
tmpl := LoadTemplateFiles(fmt.Sprintf("%d.html", statusCode), page)
pagePath := fmt.Sprintf("templates/error/%d.html", statusCode)
log.Printf("📄 Checking for template file: %s", pagePath)
if _, err := os.Stat(pagePath); err != nil {
log.Printf("🚫 Template file missing: %s", err)
http.Error(w, http.StatusText(statusCode), statusCode)
return
}
log.Println("✅ Template file found, loading...")
tmpl := LoadTemplateFiles(fmt.Sprintf("%d.html", statusCode), pagePath)
w.WriteHeader(statusCode)
err := tmpl.ExecuteTemplate(w, "layout", context)
if err != nil {
log.Printf("❌ Failed to render error page for %d: %v", statusCode, err)
log.Printf("❌ Failed to render error page layout: %v", err)
http.Error(w, http.StatusText(statusCode), statusCode)
return
}
log.Println("✅ Successfully rendered 500 page")
}
//ToDo Pages.go /template.go to be merged?