mtgbot/modules/context/context.go

34 lines
691 B
Go
Raw Normal View History

2018-12-24 06:34:13 +00:00
package context
import "github.com/gin-gonic/gin"
import "github.com/gin-gonic/gin/binding"
// Context - custom http context
type Context struct {
*gin.Context
}
// CustomMiddle -
type CustomMiddle func(*Context)
// PatchCtx - change custom middle to gin middle
func PatchCtx(handler CustomMiddle) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := &Context{
Context: c,
}
handler(ctx)
}
}
// BindData - binding data
func (c *Context) BindData(i interface{}) error {
b := binding.Default(c.Request.Method, c.ContentType())
return c.ShouldBindWith(i, b)
}
// CustomRes -
func (c *Context) CustomRes(status int, msg interface{}) {
c.AbortWithStatusJSON(status, msg)
}