StreamBox/pkg/response/response.go

37 lines
808 B
Go

package response
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Result struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
}
func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, Result{Code: 0, Msg: "success", Data: data})
}
func SuccessWithMsg(c *gin.Context, msg string, data interface{}) {
c.JSON(http.StatusOK, Result{Code: 0, Msg: msg, Data: data})
}
func Fail(c *gin.Context, httpCode, code int, msg string) {
c.JSON(httpCode, Result{Code: code, Msg: msg})
}
func BadRequest(c *gin.Context, msg string) {
Fail(c, http.StatusBadRequest, 400, msg)
}
func InternalError(c *gin.Context, msg string) {
if msg == "" {
msg = "internal server error"
}
Fail(c, http.StatusInternalServerError, 500, msg)
}