feat(response): 统一结构返回

This commit is contained in:
2024-08-26 11:19:18 +08:00
parent 85c90ec1e1
commit 74c955a3f7
4 changed files with 92 additions and 40 deletions

View File

@ -8,6 +8,7 @@ import (
"log"
"net/http"
"onvif-agent/config"
"onvif-agent/response"
)
func CreateEventSubscription(c *gin.Context) {
@ -17,21 +18,17 @@ func CreateEventSubscription(c *gin.Context) {
conn := conns[xaddr]
if conn == nil {
c.JSON(http.StatusNotFound, gin.H{
"message": "Connection not found",
})
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
return
}
result, err := conn.SubscribeEvents(callbackURL, "PT60S")
if err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
c.JSON(http.StatusOK, result)
response.NewResponse().Success().WithData(result).Send(c)
}
func EventNotifyCallback(c *gin.Context) {
@ -41,7 +38,7 @@ func EventNotifyCallback(c *gin.Context) {
var notify event.Notify
envelope := gosoap.NewSOAPEnvelope(&notify)
if err := c.ShouldBindXML(&envelope); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
response.NewResponse().Error(err).WithCode(http.StatusBadRequest).Send(c)
return
}
@ -50,5 +47,5 @@ func EventNotifyCallback(c *gin.Context) {
log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message)
}
c.JSON(http.StatusOK, gin.H{"message": "OK"})
response.NewResponse().Success().Send(c)
}