test: enable mosquitto password auth; add mqttping endpoint; use credentials in subscriber; publish defaults

This commit is contained in:
2025-12-28 15:24:12 +01:00
parent 8fd9353068
commit c835fb7a98
5 changed files with 86 additions and 41 deletions
+18 -2
View File
@@ -13,10 +13,11 @@ import (
"github.com/gin-gonic/gin"
"git.piskot.si/SeminarM2/lambdaiot-core/internal/config"
mqttclient "git.piskot.si/SeminarM2/lambdaiot-core/internal/mqtt"
"git.piskot.si/SeminarM2/lambdaiot-core/internal/handler"
"git.piskot.si/SeminarM2/lambdaiot-core/internal/middleware"
mqttclient "git.piskot.si/SeminarM2/lambdaiot-core/internal/mqtt"
)
func main() {
// load configuration (look for ./config.toml by default)
cfg, err := config.Load("")
@@ -41,6 +42,7 @@ func main() {
log.Printf("warning: mqtt connect failed: %v", err)
} else {
mq = mqc
mqttclient.SetDefault(mqc)
// publish a startup message (non-blocking)
go func() {
msg := fmt.Sprintf("lambdaiot-core started on %s", addr)
@@ -51,7 +53,7 @@ func main() {
}
}
// mqttping endpoint will be added after router initialization
// Gin setup
r := gin.New()
@@ -63,6 +65,20 @@ func main() {
r.GET("/hello", handler.Hello)
r.POST("/login", handler.Login)
// mqttping endpoint: publish current timestamp to MQTT topic
r.POST("/mqttping", func(c *gin.Context) {
ts := time.Now().Format(time.RFC3339)
if mq == nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "mqtt not connected"})
return
}
if err := mq.Publish(cfg.MQTT.Topic, []byte(ts)); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"timestamp": ts})
})
// Protected routes
auth := r.Group("/")
auth.Use(middleware.AuthMiddleware(cfg.Server.JWTSecret))