biblio/utils/ginhelper/gin.go

32 lines
591 B
Go
Raw Permalink Normal View History

2022-10-04 21:09:31 +08:00
package ginhelper
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
)
const MessageSuccess="success"
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func Success(c *gin.Context, data interface{}){
c.JSON(http.StatusOK, &Response{Code: 0,
Message: MessageSuccess,
Data: data,
})
}
func Error(c *gin.Context, code int, err error){
c.JSON(http.StatusOK, &Response{Code: code, Message: err.Error(), Data: nil})
}
func ErrorWithLog(c *gin.Context, code int, err error){
log.Println(err)
Error(c, code, err)
}