feat: enhance Dockerfile for SQLite support and update main.go for MySQL connection; add sensor creation endpoint and improve wait-for-mqtt script

This commit is contained in:
2026-01-10 14:20:38 +01:00
parent a6196cc0ee
commit 81677943c4
5 changed files with 89 additions and 35 deletions
+23 -6
View File
@@ -68,10 +68,10 @@ func (h *Handler) Login(c *gin.Context) {
func (h *Handler) CreateSensor(c *gin.Context) {
var req struct {
DeviceID string `json:"device_id" binding:"required,uuid"`
Name string `json:"name" binding:"required"`
Type string `json:"type" binding:"required"`
Data_Type string `json:"data_type" binding:"required"`
DeviceID string `json:"device_id" binding:"required,uuid"`
Name string `json:"name" binding:"required"`
Type string `json:"type" binding:"required"`
DataTypeID int `json:"data_type_id" binding:"required,min=1"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@@ -84,9 +84,9 @@ func (h *Handler) CreateSensor(c *gin.Context) {
return
}
// Create sensor in database
// Create sensor in database (schema uses data_type_id)
sensorID := uuid.New()
_, err = h.DB.Exec("INSERT INTO sensors (id, device_id, name, type, data_type) VALUES (UUID_TO_BIN(?), UUID_TO_BIN(?), ?, ?, ?)", sensorID.String(), deviceUUID.String(), req.Name, req.Type, req.Data_Type)
_, err = h.DB.Exec("INSERT INTO sensors (id, device_id, name, type, data_type_id) VALUES (UUID_TO_BIN(?), UUID_TO_BIN(?), ?, ?, ?)", sensorID.String(), deviceUUID.String(), req.Name, req.Type, req.DataTypeID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create sensor"})
return
@@ -246,6 +246,23 @@ func (h *Handler) Protected(c *gin.Context) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "no claims"})
}
// Package-level handlers used by tests
func Health(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
func Hello(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hello from lambdaiot"})
}
func Protected(c *gin.Context) {
if v, ok := c.Get("claims"); ok {
c.JSON(http.StatusOK, gin.H{"claims": v})
return
}
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) {