app/server/helper.go

48 lines
914 B
Go
Raw Permalink Normal View History

2021-12-16 04:11:33 +00:00
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,
})
}