feat(response): 统一结构返回
This commit is contained in:
parent
85c90ec1e1
commit
74c955a3f7
73
response/response.go
Normal file
73
response/response.go
Normal 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
|
||||||
|
}
|
@ -2,10 +2,9 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"onvif-agent/response"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Hello(c *gin.Context) {
|
func Hello(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{
|
response.NewResponse().Success("Hello, World!").Send(c)
|
||||||
"message": "Hello, World!",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package onvif
|
|||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"onvif-agent/response"
|
||||||
"onvif-agent/service/onvif"
|
"onvif-agent/service/onvif"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,32 +18,26 @@ type CreateConnectionRequest struct {
|
|||||||
func CreateConnection(c *gin.Context) {
|
func CreateConnection(c *gin.Context) {
|
||||||
var req CreateConnectionRequest
|
var req CreateConnectionRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
response.NewResponse().Error(err).Send(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := onvif.New(req.Xaddr, req.Username, req.Password)
|
conn, err := onvif.New(req.Xaddr, req.Username, req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
response.NewResponse().Error(err).Send(c)
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := conn.GetDeviceInfo()
|
info, err := conn.GetDeviceInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
response.NewResponse().Error(err).Send(c)
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// store connection
|
// store connection
|
||||||
conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
conns[conn.Device.GetDeviceParams().Xaddr] = conn
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
response.NewResponse().WithData(info).Send(c)
|
||||||
"device": info,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConnections(c *gin.Context) {
|
func GetConnections(c *gin.Context) {
|
||||||
@ -51,18 +46,14 @@ func GetConnections(c *gin.Context) {
|
|||||||
for xaddr, conn := range conns {
|
for xaddr, conn := range conns {
|
||||||
info, err := conn.GetDeviceInfo()
|
info, err := conn.GetDeviceInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
response.NewResponse().Error(err).Send(c)
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
devices[xaddr] = info
|
devices[xaddr] = info
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
response.NewResponse().WithData(devices).Send(c)
|
||||||
"connections": devices,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConnectionByXaddr(c *gin.Context) {
|
func GetConnectionByXaddr(c *gin.Context) {
|
||||||
@ -70,30 +61,22 @@ func GetConnectionByXaddr(c *gin.Context) {
|
|||||||
|
|
||||||
conn := conns[xaddr]
|
conn := conns[xaddr]
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{
|
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
||||||
"message": "Connection not found",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
info, err := conn.GetDeviceInfo()
|
info, err := conn.GetDeviceInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
response.NewResponse().Error(err).Send(c)
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
response.NewResponse().WithData(info).Send(c)
|
||||||
"device": info,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteConnection(c *gin.Context) {
|
func DeleteConnection(c *gin.Context) {
|
||||||
xaddr := c.Param("xaddr")
|
xaddr := c.Param("xaddr")
|
||||||
delete(conns, xaddr)
|
delete(conns, xaddr)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
response.NewResponse().Success().Send(c)
|
||||||
"message": "OK",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"onvif-agent/config"
|
"onvif-agent/config"
|
||||||
|
"onvif-agent/response"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateEventSubscription(c *gin.Context) {
|
func CreateEventSubscription(c *gin.Context) {
|
||||||
@ -17,21 +18,17 @@ func CreateEventSubscription(c *gin.Context) {
|
|||||||
|
|
||||||
conn := conns[xaddr]
|
conn := conns[xaddr]
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
c.JSON(http.StatusNotFound, gin.H{
|
response.NewResponse().Fail("Connection not found").WithCode(http.StatusNotFound).Send(c)
|
||||||
"message": "Connection not found",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := conn.SubscribeEvents(callbackURL, "PT60S")
|
result, err := conn.SubscribeEvents(callbackURL, "PT60S")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
response.NewResponse().Error(err).Send(c)
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, result)
|
response.NewResponse().Success().WithData(result).Send(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func EventNotifyCallback(c *gin.Context) {
|
func EventNotifyCallback(c *gin.Context) {
|
||||||
@ -41,7 +38,7 @@ func EventNotifyCallback(c *gin.Context) {
|
|||||||
var notify event.Notify
|
var notify event.Notify
|
||||||
envelope := gosoap.NewSOAPEnvelope(¬ify)
|
envelope := gosoap.NewSOAPEnvelope(¬ify)
|
||||||
if err := c.ShouldBindXML(&envelope); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,5 +47,5 @@ func EventNotifyCallback(c *gin.Context) {
|
|||||||
log.Printf("Topic: %s, Message: %s", msg.Topic.TopicKinds, msg.Message.Message)
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user