twitchtv / twirp

A simple RPC framework with protobuf service definitions
https://twitchtv.github.io/twirp/docs/intro.html
Apache License 2.0
7.2k stars 326 forks source link

gorilla mux not working with twirp handler #54

Closed barthr closed 6 years ago

barthr commented 6 years ago

I was trying twirp today to add some calls to an existing service which uses the gorilla mux (https://github.com/gorilla/mux). But the handler from twirp wasn't working with the mux from gorilla. Is this intentional or something we could support in the future?

The mux from gorilla implements the http.Handler interface and also the Handle method for registring a http.Handler with a specific path.

spenczar commented 6 years ago

This should work without much trouble. Like so:

svc := example.NewHaberdasherServer(...)

r := mux.NewRouter()
r.PathPrefix(example.HaberdasherPathPrefix).Handler(svc)

Could you post the code that isn't working for you?

marioizquierdo commented 6 years ago

Twirp services can only be mounted on the root path. See, the generated service router works on the req.URL.Path https://github.com/twitchtv/twirp/blob/dff337b186d4ea1ada1d1e54ef422fdbcfa3113d/example/service.twirp.go#L156 it looks for an exact match, independently of where it was mounted. Maybe this is the issue?

barthr commented 6 years ago

Ahh I see. Didn't notice that, I was defining it like this:

svc := example.NewHaberdasherServer(...)

r := mux.NewRouter()
r.Handle(example.HaberdasherPathPrefix, svc)

Mental note, always mount it on the root path with PathPrefix Thanks!

spenczar commented 6 years ago

Right, github.com/gorilla/mux uses r.Handle as an exact match, not a prefix match.