package handlers import ( "encoding/json" "github.com/gofiber/fiber/v3" "lmika.dev/lmika/hugo-cms/models" "log" ) func GetUser(c fiber.Ctx) models.User { return fiber.Locals[models.User](c, "user") } func GetSite(c fiber.Ctx) models.Site { return fiber.Locals[models.Site](c, "site") } func UpdatePrefCookie(c fiber.Ctx, update func(prefs *models.PrefCookie)) { cookie := GetPrefCookie(c) update(&cookie) setPrefCookie(c, cookie) } func GetPrefCookie(c fiber.Ctx) models.PrefCookie { prefCookieValue := c.Cookies(models.PrefCookieName) if prefCookieValue == "" { return models.PrefCookie{} } var prefCookie models.PrefCookie err := json.Unmarshal([]byte(prefCookieValue), &prefCookie) if err != nil { return models.PrefCookie{} } return prefCookie } func setPrefCookie(c fiber.Ctx, prefCookie models.PrefCookie) { if prefJson, err := json.Marshal(prefCookie); err == nil { c.Cookie(&fiber.Cookie{ Name: models.PrefCookieName, Value: string(prefJson), }) } else { log.Printf("unable to save pref cookie: %v", err) } }