Answer the question
In order to leave comments, you need to log in
How to check if a response has been sent before and where to add this check?
There is a router ( code here ). You can add global middleware to the router, for all paths, and individual ones.
The problem is this: let's say if the user adds 2 global middleware, and in the first middleware sends a response
writer.WriteHeader(403)
writer.Write([]byte("что-то"))
http: superfluous response.WriteHeader call
func middlewareChainer(middlewares []TheMiddleware, next http.Handler) http.Handler {
for i := len(middlewares) - 1; i >= 0; i-- {
next = middlewares[i](next)
}
return next
}
Answer the question
In order to leave comments, you need to log in
If the Middleware has written the headers and the response, it should call return, not next
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question