app/server/helper.go

64 lines
1.1 KiB
Go
Raw Normal View History

2021-12-16 04:11:33 +00:00
package server
import (
"net/http"
2021-12-16 17:31:56 +00:00
"path/filepath"
2021-12-16 04:11:33 +00:00
"github.com/gin-gonic/gin"
"github.com/rs/xid"
)
2021-12-16 17:31:56 +00:00
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
}
}
2021-12-16 04:11:33 +00:00
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,
})
}