Let's say I have code like this
handler := middleware1.New(
middleware2.New(
middleware3.New(
middleware4.New(
NewHandler()
),
),
),
)
http.ListenAndServe(":8080", handler)
where handler has tons of middleware.
Now I want to create custom endpoint, which will skip all the middleware, so nothing what's inside serveHTTP() functions is executed:
http.HandleFunc("/testing", func(
w http.ResponseWriter,
r *http.Request,
) {
fmt.Fprintf(w, "it works!")
return
})
http.ListenAndServe(":8080", handler)
But this doesn't work and /testing is never reached. Ideally, I don't want to modify handler at all, is that possible?
http.HandleFuncapplies to theDefaultServeMux, not yourhandler. Do you want to addhandlerto aServeMuxor do you want to add/testingto yourhandler?/testingto handler won't skip middleware, am I right?handlerdoesn't do any muxing before the middleware, then you need to do it separately -- a ServeMux (or any muxer/router package) can do that for you.