64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"path/filepath"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/rs/xid"
|
|
)
|
|
|
|
func DefaultSkipLog(c *gin.Context) bool {
|
|
switch filepath.Ext(c.Request.URL.Path) {
|
|
case ".css":
|
|
fallthrough
|
|
case ".woff2":
|
|
fallthrough
|
|
case ".map":
|
|
fallthrough
|
|
case ".js":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
type Response struct {
|
|
Status int `json:"status"`
|
|
ID string `json:"id"`
|
|
Code string `json:"code,omitempty"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
func Res(c *gin.Context, res *Response) {
|
|
res.ID = xid.New().String()
|
|
if res.Status == 0 {
|
|
res.Status = 200
|
|
}
|
|
c.JSON(res.Status, res)
|
|
}
|
|
|
|
func OK(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Status: http.StatusOK,
|
|
ID: xid.New().String(),
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func Error(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusInternalServerError, Response{
|
|
Status: http.StatusInternalServerError,
|
|
ID: xid.New().String(),
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
func WithCode(c *gin.Context, code int, data interface{}) {
|
|
c.JSON(code, Response{
|
|
Status: code,
|
|
ID: xid.New().String(),
|
|
Data: data,
|
|
})
|
|
}
|