mtgbot/modules/context/context.go

73 lines
1.6 KiB
Go
Raw Normal View History

2018-12-24 06:34:13 +00:00
package context
2018-12-24 09:34:24 +00:00
import (
"git.trj.tw/golang/mtgbot/modules/apimsg"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
2018-12-24 06:34:13 +00:00
// 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)
}
2018-12-24 09:34:24 +00:00
// LoginFirst -
func (c *Context) LoginFirst(msg interface{}) {
obj := apimsg.GetRes("LoginFirst", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// NotFound -
func (c *Context) NotFound(msg interface{}) {
obj := apimsg.GetRes("NotFound", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// DataFormat -
func (c *Context) DataFormat(msg interface{}) {
obj := apimsg.GetRes("DataFormat", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// Forbidden -
func (c *Context) Forbidden(msg interface{}) {
obj := apimsg.GetRes("Forbidden", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// Success -
func (c *Context) Success(msg interface{}) {
obj := apimsg.GetRes("Success", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}
// ServerError -
func (c *Context) ServerError(msg interface{}) {
obj := apimsg.GetRes("InternalError", msg)
c.AbortWithStatusJSON(obj.Status, obj.Obj)
}