Skip to content

Commit 2056878

Browse files
committed
fix: tests
1 parent c3523de commit 2056878

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

intake.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ func (a *Intake) AddEndpoints(e ...Endpoints) {
9696
// - finalHandler: The handler function that will process the request
9797
// - middleware: Optional route-specific middleware functions
9898
func (a *Intake) AddEndpoint(verb string, path string, finalHandler http.HandlerFunc, middleware ...MiddleWare) {
99-
mws := append(a.GlobalMiddleware, middleware...)
100-
for i := len(mws) - 1; i >= 0; i-- {
101-
if mws[i] != nil {
102-
finalHandler = mws[i](finalHandler)
103-
}
104-
}
105-
10699
// Store the route in our registry
107100
if methods, exists := a.registeredRoutes[path]; exists {
108101
a.registeredRoutes[path] = append(methods, verb)
@@ -111,17 +104,36 @@ func (a *Intake) AddEndpoint(verb string, path string, finalHandler http.Handler
111104
}
112105

113106
handlerKey := fmt.Sprintf("%s %s", verb, path)
114-
a.Mux.HandleFunc(handlerKey, func(w http.ResponseWriter, r *http.Request) {
115-
// Apply panic recovery if a handler is provided
107+
108+
// Apply global middleware first
109+
var handler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
110+
// Apply panic recovery here to capture panics in both global and route middleware
116111
if a.PanicHandler != nil {
117112
defer func() {
118113
if err := recover(); err != nil {
119114
a.PanicHandler(w, r, err)
120115
}
121116
}()
122117
}
123-
finalHandler(w, r)
124-
})
118+
119+
// Apply route middleware and the final handler
120+
routeHandler := finalHandler
121+
for i := len(middleware) - 1; i >= 0; i-- {
122+
if middleware[i] != nil {
123+
routeHandler = middleware[i](routeHandler)
124+
}
125+
}
126+
routeHandler(w, r)
127+
}
128+
129+
// Apply global middleware in reverse order
130+
for i := len(a.GlobalMiddleware) - 1; i >= 0; i-- {
131+
if a.GlobalMiddleware[i] != nil {
132+
handler = a.GlobalMiddleware[i](handler)
133+
}
134+
}
135+
136+
a.Mux.HandleFunc(handlerKey, handler)
125137
}
126138

127139
// Run starts the HTTP server and handles graceful shutdown on SIGINT/SIGTERM.
@@ -190,4 +202,4 @@ func (a *Intake) AddOptionsEndpoints() {
190202
// Register the OPTIONS handler for this path
191203
a.AddEndpoint(http.MethodOptions, path, optionsHandler)
192204
}
193-
}
205+
}

middleware.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package intake
44

55
import (
66
"net/http"
7+
"strconv"
78
"strings"
89
)
910

@@ -92,7 +93,7 @@ func CORS(config CORSConfig) MiddleWare {
9293
corsHeaders(w, config, origin)
9394

9495
// Handle preflight specific headers
95-
w.Header().Set("Access-Control-Max-Age", string(config.MaxAge))
96+
w.Header().Set("Access-Control-Max-Age", strconv.Itoa(config.MaxAge))
9697

9798
// Set allowed methods
9899
if len(config.AllowedMethods) > 0 {

0 commit comments

Comments
 (0)