init repo
This commit is contained in:
43
common/response.go
Normal file
43
common/response.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user