1
0
Fork 0

Wrap capture for services used by pieces of middleware

This commit is contained in:
Romain 2024-09-03 10:30:08 +02:00 committed by GitHub
parent 8ca27b4a1d
commit cf2869407d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 75 additions and 38 deletions

View file

@ -43,9 +43,21 @@ const capturedData key = "capturedData"
// It satisfies the alice.Constructor type.
func Wrap(next http.Handler) (http.Handler, error) {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
c := &Capture{}
newRW, newReq := c.renew(rw, req)
next.ServeHTTP(newRW, newReq)
capt, err := FromContext(req.Context())
if err != nil {
c := &Capture{}
newRW, newReq := c.renew(rw, req)
next.ServeHTTP(newRW, newReq)
return
}
if capt.NeedsReset(rw) {
newRW, newReq := capt.renew(rw, req)
next.ServeHTTP(newRW, newReq)
return
}
next.ServeHTTP(rw, req)
}), nil
}