wireguard-dashboard/utils/gin_response.go
2024-03-06 17:13:29 +08:00

45 lines
806 B
Go

package utils
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
type ginResponse struct {
c *gin.Context
}
func GinResponse(c *gin.Context) ginResponse {
return ginResponse{c: c}
}
func (r ginResponse) OK() {
r.c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": "success",
})
}
func (r ginResponse) Failed() {
r.c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": "bad request",
})
}
func (r ginResponse) FailedWithErr(msg string, err error) {
r.c.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": fmt.Errorf("%s: %s", msg, err.Error()),
})
}
func (r ginResponse) OKWithData(data any) {
r.c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"message": "success",
"data": data,
})
}