first version
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// Context -
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
}
|
||||
|
||||
// CustomMiddle func
|
||||
type CustomMiddle func(*Context)
|
||||
|
||||
// PatchCtx - patch context to custom middleware
|
||||
func PatchCtx(handler CustomMiddle) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := &Context{Context: c}
|
||||
handler(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// BindData client body data
|
||||
func (c *Context) BindData(i interface{}) error {
|
||||
bind := binding.Default(c.Request.Method, c.ContentType())
|
||||
return c.ShouldBindWith(i, bind)
|
||||
}
|
||||
|
||||
// CustomRes -
|
||||
func (c *Context) CustomRes(status int, msg interface{}) {
|
||||
c.AbortWithStatusJSON(status, msg)
|
||||
}
|
||||
|
||||
// LoginFirst -
|
||||
func (c *Context) LoginFirst(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "login first"}
|
||||
}
|
||||
c.AbortWithStatusJSON(401, msg)
|
||||
}
|
||||
|
||||
// DataFormat -
|
||||
func (c *Context) DataFormat(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "data format error"}
|
||||
}
|
||||
c.AbortWithStatusJSON(400, msg)
|
||||
}
|
||||
|
||||
// NotFound -
|
||||
func (c *Context) NotFound(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "not found"}
|
||||
}
|
||||
c.AbortWithStatusJSON(404, msg)
|
||||
}
|
||||
|
||||
// Forbidden -
|
||||
func (c *Context) Forbidden(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "forbidden"}
|
||||
}
|
||||
c.AbortWithStatusJSON(403, msg)
|
||||
}
|
||||
|
||||
// Success -
|
||||
func (c *Context) Success(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "success"}
|
||||
}
|
||||
|
||||
if str, ok := msg.(string); ok {
|
||||
c.String(200, str)
|
||||
c.Abort()
|
||||
} else {
|
||||
c.AbortWithStatusJSON(200, msg)
|
||||
}
|
||||
}
|
||||
|
||||
// ServerError -
|
||||
func (c *Context) ServerError(msg interface{}) {
|
||||
if msg == nil {
|
||||
msg = map[string]interface{}{"message": "internal error"}
|
||||
}
|
||||
c.AbortWithStatusJSON(500, msg)
|
||||
}
|
||||
Reference in New Issue
Block a user