github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/tools/jsontoolkit/json.go (about)

     1  package jsontoolkit
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/GuanceCloud/cliutils/pprofparser/tools/logtoolkit"
     8  	"github.com/gin-gonic/gin"
     9  )
    10  
    11  type JSONResp struct {
    12  	Code    int         `json:"code"`
    13  	Message string      `json:"message"`
    14  	Data    interface{} `json:"data"`
    15  }
    16  
    17  func JSONSuccess(ctx *gin.Context, data interface{}) {
    18  	ctx.JSON(http.StatusOK, &JSONResp{
    19  		Code:    0,
    20  		Message: "success",
    21  		Data:    data,
    22  	})
    23  }
    24  
    25  func JSONError(ctx *gin.Context, code int, message string) {
    26  	ctx.JSON(http.StatusOK, &JSONResp{
    27  		Code:    code,
    28  		Message: message,
    29  		Data:    struct{}{},
    30  	})
    31  }
    32  
    33  func JSONErrorf(ctx *gin.Context, code int, format string, args ...interface{}) {
    34  	msg := fmt.Sprintf(format, args...)
    35  	logtoolkit.Error(msg)
    36  	ctx.JSON(http.StatusOK, &JSONResp{
    37  		Code:    code,
    38  		Message: msg,
    39  		Data:    struct{}{},
    40  	})
    41  }