37 lines
669 B
Go
37 lines
669 B
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Body struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data any `json:"data,omitempty"`
|
|
}
|
|
|
|
func OK(c *gin.Context, data any) {
|
|
c.JSON(http.StatusOK, Body{
|
|
Code: 0,
|
|
Message: "ok",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func Fail(c *gin.Context, httpStatus int, code int, message string) {
|
|
c.JSON(httpStatus, Body{
|
|
Code: code,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
func BadRequest(c *gin.Context, message string) {
|
|
Fail(c, http.StatusBadRequest, 40000, message)
|
|
}
|
|
|
|
func InternalError(c *gin.Context, message string) {
|
|
Fail(c, http.StatusInternalServerError, 50000, message)
|
|
}
|