There are lots of resource about how to share context between members of a chain of http-handlers. I've studied what came in my way and saw different ways of doing it (even written a lib based on
Alice that supports context).
There are two main approaches here:
- Passing the context object explicitly to the handler.
- Sharing the context by other means without changing the signatures.
The first approach seems to be the prominent one and now by popping up the `x/net/context` everywhere, it is even more in demand. The second approach, which can be achieved in different ways (shared map like in
gorilla context or use a struct as a context as in
gocraft/web) is less popular.
Question: What are Pros & Cons of each approach? Which one is more Godiomatic?
(IMHO) It seems something like:
type Handler2 interface {
http.Handler // embedded classic middleware interface
// ... other parts like (probabely in a lazy manner):
Context() *context.Context
}
Is more suited to solve this problem. Context should be seen as a facility service. What if I do not want Context? To be created and make every request more bloated? And if a problem of a `service` nature gets solved today by means of changing signatures, soon there will be other cases and we will need other signatures.
One could argue that having a context in a chain of request handlers is so essential that changing the signature in the standard lib would worth it & benefits Go ecosystem (& probably he could be right; I don't know. I want to study this). But solving this problem from a `service-like nature` point of view would help with overcoming architectural obstacles, in a more general way (OK; context can be hard coded here; no need for a `that-generic` solution; but it can be a pattern).
And there should be a default plumber (chain-of-http-handlers+context handler) in standard library (or in code of conduct!
I see many people give up Go because of Analysis Paralysis phenomena & it should be in attention spot of the community. This happens a lot; far more than expected).