first commit
This commit is contained in:
51
service/response.go
Normal file
51
service/response.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CommonResp interface {
|
||||
gin.H
|
||||
}
|
||||
|
||||
func OKResp[T CommonResp](c *gin.Context, origin *T) {
|
||||
if c.ContentType() == "application/x-protobuf" {
|
||||
c.ProtoBuf(http.StatusOK, origin)
|
||||
} else {
|
||||
c.JSON(http.StatusOK, OK(RespSuccess).WithBody(origin))
|
||||
}
|
||||
}
|
||||
|
||||
func OKRespWithJsonMarshal[T CommonResp](c *gin.Context, origin *T) {
|
||||
c.JSON(http.StatusOK, OK(RespSuccess).WithBody(origin))
|
||||
}
|
||||
|
||||
func ErrResp[T CommonResp](c *gin.Context, origin *T, err string, errCode ...int) {
|
||||
if c.ContentType() == "application/x-protobuf" {
|
||||
c.ProtoBuf(http.StatusInternalServerError, origin)
|
||||
} else {
|
||||
if len(errCode) > 0 {
|
||||
c.JSON(errCode[0], Err(err).WithBody(origin))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, Err(err).WithBody(origin))
|
||||
}
|
||||
}
|
||||
|
||||
func ErrUnAuthorized(c *gin.Context, err string) {
|
||||
if c.ContentType() == "application/x-protobuf" {
|
||||
c.ProtoBuf(http.StatusUnauthorized, nil)
|
||||
} else {
|
||||
c.JSON(http.StatusUnauthorized, Err(err).WithBody(nil))
|
||||
}
|
||||
}
|
||||
|
||||
func ErrNotFound(c *gin.Context, err string) {
|
||||
if c.ContentType() == "application/x-protobuf" {
|
||||
c.ProtoBuf(http.StatusNotFound, nil)
|
||||
} else {
|
||||
c.JSON(http.StatusNotFound, Err(err).WithBody(nil))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user