27 lines
474 B
Go
27 lines
474 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-contrib/timeout"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func testResponse(c *gin.Context) {
|
|
c.JSON(http.StatusGatewayTimeout, gin.H{
|
|
"code": http.StatusGatewayTimeout,
|
|
"msg": "timeout",
|
|
})
|
|
}
|
|
|
|
func TimeoutMiddleware() gin.HandlerFunc {
|
|
return timeout.New(
|
|
timeout.WithTimeout(5000*time.Millisecond),
|
|
timeout.WithHandler(func(c *gin.Context) {
|
|
c.Next()
|
|
}),
|
|
timeout.WithResponse(testResponse),
|
|
)
|
|
}
|