feat(response): 统一结构返回

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

73
response/response.go Normal file
View File

@ -0,0 +1,73 @@
package response
import (
"encoding/json"
"github.com/gin-gonic/gin"
"net/http"
)
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
func NewResponse() *Response {
return &Response{
Code: http.StatusOK,
Message: "success",
}
}
func (r *Response) WithCode(code int) *Response {
r.Code = code
return r
}
func (r *Response) WithMessage(message string) *Response {
r.Message = message
return r
}
func (r *Response) WithData(data any) *Response {
r.Data = data
return r
}
func (r *Response) Error(err error) *Response {
r.Code = http.StatusInternalServerError
r.Message = err.Error()
return r
}
func (r *Response) Success(message ...string) *Response {
r.Code = http.StatusOK
if len(message) > 0 {
r.Message = message[0]
} else {
r.Message = "success"
}
return r
}
func (r *Response) Fail(message ...string) *Response {
r.Code = http.StatusInternalServerError
if len(message) > 0 {
r.Message = message[0]
} else {
r.Message = "fail"
}
return r
}
func (r *Response) Send(c *gin.Context) {
c.JSON(http.StatusOK, r)
}
func (r *Response) ToJSON() (string, error) {
data, err := json.Marshal(r)
if err != nil {
return "", err
}
return string(data), nil
}

View File

@ -2,10 +2,9 @@ package handler
import (
"github.com/gin-gonic/gin"
"onvif-agent/response"
)
func Hello(c *gin.Context) {
c.JSON(200, gin.H{
"message": "Hello, World!",
})
response.NewResponse().Success("Hello, World!").Send(c)
}

View File

@ -3,6 +3,7 @@ package onvif
import (
"github.com/gin-gonic/gin"
"net/http"
"onvif-agent/response"
"onvif-agent/service/onvif"
)
@ -17,32 +18,26 @@ type CreateConnectionRequest struct {
func CreateConnection(c *gin.Context) {
var req CreateConnectionRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
response.NewResponse().Error(err).Send(c)
return
}
conn, err := onvif.New(req.Xaddr, req.Username, req.Password)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
info, err := conn.GetDeviceInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
// store connection
conns[conn.Device.GetDeviceParams().Xaddr] = conn
c.JSON(http.StatusOK, gin.H{
"device": info,
})
response.NewResponse().WithData(info).Send(c)
}
func GetConnections(c *gin.Context) {
@ -51,18 +46,14 @@ func GetConnections(c *gin.Context) {
for xaddr, conn := range conns {
info, err := conn.GetDeviceInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
devices[xaddr] = info
}
c.JSON(http.StatusOK, gin.H{
"connections": devices,
})
response.NewResponse().WithData(devices).Send(c)
}
func GetConnectionByXaddr(c *gin.Context) {
@ -70,30 +61,22 @@ func GetConnectionByXaddr(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
}
info, err := conn.GetDeviceInfo()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
response.NewResponse().Error(err).Send(c)
return
}
c.JSON(http.StatusOK, gin.H{
"device": info,
})
response.NewResponse().WithData(info).Send(c)
}
func DeleteConnection(c *gin.Context) {
xaddr := c.Param("xaddr")
delete(conns, xaddr)
c.JSON(http.StatusOK, gin.H{
"message": "OK",
})
response.NewResponse().Success().Send(c)
}

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)
}