This commit is contained in:
Sakurasan
2023-04-06 22:40:20 +08:00
commit d11241960a
8 changed files with 396 additions and 0 deletions

63
router/router.go Normal file
View File

@@ -0,0 +1,63 @@
package router
import (
"net/http"
"emqxboard/pkg/emq"
"github.com/gin-gonic/gin"
)
type data struct {
UID string `json:"uid,omitempty"`
Topic string `json:"topic,omitempty"`
Type int `json:"type,omitempty"`
Msg string `json:"msg,omitempty"`
}
func MapRoutes() *gin.Engine {
router := gin.Default()
router.GET("/va/getpushmsg", handrouter)
// router.POST("/va/postJsonMsg", handrouter)
return router
}
func handrouter(c *gin.Context) {
// var d data
// if err := c.BindQuery(&d); err != nil {
// c.JSON(http.StatusBadRequest, gin.H{"code": 1, "error": err.Error()})
// return
// }
// if d.Topic == "" {
// if err := c.Bind(&d); err != nil {
// c.JSON(http.StatusOK, gin.H{
// "code": 1,
// "error": err.Error(),
// })
// }
// }
if token := emq.Client.Connect(); token.Wait() && token.Error() != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": "1",
"error": token.Error(),
})
return
}
token := emq.Client.Publish(c.Query("topic"), 0, false, c.Query("msg"))
if token.Wait() && token.Error() != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": "1",
"error": token.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": "0",
"message": "OK",
})
}