From 6508843fcc2f0b2cb8feaaa5699702f78a7f3566 Mon Sep 17 00:00:00 2001 From: 72 <72sevenzy2@gmail.com> Date: Fri, 24 Apr 2026 18:47:21 +0500 Subject: [PATCH 1/5] Refactor Chain function to use Middleware type --- chain.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/chain.go b/chain.go index a2278414..c3fa724c 100644 --- a/chain.go +++ b/chain.go @@ -2,8 +2,11 @@ package chi import "net/http" +// middleware type +type Middleware func(http.Handler) http.Handler + // Chain returns a Middlewares type from a slice of middleware handlers. -func Chain(middlewares ...func(http.Handler) http.Handler) Middlewares { +func Chain(middlewares ...Middleware) Middlewares { return Middlewares(middlewares) } @@ -33,7 +36,7 @@ func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // chain builds a http.Handler composed of an inline middleware stack and endpoint // handler in the order they are passed. -func chain(middlewares []func(http.Handler) http.Handler, endpoint http.Handler) http.Handler { +func chain(middlewares []Middleware, endpoint http.Handler) http.Handler { // Return ahead of time if there aren't any middlewares for the chain if len(middlewares) == 0 { return endpoint From 3cad835c5f7041dc8984381e84cd05c089ae575c Mon Sep 17 00:00:00 2001 From: 72 <72sevenzy2@gmail.com> Date: Fri, 24 Apr 2026 21:38:02 +0500 Subject: [PATCH 2/5] Refactor middleware type and Chain function signature --- chain.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/chain.go b/chain.go index c3fa724c..9ffc585b 100644 --- a/chain.go +++ b/chain.go @@ -2,23 +2,25 @@ package chi import "net/http" -// middleware type -type Middleware func(http.Handler) http.Handler +// middleware type alias +type Middleware = func(http.Handler) http.Handler + +type Middlewares Middleware[] // Chain returns a Middlewares type from a slice of middleware handlers. -func Chain(middlewares ...Middleware) Middlewares { +func Chain(middlewares Middlewares) Middlewares { return Middlewares(middlewares) } // Handler builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. -func (mws Middlewares) Handler(h http.Handler) http.Handler { +func (mws Middleware) Handler(h http.Handler) http.Handler { return &ChainHandler{h, chain(mws, h), mws} } // HandlerFunc builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. -func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler { +func (mws Middleware) HandlerFunc(h http.HandlerFunc) http.Handler { return &ChainHandler{h, chain(mws, h), mws} } @@ -36,7 +38,7 @@ func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // chain builds a http.Handler composed of an inline middleware stack and endpoint // handler in the order they are passed. -func chain(middlewares []Middleware, endpoint http.Handler) http.Handler { +func chain(middlewares Middlewares, endpoint http.Handler) http.Handler { // Return ahead of time if there aren't any middlewares for the chain if len(middlewares) == 0 { return endpoint From e740aec3bbf929679b3d640f033393a563380898 Mon Sep 17 00:00:00 2001 From: 72 <72sevenzy2@gmail.com> Date: Fri, 24 Apr 2026 21:39:52 +0500 Subject: [PATCH 3/5] Rename Middleware to Middlewares in Handler methods --- chain.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chain.go b/chain.go index 9ffc585b..ce3744b6 100644 --- a/chain.go +++ b/chain.go @@ -14,13 +14,13 @@ func Chain(middlewares Middlewares) Middlewares { // Handler builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. -func (mws Middleware) Handler(h http.Handler) http.Handler { +func (mws Middlewares) Handler(h http.Handler) http.Handler { return &ChainHandler{h, chain(mws, h), mws} } // HandlerFunc builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. -func (mws Middleware) HandlerFunc(h http.HandlerFunc) http.Handler { +func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler { return &ChainHandler{h, chain(mws, h), mws} } From a01c4aa28104dc790abc61cf7914e6e11916f186 Mon Sep 17 00:00:00 2001 From: 72 <72sevenzy2@gmail.com> Date: Sun, 26 Apr 2026 18:30:32 +0500 Subject: [PATCH 4/5] Add Middleware type alias in chi.go Added type alias for Middleware and Middlewares. --- chi.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/chi.go b/chi.go index f650116a..99e1cf69 100644 --- a/chi.go +++ b/chi.go @@ -61,6 +61,11 @@ func NewRouter() *Mux { return NewMux() } +// middleware type alias (for chain.go) +type Middleware = func(http.Handler) http.Handler + +type Middlewares Middleware[] + // Router consisting of the core routing methods used by chi's Mux, // using only the standard net/http. type Router interface { From dc24abc14e8718367910f9797060ea572cf4feb7 Mon Sep 17 00:00:00 2001 From: 72 <72sevenzy2@gmail.com> Date: Sun, 26 Apr 2026 18:30:49 +0500 Subject: [PATCH 5/5] Refactor middleware type definitions Removed type alias for Middleware and Middlewares. --- chain.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/chain.go b/chain.go index ce3744b6..838c5b11 100644 --- a/chain.go +++ b/chain.go @@ -2,11 +2,6 @@ package chi import "net/http" -// middleware type alias -type Middleware = func(http.Handler) http.Handler - -type Middlewares Middleware[] - // Chain returns a Middlewares type from a slice of middleware handlers. func Chain(middlewares Middlewares) Middlewares { return Middlewares(middlewares)