Skip to content

Commit

Permalink
exchange timeout middleware (gin-contrib/timeout#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rk committed Sep 9, 2022
1 parent 5a6781d commit dbd772c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
11 changes: 4 additions & 7 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ module github.com/d-rk/checkin-system
go 1.19

require (
github.com/flytam/filenamify v1.1.1
github.com/gin-gonic/gin v1.8.1
github.com/gocarina/gocsv v0.0.0-20220823132111-71f3a5cb2654
github.com/gorilla/websocket v1.5.0
github.com/jmoiron/sqlx v1.3.5
github.com/joho/godotenv v1.4.0
github.com/lib/pq v1.10.6
github.com/rubenv/sql-migrate v1.1.2
)

require (
github.com/flytam/filenamify v1.1.1 // indirect
github.com/gin-contrib/timeout v0.0.3 // indirect
github.com/gocarina/gocsv v0.0.0-20220823132111-71f3a5cb2654 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/vearne/gin-timeout v0.1.1
)

require (
Expand Down
7 changes: 3 additions & 4 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-contrib/timeout v0.0.3 h1:ysZQ7kChgqlzBkuLgwTTDjTPP2uqdI68XxRyqIFK68g=
github.com/gin-contrib/timeout v0.0.3/go.mod h1:F3fjkmFc4I1QdF7MyVwtO6ZkPueBckNoiOVpU73HGgU=
github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/gin-gonic/gin v1.7.3/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -327,11 +325,12 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/vearne/gin-timeout v0.1.1 h1:5BxjqRRJYJSP17B2lqoBxkQx856DXJiT2br5Bd0n1/s=
github.com/vearne/gin-timeout v0.1.1/go.mod h1:uunduHOaylsn1UfQHYHPnRs+8AISegLJzfcB5ekUCGo=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
1 change: 0 additions & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ func main() {

api.GET("/checkins/dates", checkInHandler.ListCheckInDates)


api.GET("/websocket", websocketHandler)

r.Run(":8080")
Expand Down
19 changes: 6 additions & 13 deletions backend/pkg/middlewares/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,16 @@ import (
"net/http"
"time"

"github.com/gin-contrib/timeout"
"github.com/gin-gonic/gin"
timeout "github.com/vearne/gin-timeout"
)

func timeoutResponse(timeoutDuration time.Duration) func(*gin.Context) {
return func(c *gin.Context) {
c.JSON(http.StatusRequestTimeout, gin.H{"error": fmt.Sprintf("request aborted after %v", timeoutDuration)})
}
}

func TimeoutMiddleware(timeoutDuration time.Duration) gin.HandlerFunc {

return timeout.New(
defaultMsg := fmt.Sprintf(`{"error": "request aborted after %v"}`, timeoutDuration)

return timeout.Timeout(
timeout.WithTimeout(timeoutDuration),
timeout.WithHandler(func(c *gin.Context) {
c.Next()
}),
timeout.WithResponse(timeoutResponse(timeoutDuration)),
)
timeout.WithErrorHttpCode(http.StatusRequestTimeout),
timeout.WithDefaultMsg(defaultMsg))
}

0 comments on commit dbd772c

Please sign in to comment.