init repo
This commit is contained in:
34
common/request.go
Normal file
34
common/request.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/nose7en/ToyBoomServer/defs"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
type ReqType interface {
|
||||
gin.H |
|
||||
defs.CommonPaginationRequest | defs.CommonQueryPaginationRequest |
|
||||
defs.CommonQueryRequest | defs.CommonRequest
|
||||
}
|
||||
|
||||
func GetProtoRequest[T ReqType](c *gin.Context) (r *T, err error) {
|
||||
r = new(T)
|
||||
if c.Request.ContentLength == 0 {
|
||||
return r, nil
|
||||
}
|
||||
|
||||
if c.ContentType() == "application/x-protobuf" {
|
||||
err = c.Copy().ShouldBindWith(r, binding.ProtoBuf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
err = c.Copy().ShouldBindJSON(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
Reference in New Issue
Block a user