From 48c0be51dcde46764e7137bd61372b5c3a816431 Mon Sep 17 00:00:00 2001 From: Levente Nas Date: Mon, 15 Sep 2025 13:04:21 -0400 Subject: [PATCH] feat: add optional http server config params --- unstable/simplerouter/router.go | 42 ++++++++++++++++++++++------ unstable/simplerouter/router_test.go | 3 +- unstable/simplerouter/types.go | 11 +++++++- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/unstable/simplerouter/router.go b/unstable/simplerouter/router.go index f42bf0b..00365a8 100644 --- a/unstable/simplerouter/router.go +++ b/unstable/simplerouter/router.go @@ -8,12 +8,26 @@ import ( "os/signal" "syscall" "time" + + Type "github.com/lbatuska/goutils/type" ) -func (rg *RouteGroup) StartServer(addr string) *http.Server { - server := &http.Server{ - Addr: addr, - Handler: rg, +func (rg *RouteGroup) StartServer(addr string, config Type.Optional[ServerConfig]) *http.Server { + var server *http.Server + if config.HasValue() { + serverConfig := config.Unwrap() + server = &http.Server{ + Addr: addr, + Handler: rg, + ReadTimeout: serverConfig.ReadTimeout, + ReadHeaderTimeout: serverConfig.ReadHeaderTimeout, + WriteTimeout: serverConfig.WriteTimeout, + } + } else { + server = &http.Server{ + Addr: addr, + Handler: rg, + } } go func() { @@ -25,10 +39,22 @@ func (rg *RouteGroup) StartServer(addr string) *http.Server { return server } -func (rg *RouteGroup) StartWithGracefulShutdown(addr string) { - server := &http.Server{ - Addr: addr, - Handler: rg, // This will use your RouteGroup (with ServeMux) as the handler +func (rg *RouteGroup) StartWithGracefulShutdown(addr string, config Type.Optional[ServerConfig]) { + var server *http.Server + if config.HasValue() { + serverConfig := config.Unwrap() + server = &http.Server{ + Addr: addr, + Handler: rg, + ReadTimeout: serverConfig.ReadTimeout, + ReadHeaderTimeout: serverConfig.ReadHeaderTimeout, + WriteTimeout: serverConfig.WriteTimeout, + } + } else { + server = &http.Server{ + Addr: addr, + Handler: rg, + } } // Start the server in a separate goroutine diff --git a/unstable/simplerouter/router_test.go b/unstable/simplerouter/router_test.go index d242d68..f8d8fb1 100644 --- a/unstable/simplerouter/router_test.go +++ b/unstable/simplerouter/router_test.go @@ -9,6 +9,7 @@ import ( "time" Testing "github.com/lbatuska/goutils/testing" + Type "github.com/lbatuska/goutils/type" ) func setupServer(wg *sync.WaitGroup) { @@ -23,7 +24,7 @@ func setupServer(wg *sync.WaitGroup) { w.WriteHeader(200) })) - simpleRouter.StartWithGracefulShutdown("0.0.0.0:8080") + simpleRouter.StartWithGracefulShutdown("0.0.0.0:8080", Type.None[ServerConfig]()) } func sendKillSignal(wg *sync.WaitGroup) { diff --git a/unstable/simplerouter/types.go b/unstable/simplerouter/types.go index 29c9b2e..df66347 100644 --- a/unstable/simplerouter/types.go +++ b/unstable/simplerouter/types.go @@ -1,6 +1,9 @@ package SimpleRouter -import "net/http" +import ( + "net/http" + "time" +) type Middleware func(http.Handler) http.Handler @@ -10,3 +13,9 @@ type RouteGroup struct { middlewares []Middleware // Stack of middlewares that will be applied on a handler in order global_middlewares *[]Middleware } + +type ServerConfig struct { + ReadTimeout time.Duration + ReadHeaderTimeout time.Duration + WriteTimeout time.Duration +} -- 2.49.1