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

@@ -2,24 +2,33 @@ package helpers
import (
"html/template"
"log"
"net/http"
"strings"
"time"
helpers "synlotto-website/internal/helpers/http"
httpHelpers "synlotto-website/internal/helpers/http"
"synlotto-website/internal/models"
"synlotto-website/internal/platform/config"
"github.com/gorilla/csrf"
"github.com/justinas/nosurf"
)
func TemplateContext(w http.ResponseWriter, r *http.Request, data models.TemplateData) map[string]interface{} {
cfg := config.Get()
if cfg == nil {
log.Println("⚠️ Config not initialized!")
// ToDo should these structs be here?
type siteMeta struct {
Name string
CopyrightYearStart int
}
var meta siteMeta
func InitSiteMeta(name string, yearStart, yearEnd int) {
meta = siteMeta{
Name: name,
CopyrightYearStart: yearStart,
}
session, _ := helpers.GetSession(w, r)
}
func TemplateContext(w http.ResponseWriter, r *http.Request, data models.TemplateData) map[string]interface{} {
session, _ := httpHelpers.GetSession(w, r)
var flash string
if f, ok := session.Values["flash"].(string); ok {
@@ -29,7 +38,7 @@ func TemplateContext(w http.ResponseWriter, r *http.Request, data models.Templat
}
return map[string]interface{}{
"CSRFField": csrf.TemplateField(r),
"CSRFToken": nosurf.Token(r),
"Flash": flash,
"User": data.User,
"IsAdmin": data.IsAdmin,
@@ -37,8 +46,8 @@ func TemplateContext(w http.ResponseWriter, r *http.Request, data models.Templat
"Notifications": data.Notifications,
"MessageCount": data.MessageCount,
"Messages": data.Messages,
"SiteName": cfg.Site.SiteName,
"CopyrightYearStart": cfg.Site.CopyrightYearStart,
"SiteName": meta.Name,
"CopyrightYearStart": meta.CopyrightYearStart,
}
}
@@ -57,9 +66,8 @@ func TemplateFuncs() template.FuncMap {
"min": func(a, b int) int {
if a < b {
return a
} else {
return b
}
return b
},
"intVal": func(p *int) int {
if p == nil {
@@ -102,12 +110,11 @@ func LoadTemplateFiles(name string, files ...string) *template.Template {
"templates/main/footer.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) {
session, _ := helpers.GetSession(w, r)
session, _ := httpHelpers.GetSession(w, r)
session.Values["flash"] = message
session.Save(r, w)
}