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:
@@ -1,7 +1,6 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
@@ -18,12 +17,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
tmpl := helpers.LoadTemplateFiles("login.html", "templates/account/login.html")
|
||||||
tmpl := template.Must(template.New("login.html").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
|
||||||
"templates/layout.html",
|
|
||||||
"templates/topbar.html",
|
|
||||||
"templates/account/login.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
context["csrfField"] = csrf.TemplateField(r)
|
context["csrfField"] = csrf.TemplateField(r)
|
||||||
@@ -101,10 +95,7 @@ func Logout(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func Signup(w http.ResponseWriter, r *http.Request) {
|
func Signup(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
tmpl := template.Must(template.ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("signup.html", "templates/account/signup.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/account/signup.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
|
tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
|
||||||
"csrfField": csrf.TemplateField(r),
|
"csrfField": csrf.TemplateField(r),
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
@@ -46,10 +45,8 @@ func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
context["AuditLogs"] = logs
|
context["AuditLogs"] = logs
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("access_log.html", "templates/admin/logs/access_log.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/logs/access_log.html",
|
|
||||||
))
|
|
||||||
_ = tmpl.ExecuteTemplate(w, "layout", context)
|
_ = tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -84,10 +81,7 @@ func AuditLogHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
|
|
||||||
context["AuditLogs"] = logs
|
context["AuditLogs"] = logs
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("audit.html", "templates/admin/logs/audit.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/logs/audit.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", context)
|
err = tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -55,10 +54,7 @@ func AdminDashboardHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
context["MatchLogs"] = logs
|
context["MatchLogs"] = logs
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("dashboard.html", "templates/admin/dashboard.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/dashboard.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", context)
|
err = tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -31,10 +30,8 @@ func NewDrawHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl := template.Must(template.New("new_draw").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("new_draw", "templates/admin/draws/new_draw.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/draws/new_draw.html",
|
|
||||||
))
|
|
||||||
tmpl.ExecuteTemplate(w, "layout", context)
|
tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -103,10 +100,8 @@ func ListDrawsHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
|
|
||||||
context["Draws"] = draws
|
context["Draws"] = draws
|
||||||
|
|
||||||
tmpl := template.Must(template.New("draw_list").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("list.html", "templates/admin/draws/list.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/draws/list.html",
|
|
||||||
))
|
|
||||||
tmpl.ExecuteTemplate(w, "layout", context)
|
tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -18,7 +17,6 @@ func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
|
|
||||||
// Inject flash message if available
|
|
||||||
if flash := r.URL.Query().Get("flash"); flash != "" {
|
if flash := r.URL.Query().Get("flash"); flash != "" {
|
||||||
context["Flash"] = flash
|
context["Flash"] = flash
|
||||||
}
|
}
|
||||||
@@ -69,16 +67,11 @@ func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
flashMsg = "⚠️ Unknown action."
|
flashMsg = "⚠️ Unknown action."
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect back with flash message
|
|
||||||
http.Redirect(w, r, "/admin/triggers?flash="+url.QueryEscape(flashMsg), http.StatusSeeOther)
|
http.Redirect(w, r, "/admin/triggers?flash="+url.QueryEscape(flashMsg), http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render the admin trigger page
|
tmpl := helpers.LoadTemplateFiles("triggers.html", "templates/admin/triggers.html")
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/triggers.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
@@ -13,10 +12,8 @@ import (
|
|||||||
func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
|
func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
|
||||||
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("add_prizes.html", "templates/admin/draws/prizes/add_prizes.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/draws/prizes/add_prizes.html",
|
|
||||||
))
|
|
||||||
tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{}))
|
tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -47,10 +44,8 @@ func AddPrizesHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
func ModifyPrizesHandler(db *sql.DB) http.HandlerFunc {
|
func ModifyPrizesHandler(db *sql.DB) http.HandlerFunc {
|
||||||
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
return helpers.AuthMiddleware(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("modify_prizes.html", "templates/admin/draws/prizes/modify_prizes.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/admin/draws/prizes/modify_prizes.html",
|
|
||||||
))
|
|
||||||
tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{}))
|
tmpl.ExecuteTemplate(w, "layout", helpers.TemplateContext(w, r, models.TemplateData{}))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -18,11 +17,7 @@ func NewDraw(db *sql.DB) http.HandlerFunc {
|
|||||||
context["Page"] = "new_draw"
|
context["Page"] = "new_draw"
|
||||||
context["Data"] = nil
|
context["Data"] = nil
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
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.
|
||||||
"templates/layout.html",
|
|
||||||
"templates/topbar.html",
|
|
||||||
"templates/new_draw.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -48,7 +43,6 @@ func Submit(w http.ResponseWriter, r *http.Request) {
|
|||||||
Thunderball: helpers.Atoi(r.FormValue("thunderball")),
|
Thunderball: helpers.Atoi(r.FormValue("thunderball")),
|
||||||
}
|
}
|
||||||
|
|
||||||
// For now you're appending to memory - can replace with DB insert later
|
|
||||||
Draws = append(Draws, draw)
|
Draws = append(Draws, draw)
|
||||||
|
|
||||||
log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
|
log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
@@ -13,11 +12,7 @@ func Home(db *sql.DB) http.HandlerFunc {
|
|||||||
data := BuildTemplateData(db, w, r)
|
data := BuildTemplateData(db, w, r)
|
||||||
context := helpers.TemplateContext(w, r, data)
|
context := helpers.TemplateContext(w, r, data)
|
||||||
|
|
||||||
tmpl := template.Must(template.New("index.html").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("index.html", "templates/index.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/topbar.html",
|
|
||||||
"templates/index.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
"synlotto-website/storage"
|
"synlotto-website/storage"
|
||||||
@@ -16,13 +15,7 @@ func NotificationsHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
data := BuildTemplateData(db, w, r)
|
data := BuildTemplateData(db, w, r)
|
||||||
context := helpers.TemplateContext(w, r, data)
|
context := helpers.TemplateContext(w, r, data)
|
||||||
|
|
||||||
tmpl := template.Must(template.New("notifications.html").
|
tmpl := helpers.LoadTemplateFiles("notifications.html", "templates/account/notifications.html")
|
||||||
Funcs(helpers.TemplateFuncs()).
|
|
||||||
ParseFiles(
|
|
||||||
"templates/layout.html",
|
|
||||||
"templates/topbar.html",
|
|
||||||
"templates/account/notifications.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err := tmpl.ExecuteTemplate(w, "layout", context)
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -63,13 +56,7 @@ func MarkNotificationReadHandler(db *sql.DB) http.HandlerFunc {
|
|||||||
context := helpers.TemplateContext(w, r, data)
|
context := helpers.TemplateContext(w, r, data)
|
||||||
context["Notification"] = notification
|
context["Notification"] = notification
|
||||||
|
|
||||||
tmpl := template.Must(template.New("read.html").
|
tmpl := helpers.LoadTemplateFiles("read.html", "templates/account/notifications/read.html")
|
||||||
Funcs(helpers.TemplateFuncs()).
|
|
||||||
ParseFiles(
|
|
||||||
"templates/layout.html",
|
|
||||||
"templates/topbar.html",
|
|
||||||
"templates/account/notifications/read.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", context)
|
err = tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"html/template"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -110,10 +109,7 @@ func ResultsThunderball(db *sql.DB) http.HandlerFunc {
|
|||||||
noResultsMsg = "No results found for \"" + query + "\""
|
noResultsMsg = "No results found for \"" + query + "\""
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("thunderball.html", "templates/results/thunderball.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/results/thunderball.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
|
err = tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
|
||||||
"Results": results,
|
"Results": results,
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -44,10 +43,7 @@ func AddTicket(db *sql.DB) http.HandlerFunc {
|
|||||||
context["csrfField"] = csrf.TemplateField(r)
|
context["csrfField"] = csrf.TemplateField(r)
|
||||||
context["DrawDates"] = drawDates
|
context["DrawDates"] = drawDates
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("add_ticket.html", "templates/account/tickets/add_ticket.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/account/tickets/add_ticket.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", context)
|
err = tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -362,10 +358,7 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
|
|||||||
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
context := helpers.TemplateContext(w, r, models.TemplateData{})
|
||||||
context["Tickets"] = tickets
|
context["Tickets"] = tickets
|
||||||
|
|
||||||
tmpl := template.Must(template.New("").Funcs(helpers.TemplateFuncs()).ParseFiles(
|
tmpl := helpers.LoadTemplateFiles("my_tickets.html", "templates/account/tickets/my_tickets.html")
|
||||||
"templates/layout.html",
|
|
||||||
"templates/account/tickets/my_tickets.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
err = tmpl.ExecuteTemplate(w, "layout", context)
|
err = tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,18 +2,23 @@ package helpers
|
|||||||
|
|
||||||
// ToDo should be a handler?
|
// ToDo should be a handler?
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"synlotto-website/models"
|
"synlotto-website/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Render403(w http.ResponseWriter, r *http.Request) {
|
func RenderError(w http.ResponseWriter, r *http.Request, statusCode int) {
|
||||||
context := TemplateContext(w, r, models.TemplateData{})
|
context := TemplateContext(w, r, models.TemplateData{})
|
||||||
tmpl := template.Must(template.New("").Funcs(TemplateFuncs()).ParseFiles(
|
|
||||||
"templates/layout.html",
|
|
||||||
"templates/error/403.html",
|
|
||||||
))
|
|
||||||
|
|
||||||
tmpl.ExecuteTemplate(w, "layout", context)
|
page := fmt.Sprintf("templates/error/%d.html", statusCode)
|
||||||
|
tmpl := LoadTemplateFiles(fmt.Sprintf("%d.html", statusCode), page)
|
||||||
|
|
||||||
|
w.WriteHeader(statusCode)
|
||||||
|
err := tmpl.ExecuteTemplate(w, "layout", context)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("❌ Failed to render error page for %d: %v", statusCode, err)
|
||||||
|
http.Error(w, http.StatusText(statusCode), statusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ func TemplateContext(w http.ResponseWriter, r *http.Request, data models.Templat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TemplateFuncs provides helper functions to be used in templates.
|
|
||||||
func TemplateFuncs() template.FuncMap {
|
func TemplateFuncs() template.FuncMap {
|
||||||
return template.FuncMap{
|
return template.FuncMap{
|
||||||
"plus1": func(i int) int { return i + 1 },
|
"plus1": func(i int) int { return i + 1 },
|
||||||
@@ -63,7 +62,16 @@ func TemplateFuncs() template.FuncMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetFlash sets a flash message in session.
|
func LoadTemplateFiles(name string, files ...string) *template.Template {
|
||||||
|
shared := []string{
|
||||||
|
"templates/layout.html",
|
||||||
|
"templates/topbar.html",
|
||||||
|
}
|
||||||
|
all := append(shared, files...)
|
||||||
|
|
||||||
|
return template.Must(template.New(name).Funcs(TemplateFuncs()).ParseFiles(all...))
|
||||||
|
}
|
||||||
|
|
||||||
func SetFlash(w http.ResponseWriter, r *http.Request, message string) {
|
func SetFlash(w http.ResponseWriter, r *http.Request, message string) {
|
||||||
session, _ := GetSession(w, r)
|
session, _ := GetSession(w, r)
|
||||||
session.Values["flash"] = message
|
session.Values["flash"] = message
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ import (
|
|||||||
"synlotto-website/helpers"
|
"synlotto-website/helpers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Wraps an existing handler but checks is_admin before executing
|
|
||||||
func AdminOnly(db *sql.DB, next http.HandlerFunc) http.HandlerFunc {
|
func AdminOnly(db *sql.DB, next http.HandlerFunc) http.HandlerFunc {
|
||||||
return Auth(true)(func(w http.ResponseWriter, r *http.Request) {
|
return Auth(true)(func(w http.ResponseWriter, r *http.Request) {
|
||||||
userID, ok := helpers.GetCurrentUserID(r)
|
userID, ok := helpers.GetCurrentUserID(r)
|
||||||
if !ok || !helpers.IsAdmin(db, userID) {
|
if !ok || !helpers.IsAdmin(db, userID) {
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
log.Printf("⛔️ Unauthorized admin attempt: user_id=%v, IP=%s, Path=%s", userID, r.RemoteAddr, r.URL.Path)
|
||||||
|
helpers.RenderError(w, r, http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,7 +20,6 @@ func AdminOnly(db *sql.DB, next http.HandlerFunc) http.HandlerFunc {
|
|||||||
ua := r.UserAgent()
|
ua := r.UserAgent()
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
|
|
||||||
// Store log entry in DB
|
|
||||||
_, err := db.Exec(`
|
_, err := db.Exec(`
|
||||||
INSERT INTO admin_access_log (user_id, path, ip, user_agent)
|
INSERT INTO admin_access_log (user_id, path, ip, user_agent)
|
||||||
VALUES (?, ?, ?, ?)`,
|
VALUES (?, ?, ?, ?)`,
|
||||||
@@ -30,9 +29,10 @@ func AdminOnly(db *sql.DB, next http.HandlerFunc) http.HandlerFunc {
|
|||||||
log.Printf("⚠️ Failed to log admin access: %v", err)
|
log.Printf("⚠️ Failed to log admin access: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional: still log to console
|
|
||||||
log.Printf("🛡️ Admin access: user_id=%d IP=%s Path=%s", userID, ip, path)
|
log.Printf("🛡️ Admin access: user_id=%d IP=%s Path=%s", userID, ip, path)
|
||||||
|
|
||||||
next(w, r)
|
next(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ToDo need to look into audit/access log tables and consolidate
|
||||||
|
|||||||
7
templates/error/429.html
Normal file
7
templates/error/429.html
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{{ define "content" }}
|
||||||
|
<div class="container py-5 text-center">
|
||||||
|
<h1 class="text-danger">🚫 Too Many Requests</h1>
|
||||||
|
<p>Whoa there! You're making requests too quickly. Please slow down and try again in a moment.</p>
|
||||||
|
<a href="/" class="btn btn-primary mt-3">Back to home</a>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
14
templates/error/500.html
Normal file
14
templates/error/500.html
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{{ define "content" }}
|
||||||
|
<div class="container py-5 text-center">
|
||||||
|
<h1 class="display-4 text-danger">500 - Server Error</h1>
|
||||||
|
<p class="lead">Something went wrong on our end. We're working to fix it.</p>
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<i class="bi bi-exclamation-triangle-fill text-warning" style="font-size: 3rem;"></i>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p class="mt-4 text-muted">Please try again later or contact support if the issue persists.</p>
|
||||||
|
|
||||||
|
<a href="/" class="btn btn-primary mt-3">Return to Homepage</a>
|
||||||
|
</div>
|
||||||
|
{{ end }}
|
||||||
Reference in New Issue
Block a user