service/library/middleware/timeout.go

29 lines
568 B
Go

package middleware
import (
"net/http"
"service/api/base"
"service/api/consts"
"time"
"github.com/gin-contrib/timeout"
"github.com/gin-gonic/gin"
)
func timeoutHandler(ctx *gin.Context) {
ctx.AbortWithStatusJSON(http.StatusGatewayTimeout, base.BaseResponse{
Ret: consts.RetCodeFail,
Msg: "出了个小问题,请稍后再试~",
})
}
func TimeoutMiddleware() gin.HandlerFunc {
return timeout.New(
timeout.WithTimeout(3000*time.Millisecond),
timeout.WithHandler(func(c *gin.Context) {
c.Next()
}),
timeout.WithResponse(timeoutHandler),
)
}