Messages now sending/loading and populating on message dropdown

This commit is contained in:
2025-10-31 12:08:38 +00:00
parent 776ea53a66
commit 8529116ad2
13 changed files with 136 additions and 81 deletions

View File

@@ -18,14 +18,9 @@ import (
// using ONLY session data (no DB) so 404/500 pages don't crash and still
// look "logged in" when a session exists.
func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
// Synthesize minimal TemplateData from session only
var data models.TemplateData
ctx := c.Request.Context()
// Read minimal user snapshot from session
var uid int64
if v := sessions.Get(ctx, sessionkeys.UserID); v != nil {
r := c.Request
uid := int64(0)
if v := sessions.Get(r.Context(), sessionkeys.UserID); v != nil {
switch t := v.(type) {
case int64:
uid = t
@@ -33,22 +28,22 @@ func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
uid = int64(t)
}
}
// --- build minimal template data from session
var data models.TemplateData
if uid > 0 {
// username and is_admin are optional but make navbar correct
var uname string
if v := sessions.Get(ctx, sessionkeys.Username); v != nil {
uname := ""
if v := sessions.Get(r.Context(), sessionkeys.Username); v != nil {
if s, ok := v.(string); ok {
uname = s
}
}
var isAdmin bool
if v := sessions.Get(ctx, sessionkeys.IsAdmin); v != nil {
isAdmin := false
if v := sessions.Get(r.Context(), sessionkeys.IsAdmin); v != nil {
if b, ok := v.(bool); ok {
isAdmin = b
}
}
// Build a lightweight user; avoids DB lookups in error paths
data.User = &models.User{
Id: uid,
Username: uname,
@@ -57,15 +52,11 @@ func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
data.IsAdmin = isAdmin
}
// Turn into the template context map (adds site meta, funcs, etc.)
ctxMap := templateHelpers.TemplateContext(c.Writer, c.Request, data)
// Flash (SCS)
if f := sessions.PopString(ctx, sessionkeys.Flash); f != "" {
ctxMap := templateHelpers.TemplateContext(c.Writer, r, data)
if f := sessions.PopString(r.Context(), sessionkeys.Flash); f != "" {
ctxMap["Flash"] = f
}
// Template paths (layout-first)
pagePath := fmt.Sprintf("web/templates/error/%d.html", status)
if _, err := os.Stat(pagePath); err != nil {
c.String(status, http.StatusText(status))
@@ -86,11 +77,13 @@ func RenderStatus(c *gin.Context, sessions *scs.SessionManager, status int) {
func NoRoute(sessions *scs.SessionManager) gin.HandlerFunc {
return func(c *gin.Context) { RenderStatus(c, sessions, http.StatusNotFound) }
}
func NoMethod(sessions *scs.SessionManager) gin.HandlerFunc {
return func(c *gin.Context) { RenderStatus(c, sessions, http.StatusMethodNotAllowed) }
}
func Recovery(sessions *scs.SessionManager) gin.RecoveryFunc {
return func(c *gin.Context, _ interface{}) {
return func(c *gin.Context, rec interface{}) {
RenderStatus(c, sessions, http.StatusInternalServerError)
}
}

View File

@@ -0,0 +1,26 @@
// internal/http/middleware/errorlog.go
package middleware
import (
"time"
"synlotto-website/internal/logging"
"github.com/gin-gonic/gin"
)
func ErrorLogger() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
if len(c.Errors) == 0 {
return
}
for _, e := range c.Errors {
logging.Info("❌ %s %s -> %d in %v: %v",
c.Request.Method, c.FullPath(), c.Writer.Status(),
time.Since(start), e.Err)
}
}
}

View File

@@ -69,7 +69,8 @@ func RegisterAccountRoutes(app *bootstrap.App) {
messages.GET("/send", msgH.SendGet)
messages.POST("/send", msgH.SendPost)
messages.GET("/archived", msgH.ArchivedList) // renders archived.html
messages.GET("/:id", msgH.ReadGet) // renders read.html
messages.GET("/read", msgH.ReadGet)
}
// Notifications (auth-required)