-
Notifications
You must be signed in to change notification settings - Fork 140
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
Document Conflict between gorilla/context & Go 1.7's http.Request.WithContext() #32
Comments
Hi there! I have a project that uses What's the recommended way to keep context info that's backwards compatible? Is it something like this? Any help is greatly appreciated - I'm a big fan of the |
@jordan-wright Yep, use 1.7's context in its entirety. Mixing and matching that and any kind of request map (like gorilla/context) will break things. |
Gotcha. Thanks for the quick reply! The issue that I foresee is that the context package was under a different path pre 1.7, and backwards compatibility is important. You're saying that it's probably best to basically create an abstracted package (like the one I linked to used by mux) that will use the right context package depending on the Go version using build flags? |
@jordan-wright Yep. There really isn't a better way. mux could have kept using gorilla/context in 1.7, but any user who uses (directly, or via a lib) the new |
So I guess that's the part that's throwing me for a loop. I thought To give a concrete (simplified) example, my setup is something like this - I've tried to slim it down as much as possible:
So I'm using mux, sessions, and context. The issue I'm running into strictly with Go 1.7 when hitting /login on the line that
I'm not using |
mux uses the new stdlib context, hence the issue. Other libraries reported Any mixture of any packages that mix the two causes the issue.
|
Roger that. Thanks for the quick responses, and for all your great work on the Gorilla suite! |
@elithrar But it looks gorilla/context is easy to use and understand then the http.Request.WithContext() |
@codeskyblue It "works", but if you - or any library you import - uses the new |
Finally, I modified context, replace *http.Request to *url.URL to make it work after go1.7 |
Required for newer golang compatibility. Otherwise, all gorilla context.Get calls start returning `nil`. `gorilla/context` is forcibly deprecated for newer versions of go. See: - gorilla/context#32 - gorilla/mux#182 If support for old & new golang is wanted, we can revist this and wrap `context` calls and use some linker flag magic to support both. Kinda nifty! See: gorilla/mux#182 (comment)
I'm quite confused. I just migrated from go 1.5 to 1.7 and spent 2 days trying to figure out the issue and finally concluded it was a gorilla/context issue. |
Where would it be helpful to document (beyond where we already do) this? What would have shortcut the 2 days? |
I just switched to golang 1.7 |
@drichelson - any use of a global request map with Go 1.7's Request.WithContext() will fail. You should: a) make sure you are using the latest gorilla/mux gorilla/context can't be changed to use the new context as it would break the API, and be superfluous given that the standard lib now provides a request context. |
@elithrar Thanks for the quick response! |
@drichelson The route name - what (specifically) are you after - the value of If you need to access the name of the route, you should pass it in via middleware. |
@elithrar Yes! I'm after |
(it's not)
You should inject it into middleware, or re-think how you need to use the
name *inside* a request.
Can you explain more about your intent/show an example? Will help me
understand how we can achieve this.
…On Sun, May 14, 2017 at 8:26 PM Dan Richelson ***@***.***> wrote:
@elithrar <https://github.com/elithrar> Yes! I'm after route.GetName().
How would I go about this? It's not clear how the route is made available
from within a request.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#32 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABIcFW8OdNkz_fFHzbCpcCD5KOkvSydks5r58X9gaJpZM4Jjt1d>
.
|
Here's the way things worked for us before go 1.7: (I've simplified things as much as possible)
I have lots of existing metrics and dashboards based on Are you able to show on example of what you mean by "inject it into the middleware"? (this middleware is used across all routes) |
That should work just as before: package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.Handle("/hello", RouteMiddleware(http.HandlerFunc(HelloHandler))).Name("helloRoute")
log.Fatal(http.ListenAndServe("localhost:8000", r))
}
func RouteMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
route := mux.CurrentRoute(r)
log.Printf("current route: %s", route.GetName())
})
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
} ~/Desktop go run mux.go
~/Desktop curl localhost:8000/hello
2017/05/14 21:44:47 current route: helloRoute |
Thanks @elithrar! This is helpful. It looks like the issue is how we were setting up the middleware: router := mux.NewRouter()
chain := alice.New(
func (h http.Handler) http.Handler {
// middleware
return h
},
).Then(router)
http.Handle("/", chain)
err := http.ListenAndServe("8000", nil) |
@elithrar Your example works well, but in an app with hundreds of routes, the alice middleware r.Handle("/hello", RouteMiddleware(http.HandlerFunc(HelloHandler))).Name("helloRoute") We also have several subrouters. Are you aware of a way to add a Middleware to all routes served by the root router? Alice does this for us nicely, but the mux.CurrentRoute() functionality no longer works in alice middleware in go 1.7. |
You can just wrap mux.Router itself -
http.ListenAndServe(":8000", middleware(router))
…On Tue, May 16, 2017 at 1:32 PM Dan Richelson ***@***.***> wrote:
@elithrar <https://github.com/elithrar> Your example works well, but in
an app with hundreds of routes, the alice middleware
chaining makes our code a lot cleaner.
To use your example we'd have to wrap many many lines of code looking
something like this:
r.Handle("/hello", RouteMiddleware(http.HandlerFunc(HelloHandler))).Name("helloRoute")
We also have several subrouters. Are you aware of a way to add a
Middleware to *all* routes served by the root router? Alice does this for
us nicely, but the mux.CurrentRoute() functionality no longer works in
alice middleware in go 1.7.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#32 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABIcPc4GXjSJSZI_OM-jH2B6m8ayfH8ks5r6gfbgaJpZM4Jjt1d>
.
|
RE: gorilla/mux#182
cc/ @kisielk
The text was updated successfully, but these errors were encountered: