handler: move mqttping into handler package; clean up main.go
This commit is contained in:
+2
-13
@@ -74,19 +74,8 @@ func main() {
|
|||||||
r.GET("/hello", handler.Hello)
|
r.GET("/hello", handler.Hello)
|
||||||
r.POST("/login", handler.Login)
|
r.POST("/login", handler.Login)
|
||||||
|
|
||||||
// mqttping endpoint: publish current timestamp to MQTT topic
|
// mqttping endpoint handled by internal/handler
|
||||||
r.POST("/mqttping", func(c *gin.Context) {
|
r.POST("/mqttping", handler.MQTTPing)
|
||||||
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
|
// Protected routes
|
||||||
auth := r.Group("/")
|
auth := r.Group("/")
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"git.piskot.si/SeminarM2/lambdaiot-core/internal/mqtt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MQTTPing publishes the current timestamp to the configured MQTT topic
|
||||||
|
func MQTTPing(c *gin.Context) {
|
||||||
|
ts := time.Now().Format(time.RFC3339)
|
||||||
|
topic := os.Getenv("MQTT_TOPIC")
|
||||||
|
if topic == "" {
|
||||||
|
topic = "lambdaiot"
|
||||||
|
}
|
||||||
|
if err := mqtt.PublishDefault(topic, []byte(ts)); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"timestamp": ts})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user