woj-server/internal/e/resp.go

26 lines
507 B
Go
Raw Normal View History

2022-09-07 23:34:37 +08:00
package e
import (
"github.com/WHUPRJ/woj-server/pkg/utils"
"github.com/gin-gonic/gin"
"net/http"
)
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Body interface{} `json:"body"`
}
2022-09-20 14:15:21 +08:00
func Wrap(status Status, body interface{}) interface{} {
2022-09-07 23:34:37 +08:00
return Response{
2022-09-20 14:15:21 +08:00
Code: int(status),
Msg: status.String(),
Body: utils.If(status == Success, body, nil),
2022-09-07 23:34:37 +08:00
}
}
2022-09-20 14:15:21 +08:00
func Pong(c *gin.Context, status Status, body interface{}) {
c.JSON(http.StatusOK, Wrap(status, body))
2022-09-07 23:34:37 +08:00
}