woj-server/internal/e/resp.go

36 lines
730 B
Go
Raw Normal View History

2022-09-07 23:34:37 +08:00
package e
import (
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/pkg/utils"
2022-09-07 23:34:37 +08:00
"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.Set("err", status)
2022-09-20 14:15:21 +08:00
c.JSON(http.StatusOK, Wrap(status, body))
2022-09-07 23:34:37 +08:00
}
type Endpoint func(*gin.Context) (Status, interface{})
func PongWrapper(handler Endpoint) func(*gin.Context) {
return func(c *gin.Context) {
status, body := handler(c)
Pong(c, status, body)
}
}