44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package common
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/nose7en/ToyBoomServer/defs"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type RespType interface {
|
|
gin.H |
|
|
defs.CommonResponse | defs.GetUserInfoResponse |
|
|
defs.GetUserAuthTokenResponse
|
|
}
|
|
|
|
func OKResp[T RespType](c *gin.Context, origin *T) {
|
|
c.Header(TraceIDKey, c.GetString(TraceIDKey))
|
|
if c.ContentType() == "application/x-protobuf" {
|
|
c.ProtoBuf(http.StatusOK, origin)
|
|
} else {
|
|
c.JSON(http.StatusOK, OK(ReqSuccess).WithBody(origin))
|
|
}
|
|
}
|
|
|
|
func ErrResp[T RespType](c *gin.Context, origin *T, err string) {
|
|
c.Header(TraceIDKey, c.GetString(TraceIDKey))
|
|
if c.ContentType() == "application/x-protobuf" {
|
|
c.ProtoBuf(http.StatusInternalServerError, origin)
|
|
} else {
|
|
c.JSON(http.StatusOK, Err(err).WithBody(origin))
|
|
}
|
|
}
|
|
|
|
func ErrUnAuthorized(c *gin.Context, err string) {
|
|
c.Header(TraceIDKey, c.GetString(TraceIDKey))
|
|
if c.ContentType() == "application/x-protobuf" {
|
|
c.ProtoBuf(http.StatusUnauthorized,
|
|
&defs.CommonResponse{Status: &defs.Status{Code: defs.RespCode_UNAUTHORIZED, Message: defs.RespMessage_UNAUTHORIZED}})
|
|
} else {
|
|
c.JSON(http.StatusOK, Err(err))
|
|
}
|
|
}
|