-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http: make it possible to access http.Server via http.ResponseWriter #12438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Out of interest: is there a nice way to do this in cases where the Please could someone cross reference the context efforts? I have missed those and that sounds interesting. |
The middleware extension pain is why I think we should this into Context (and make the http.Server be a Context value ... http://godoc.org/golang.org/x/net/context#Context see Context.Value). Then we can even make Flusher be a Context value as well. @Sajmani, you have links to http context stuff? |
It seems dirty to be passing the parent Server into its child Handler. The Context still wouldn't solve the problem of the ServeContent error logger without either a modification to the method signature, one of its parameters, or the addition of a global variable. If you're going to make that kind of modification, wouldn't the following suffice? package main
import (
"log"
"net/http"
"os"
)
func main() {
s := &http.Server{Addr: ":8080"}
s.ErrorLog = log.New(os.Stdout, "error: ", log.LstdFlags)
mux := http.NewServeMux()
mux.HandleFunc("/", handleIt(s.ErrorLog))
s.Handler = mux
s.ListenAndServe()
}
func handleIt(logger *log.Logger) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
logger.Println("fail")
}
} I like the idea of the Context, but I'm concerned that it bypasses compile-time safety in favor of flexibility, and it seems like more of a nuclear option to be passing Server down the food chain. |
@wardn if you take out the *Server from the context somewhere in one of the handler, it wouldn't require changes to method signature or a global value that is assuming we are discussing a future where access to a context is idiomatic in a standard library |
So far there's just client side support in ctxhttp: We've discussed extending this to support server handlers as well, but we
|
Maybe a better place to do the context interactions would be to make There's a couple of problems I can think of:
|
We cannot change the Context interface; it's already very widely used On Thu, Sep 10, 2015 at 3:59 PM, Amanda Cameron [email protected]
|
Edit: a Context method on http.Request could return a value that has the On Mon, Sep 14, 2015 at 9:47 AM, Sameer Ajmani [email protected] wrote:
|
Being able to associate a context with a |
Maybe just expanding ctxhttp with handlers that has both a context, a response writer and a request? That way we as as community can finally rally around that approach. I don't see how a solution in net/http that keeps the go1 promise could ever be truly satisfactory. |
Yes, I've discussed ctxhttp handlers with others in another thread. I'll
|
I had an itch in the back of my head that the |
Moving to Go 1.7, since a good solution to this would almost certainly involve contexts, and contexts in stdlib is being discussed for Go 1.7, not 1.6. |
CL https://golang.org/cl/21829 mentions this issue. |
If a handler wants to log an error, the ideal destination would be wherever its Server.ErrorLog points to.
But we can't get to the Server from a handler. Perhaps we can do an optional interface via the ResponseWriter.
Maybe this needs to be tied into various Context efforts.
Related: my comments on https://go-review.googlesource.com/#/c/14161/2/src/net/http/fs.go
/cc @Sajmani @adg @dsymonds @okdave
The text was updated successfully, but these errors were encountered: