diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2024-10-09 16:03:12 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2024-10-09 16:03:12 -0400 |
| commit | 17e269202ba01e8467a87163395a1e6ac8261e02 (patch) | |
| tree | 38a4698f8789268a935c133d004f2bb9f7b0d81a /middleware/timeout.go | |
| parent | a1af12ca8bf93a14b3017aa463a9463eaa4f1188 (diff) | |
Refactor middleware to separate package
Diffstat (limited to 'middleware/timeout.go')
| -rw-r--r-- | middleware/timeout.go | 17 |
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)) + }) +} |
