Refactoring for Gin, NoSurf and SCS continues.

This commit is contained in:
2025-10-24 13:08:53 +01:00
parent 7276903733
commit fb07c4a5eb
61 changed files with 546 additions and 524 deletions

View File

@@ -0,0 +1,63 @@
package handlers
// ToDo not nessisarily an issue with this file but ✅ internal/handlers/template/
//→ For anything that handles HTTP rendering (RenderError, RenderPage)
//✅ internal/helpers/template/
//→ For anything that helps render (TemplateContext, pagination, funcs)
// there for bear usages between helpers and handlers
//In clean Go architecture (especially following “Package by responsibility”):
//Type Responsibility Should access
//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.
import (
"fmt"
"log"
"net/http"
"os"
templateHelpers "synlotto-website/internal/helpers/template"
"synlotto-website/internal/models"
)
func RenderError(
w http.ResponseWriter,
r *http.Request,
statusCode int,
siteName string,
copyrightYearStart int,
) {
log.Printf("⚙️ RenderError called with status: %d", statusCode)
context := templateHelpers.TemplateContext(
w, r,
models.TemplateData{},
siteName,
copyrightYearStart,
)
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 := templateHelpers.LoadTemplateFiles(fmt.Sprintf("%d.html", statusCode), pagePath)
w.WriteHeader(statusCode)
if err := tmpl.ExecuteTemplate(w, "layout", context); err != nil {
log.Printf("❌ Failed to render error page layout: %v", err)
http.Error(w, http.StatusText(statusCode), statusCode)
return
}
log.Println("✅ Successfully rendered error page") // ToDo: log these to db
}

View File

@@ -0,0 +1,11 @@
package handlers
import "synlotto-website/internal/platform/config"
type Handler struct {
cfg config.Config
}
func New(cfg config.Config) *Handler {
return &Handler{cfg: cfg}
}

View File

@@ -6,9 +6,12 @@ import (
"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/storage"
)
func BuildTemplateData(db *sql.DB, w http.ResponseWriter, r *http.Request) models.TemplateData {
@@ -25,13 +28,13 @@ func BuildTemplateData(db *sql.DB, w http.ResponseWriter, r *http.Request) model
var messages []models.Message
if userId, ok := session.Values["user_id"].(int); ok {
user = storage.GetUserByID(db, userId)
user = usersStorage.GetUserByID(db, userId)
if user != nil {
isAdmin = user.IsAdmin
notificationCount = storage.GetNotificationCount(db, user.Id)
notifications = storage.GetRecentNotifications(db, user.Id, 15)
messageCount, _ = storage.GetMessageCount(db, user.Id)
messages = storage.GetRecentMessages(db, user.Id, 15)
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)
}
}