From 108f6a6de39e5f5ee64ce166cd2aae9f7dd1ccaf Mon Sep 17 00:00:00 2001 From: John Wang Date: Sat, 28 Oct 2023 19:37:30 -0700 Subject: [PATCH] feat: `net/http/httputilmore`: add `NewServerTimeouts()` --- net/http/httputilmore/http_server.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/net/http/httputilmore/http_server.go b/net/http/httputilmore/http_server.go index 73fd0464..41e55c32 100644 --- a/net/http/httputilmore/http_server.go +++ b/net/http/httputilmore/http_server.go @@ -3,6 +3,7 @@ package httputilmore import ( "log" "net/http" + "time" ) // Log is a custom Http handler that will log all requests. @@ -15,3 +16,16 @@ func Log(handler http.Handler) http.Handler { handler.ServeHTTP(w, r) }) } + +// NewServerTimeouts returns a `*http.Server` with all timeouts set to a single value provided. +func NewServerTimeouts(addr string, handler http.Handler, timeout time.Duration) *http.Server { + return &http.Server{ + Addr: addr, + Handler: handler, + IdleTimeout: timeout, + ReadHeaderTimeout: timeout, + ReadTimeout: timeout, + WriteTimeout: timeout, + MaxHeaderBytes: 1 << 20, + } +}