woj-server/internal/e/resp.go

27 lines
550 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[T any] struct {
Code int `json:"code"`
Msg string `json:"msg"`
Body T `json:"body"`
2022-09-07 23:34:37 +08:00
}
func wrap[T any](status Status, body T) Response[interface{}] {
return Response[interface{}]{
2022-09-20 14:15:21 +08:00
Code: int(status),
Msg: status.String(),
Body: utils.If[interface{}](status == Success, body, nil),
2022-09-07 23:34:37 +08:00
}
}
func Pong[T any](c *gin.Context, status Status, body T) {
c.Set("err", status)
c.JSON(http.StatusOK, wrap(status, body))
}