add private api
This commit is contained in:
@@ -27,6 +27,7 @@ type Discord struct {
|
||||
// Config -
|
||||
type Config struct {
|
||||
Port int `yaml:"port"`
|
||||
SelfKey string `yaml:"self_key"`
|
||||
Database *Database `yaml:"database"`
|
||||
Discord *Discord `yaml:"discord"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// Context -
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
}
|
||||
|
||||
// CustomMiddle -
|
||||
type CustomMiddle func(*Context)
|
||||
|
||||
// PatchCtx -
|
||||
func PatchCtx(handler CustomMiddle) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := &Context{Context: c}
|
||||
handler(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// BindData - bind form body to interface{}
|
||||
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{}) {
|
||||
var resp interface{}
|
||||
if len(msg) == 0 {
|
||||
resp = make(map[string]string)
|
||||
} else {
|
||||
resp = msg[0]
|
||||
}
|
||||
c.AbortWithStatusJSON(status, resp)
|
||||
}
|
||||
|
||||
// Success -
|
||||
func (c *Context) Success(msg ...interface{}) {
|
||||
var resp interface{}
|
||||
if len(msg) == 0 {
|
||||
resp = make(map[string]string)
|
||||
resp.(map[string]string)["message"] = "Success"
|
||||
} else {
|
||||
resp = msg[0]
|
||||
}
|
||||
c.AbortWithStatusJSON(http.StatusOK, msg)
|
||||
}
|
||||
|
||||
// DataFormat -
|
||||
func (c *Context) DataFormat(msg ...interface{}) {
|
||||
var resp interface{}
|
||||
if len(msg) == 0 {
|
||||
resp = make(map[string]string)
|
||||
resp.(map[string]string)["message"] = "data format error"
|
||||
} else {
|
||||
resp = msg[0]
|
||||
}
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, resp)
|
||||
}
|
||||
|
||||
// ServerError -
|
||||
func (c *Context) ServerError(msg ...interface{}) {
|
||||
var resp interface{}
|
||||
if len(msg) == 0 {
|
||||
resp = make(map[string]string)
|
||||
resp.(map[string]string)["message"] = "internal error"
|
||||
} else {
|
||||
resp = msg[0]
|
||||
}
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, msg)
|
||||
}
|
||||
Reference in New Issue
Block a user