33 lines
598 B
Go
33 lines
598 B
Go
package helper
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
// ResponseData : response format
|
|
type ResponseData struct {
|
|
Success bool `json:"success"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
// RespondJSON : send a proper response json to the client
|
|
func RespondJSON(w *gin.Context, status int, message string, payload interface{}) {
|
|
var res ResponseData
|
|
|
|
if status >= 200 && status < 300 {
|
|
res.Success = true
|
|
}
|
|
|
|
if len(message) != 0 {
|
|
res.Message = message
|
|
}
|
|
|
|
if payload != nil {
|
|
res.Data = payload
|
|
}
|
|
|
|
w.JSON(http.StatusOK, res)
|
|
}
|