aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/middleware/timeout.go
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2024-10-09 16:03:12 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2024-10-09 16:03:12 -0400
commit17e269202ba01e8467a87163395a1e6ac8261e02 (patch)
tree38a4698f8789268a935c133d004f2bb9f7b0d81a /middleware/timeout.go
parenta1af12ca8bf93a14b3017aa463a9463eaa4f1188 (diff)
Refactor middleware to separate package
Diffstat (limited to 'middleware/timeout.go')
-rw-r--r--middleware/timeout.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/middleware/timeout.go b/middleware/timeout.go
new file mode 100644
index 0000000..6a3f5de
--- /dev/null
+++ b/middleware/timeout.go
@@ -0,0 +1,17 @@
+package middleware
+
+import (
+ "context"
+ "net/http"
+ "time"
+)
+
+// Middleware to timeout requests after a given duration
+func Timeout(next http.Handler, duration time.Duration) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ ctx, cancel := context.WithTimeout(r.Context(), duration)
+ defer cancel()
+
+ next.ServeHTTP(w, r.WithContext(ctx))
+ })
+}