48 lines
914 B
Go
48 lines
914 B
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/rs/xid"
|
||
|
)
|
||
|
|
||
|
type Response struct {
|
||
|
Status int `json:"status"`
|
||
|
ID string `json:"id"`
|
||
|
Code string `json:"code,omitempty"`
|
||
|
Data interface{} `json:"data,omitempty"`
|
||
|
}
|
||
|
|
||
|
func Res(c *gin.Context, res *Response) {
|
||
|
res.ID = xid.New().String()
|
||
|
if res.Status == 0 {
|
||
|
res.Status = 200
|
||
|
}
|
||
|
c.JSON(res.Status, res)
|
||
|
}
|
||
|
|
||
|
func OK(c *gin.Context, data interface{}) {
|
||
|
c.JSON(http.StatusOK, Response{
|
||
|
Status: http.StatusOK,
|
||
|
ID: xid.New().String(),
|
||
|
Data: data,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func Error(c *gin.Context, data interface{}) {
|
||
|
c.JSON(http.StatusInternalServerError, Response{
|
||
|
Status: http.StatusInternalServerError,
|
||
|
ID: xid.New().String(),
|
||
|
Data: data,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func WithCode(c *gin.Context, code int, data interface{}) {
|
||
|
c.JSON(code, Response{
|
||
|
Status: code,
|
||
|
ID: xid.New().String(),
|
||
|
Data: data,
|
||
|
})
|
||
|
}
|