init repo

This commit is contained in:
VaalaCat
2024-08-28 00:02:28 +08:00
committed by vaalacat
commit 13148b95e3
97 changed files with 10214 additions and 0 deletions

43
common/response.go Normal file
View 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))
}
}