diff --git a/handlers/notifications.go b/handlers/notifications.go index e37d8b0..d0c9fed 100644 --- a/handlers/notifications.go +++ b/handlers/notifications.go @@ -15,7 +15,7 @@ func NotificationsHandler(db *sql.DB) http.HandlerFunc { data := BuildTemplateData(db, w, r) context := helpers.TemplateContext(w, r, data) - tmpl := helpers.LoadTemplateFiles("notifications.html", "templates/account/notifications.html") + tmpl := helpers.LoadTemplateFiles("index.html", "templates/account/notifications/index.html") err := tmpl.ExecuteTemplate(w, "layout", context) if err != nil { diff --git a/helpers/pages.go b/helpers/pages.go index e4074bf..4e6ba84 100644 --- a/helpers/pages.go +++ b/helpers/pages.go @@ -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? diff --git a/helpers/template.go b/helpers/template.go index e935d16..ec8025c 100644 --- a/helpers/template.go +++ b/helpers/template.go @@ -2,6 +2,7 @@ package helpers import ( "html/template" + "log" "net/http" "strings" @@ -69,6 +70,7 @@ func LoadTemplateFiles(name string, files ...string) *template.Template { } all := append(shared, files...) + log.Printf("π Loading templates: %v", all) return template.Must(template.New(name).Funcs(TemplateFuncs()).ParseFiles(all...)) } diff --git a/main.go b/main.go index 81d2e66..479ac6b 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,7 @@ func setupAccountRoutes(mux *http.ServeMux, db *sql.DB) { mux.HandleFunc("/signup", middleware.Auth(false)(handlers.Signup)) mux.HandleFunc("/account/tickets/add_ticket", handlers.AddTicket(db)) mux.HandleFunc("/account/tickets/my_tickets", handlers.GetMyTickets(db)) + mux.HandleFunc("/account/notifications", middleware.Auth(true)(handlers.NotificationsHandler(db))) mux.HandleFunc("/account/notifications/read", middleware.Auth(true)(handlers.MarkNotificationReadHandler(db))) } diff --git a/middleware/recover.go b/middleware/recover.go index 3b075e6..55f2354 100644 --- a/middleware/recover.go +++ b/middleware/recover.go @@ -4,6 +4,7 @@ import ( "log" "net/http" "runtime/debug" + "synlotto-website/helpers" ) func Recover(next http.Handler) http.Handler { @@ -12,7 +13,8 @@ func Recover(next http.Handler) http.Handler { if rec := recover(); rec != nil { log.Printf("π₯ Recovered from panic: %v\n%s", rec, debug.Stack()) - http.Error(w, "Internal server error", http.StatusInternalServerError) + // β Call your custom template-based fallback + helpers.RenderError(w, r, http.StatusInternalServerError) } }() next.ServeHTTP(w, r) diff --git a/templates/account/notifications/index.html b/templates/account/notifications/index.html index d6c6dca..1a28cee 100644 --- a/templates/account/notifications/index.html +++ b/templates/account/notifications/index.html @@ -1,23 +1,29 @@ -{{ define "notifications" }} +{{ define "content" }}