Comments (5)
There's no equivalent to RequestRouted
in twirp-ruby, because the service handler is a Rack.app already (see service handlers in the docs).
Resources for Rack middleware:
- https://www.amberbit.com/blog/2011/07/13/introduction-to-rack-middleware/
- https://www.engineyard.com/blog/understanding-rack-apps-and-middleware
In a Rack application, you can easily write your own middleware. For example:
class CalculateSignature
def initialize(app)
@app = app
end
def call(env)
// do stuff before, you can return with status codes and such
@app.call(env)
// do stuff after is also possible
end
end
Then mount that middleware in your rack stack before the Twirp handler (depends if you are using Rails or something else).
Rack responses are in the form [status, headers, response_body]
. If you need to return a Twirp error from the Rack middleware, you can use some helpers in the library:
twerr = Twirp::Error.invalid_argument("invalid encoded signature", foo: "bar-meta")
status = Twirp::ERROR_CODES_TO_HTTP_STATUS[twerr.code]
headers = {'Content-Type' => 'application/json'}
return [status, headers, [JSON.generate(twerr.to_h)]]
There are other implications like triggering error hooks from middleware and such. Adding a before_request_routed
hook would simplify some of this, but it would also add other problems. For example, right now the on_error
hooks always have information about the twirp route, which is useful for reporting. If we halt the request before, then that would not work as expected (could cause nil method_missing exceptions).
from twirp-ruby.
@marioizquierdo ☝️ makes sense! We were thinking we wanted to hook into the Twirp handlers in order to get access to the route information (e.g. rpc_method
et al) but all of that can be parsed out from the Rack env
in the middleware as well (and is also more consistent with our client middleware approach).
Our current approach with the before
hook needs the raw request body, but the before
hook is only given the decoded input so we end up re-encoding that (instead of read
ing from rack_env["rack.input"]
to avoid rewind
ing the input stream repeatedly). Using the middleware forces us to use the Rack input stream but we'll be early enough that we can read
and rewind
without issue.
Thanks for the pointer on reconstructing the Twirp error response, that's something that's not great but entirely workable.
from twirp-ruby.
The Twirp::Service.error_response(twerr)
helper was added in release v1.3.0.
from twirp-ruby.
Added Rack Middleware example in the wiki: https://github.com/twitchtv/twirp-ruby/wiki/Service-Hooks
from twirp-ruby.
Thanks @marioizquierdo !
from twirp-ruby.
Related Issues (20)
- Should failed_precondition be renamed to precondition_failed? HOT 2
- This repository is moving!
- Any plans to introduce client interceptors? HOT 1
- Example could be more idiot-proof - go paths, protoc installation
- Rack 3 errors - rack_request.body.rewind, example app HOT 1
- Compiling protoc-gen-twirp_ruby plugin HOT 1
- Open to new maintainers? HOT 6
- Run ci with different versions of Faraday
- Please publish the 1.11.0 version to Rubygems HOT 6
- Invalid `v7.1.2` release breaking `go get` to latest HOT 1
- Additional Improvements & Options with CodeGen + Dropping Ruby 1.9 HOT 3
- Optional fields (>= proto 3.15) are not supported by the code generator HOT 2
- Namespace not retained when generating rpc calls HOT 1
- Can we relax faraday version constraint? HOT 2
- Add CI?
- Allow specifying charset in the content-type header HOT 2
- "go get -u github.com/twitchtv/twirp-ruby/protoc-gen-twirp_ruby" yields module deprecated error HOT 1
- Task: Integration test for the protoc-gen bin
- Task: add devcontainer to ease dev
- 1.10.0 has breaking changes HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twirp-ruby.