first commit
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/timeout"
|
||||
)
|
||||
|
||||
func testResponse(c *gin.Context) {
|
||||
c.String(http.StatusRequestTimeout, "timeout")
|
||||
}
|
||||
|
||||
func timeoutMiddleware() gin.HandlerFunc {
|
||||
return timeout.New(
|
||||
timeout.WithTimeout(500*time.Millisecond),
|
||||
timeout.WithHandler(func(c *gin.Context) {
|
||||
c.Next()
|
||||
}),
|
||||
timeout.WithResponse(testResponse),
|
||||
)
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := gin.New()
|
||||
r.Use(timeoutMiddleware())
|
||||
r.GET("/slow", func(c *gin.Context) {
|
||||
time.Sleep(800 * time.Millisecond)
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user