Stack of changes to get gin, scs, nosurf running.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
package handlers
|
||||
// internal/handlers/template/error.go
|
||||
package templateHandler
|
||||
|
||||
// ToDo not nessisarily an issue with this file but ✅ internal/handlers/template/
|
||||
//→ For anything that handles HTTP rendering (RenderError, RenderPage)
|
||||
@@ -12,14 +13,44 @@ package handlers
|
||||
//Helpers / Utilities Pure, stateless logic — e.g. template functions, math, formatters. Shared logic, no config, no HTTP handlers.
|
||||
//Handlers Own an HTTP concern — e.g. routes, rendering responses, returning templates or JSON. Injected dependencies (cfg, db, etc.). Should use helpers, not vice versa.
|
||||
|
||||
// ToDo: duplicated work of internal/http/error/errors.go?
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
templateHelpers "synlotto-website/internal/helpers/template"
|
||||
|
||||
"synlotto-website/internal/models"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RenderError delegates to the helper's RenderError, ensuring handlers remain
|
||||
// the entry point for rendering HTTP responses.
|
||||
func (h *Handler) RenderError(w http.ResponseWriter, r *http.Request, statusCode int) {
|
||||
templateHelpers.RenderError(w, r, statusCode)
|
||||
func RenderError(c *gin.Context, sessions *scs.SessionManager, status int) {
|
||||
// Base context
|
||||
ctx := templateHelpers.TemplateContext(c.Writer, c.Request, models.TemplateData{})
|
||||
|
||||
// Flash
|
||||
if f := sessions.PopString(c.Request.Context(), "flash"); f != "" {
|
||||
ctx["Flash"] = f
|
||||
}
|
||||
|
||||
// Correct template paths
|
||||
pagePath := fmt.Sprintf("web/templates/error/%d.html", status)
|
||||
if _, err := os.Stat(pagePath); err != nil {
|
||||
c.String(status, http.StatusText(status))
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles(
|
||||
"web/templates/layout.html",
|
||||
pagePath,
|
||||
)
|
||||
|
||||
c.Status(status)
|
||||
if err := tmpl.ExecuteTemplate(c.Writer, "layout", ctx); err != nil {
|
||||
c.String(status, http.StatusText(status))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user