Stack of changes to get gin, scs, nosurf running.

This commit is contained in:
2025-10-28 11:56:42 +00:00
parent 07117ba35e
commit 86be6479f1
65 changed files with 1890 additions and 1503 deletions

View File

@@ -1,4 +1,4 @@
package helpers
package templateHelper
import (
"html/template"
@@ -6,9 +6,9 @@ import (
"strings"
"time"
httpHelpers "synlotto-website/internal/helpers/http"
"synlotto-website/internal/models"
"github.com/alexedwards/scs/v2"
"github.com/justinas/nosurf"
)
@@ -27,19 +27,15 @@ func InitSiteMeta(name string, yearStart, yearEnd int) {
}
}
var sm *scs.SessionManager
func InitSessionManager(manager *scs.SessionManager) {
sm = manager
}
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 {
flash = f
delete(session.Values, "flash")
session.Save(r, w)
}
return map[string]interface{}{
"CSRFToken": nosurf.Token(r),
"Flash": flash,
"User": data.User,
"IsAdmin": data.IsAdmin,
"NotificationCount": data.NotificationCount,
@@ -105,18 +101,18 @@ func TemplateFuncs() template.FuncMap {
func LoadTemplateFiles(name string, files ...string) *template.Template {
shared := []string{
"templates/main/layout.html",
"templates/main/topbar.html",
"templates/main/footer.html",
"web/templates/main/layout.html",
"web/templates/main/topbar.html",
"web/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, _ := httpHelpers.GetSession(w, r)
session.Values["flash"] = message
session.Save(r, w)
func SetFlash(r *http.Request, message string) {
if sm != nil {
sm.Put(r.Context(), "flash", message)
}
}
func InSlice(n int, list []int) bool {

View File

@@ -1,4 +1,4 @@
package helpers
package templateHelper
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package helpers
package templateHelper
import (
"database/sql"