first
This commit is contained in:
63
router/router.go
Normal file
63
router/router.go
Normal 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",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user