feat: implement SQLite storage for state messages and add message retrieval endpoint

This commit is contained in:
2026-01-10 14:20:34 +01:00
parent 7b858861a6
commit a6196cc0ee
6 changed files with 141 additions and 2 deletions
+25
View File
@@ -3,9 +3,11 @@ package handler
import (
"database/sql"
"net/http"
"strconv"
"strings"
"time"
"git.piskot.si/SeminarM2/lambdaiot-core/internal/storage"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
@@ -243,3 +245,26 @@ func (h *Handler) Protected(c *gin.Context) {
}
c.JSON(http.StatusUnauthorized, gin.H{"error": "no claims"})
}
// GetMessages returns the last 100 state messages with optional pagination
// query parameter: ?page=N (0-based)
func GetMessages(c *gin.Context) {
page := 0
if p := c.Query("page"); p != "" {
if n, err := strconv.Atoi(p); err == nil && n >= 0 {
page = n
}
}
limit := 100
offset := page * limit
if storage.Default == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "storage not initialized"})
return
}
msgs, err := storage.Default.QueryMessages(limit, offset)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"messages": msgs})
}