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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
package handlers
|
||||
package templateHandler
|
||||
|
||||
import "synlotto-website/internal/platform/config"
|
||||
import (
|
||||
"synlotto-website/internal/platform/config"
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
cfg config.Config
|
||||
cfg config.Config
|
||||
Sessions *scs.SessionManager
|
||||
}
|
||||
|
||||
func New(cfg config.Config) *Handler {
|
||||
return &Handler{cfg: cfg}
|
||||
func New(cfg config.Config, sessions *scs.SessionManager) *Handler {
|
||||
return &Handler{
|
||||
cfg: cfg,
|
||||
Sessions: sessions,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,52 @@
|
||||
package handlers
|
||||
// internal/handlers/template/templatedata.go
|
||||
package templateHandler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
httpHelper "synlotto-website/internal/helpers/http"
|
||||
// ToDo: again, need to check if i should be using multiple stotage entries like this or if this si even correct would it not be a helper?
|
||||
messageStorage "synlotto-website/internal/storage/messages"
|
||||
notificationStorage "synlotto-website/internal/storage/notifications"
|
||||
usersStorage "synlotto-website/internal/storage/users"
|
||||
|
||||
"synlotto-website/internal/models"
|
||||
"synlotto-website/internal/platform/bootstrap"
|
||||
"synlotto-website/internal/platform/sessionkeys"
|
||||
)
|
||||
|
||||
func BuildTemplateData(db *sql.DB, w http.ResponseWriter, r *http.Request) models.TemplateData {
|
||||
session, err := httpHelper.GetSession(w, r)
|
||||
if err != nil {
|
||||
log.Printf("Session error: %v", err)
|
||||
}
|
||||
// BuildTemplateData aggregates common UI data (user, notifications, messages)
|
||||
// from the current SCS session + DB.
|
||||
func BuildTemplateData(app *bootstrap.App, w http.ResponseWriter, r *http.Request) models.TemplateData {
|
||||
sm := app.SessionManager
|
||||
ctx := r.Context()
|
||||
|
||||
var user *models.User
|
||||
var isAdmin bool
|
||||
var notificationCount int
|
||||
var notifications []models.Notification
|
||||
var messageCount int
|
||||
var messages []models.Message
|
||||
var (
|
||||
user *models.User
|
||||
isAdmin bool
|
||||
notificationCount int
|
||||
notifications []models.Notification
|
||||
messageCount int
|
||||
messages []models.Message
|
||||
)
|
||||
|
||||
if userId, ok := session.Values["user_id"].(int); ok {
|
||||
user = usersStorage.GetUserByID(db, userId)
|
||||
if user != nil {
|
||||
isAdmin = user.IsAdmin
|
||||
notificationCount = notificationStorage.GetNotificationCount(db, user.Id)
|
||||
notifications = notificationStorage.GetRecentNotifications(db, user.Id, 15)
|
||||
messageCount, _ = messageStorage.GetMessageCount(db, user.Id)
|
||||
messages = messageStorage.GetRecentMessages(db, user.Id, 15)
|
||||
// Read user_id from SCS (may be int or int64 depending on writes)
|
||||
if v := sm.Get(ctx, sessionkeys.UserID); v != nil {
|
||||
var uid int64
|
||||
switch t := v.(type) {
|
||||
case int64:
|
||||
uid = t
|
||||
case int:
|
||||
uid = int64(t)
|
||||
}
|
||||
|
||||
if uid > 0 {
|
||||
if u := usersStorage.GetUserByID(app.DB, int(uid)); u != nil {
|
||||
user = u
|
||||
isAdmin = u.IsAdmin
|
||||
notificationCount = notificationStorage.GetNotificationCount(app.DB, int(u.Id))
|
||||
notifications = notificationStorage.GetRecentNotifications(app.DB, int(u.Id), 15)
|
||||
messageCount, _ = messageStorage.GetMessageCount(app.DB, int(u.Id))
|
||||
messages = messageStorage.GetRecentMessages(app.DB, int(u.Id), 15)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user