Giter Club home page Giter Club logo

Comments (5)

marioizquierdo avatar marioizquierdo commented on August 10, 2024

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:

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.

mtodd avatar mtodd commented on August 10, 2024

@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 reading from rack_env["rack.input"] to avoid rewinding 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.

marioizquierdo avatar marioizquierdo commented on August 10, 2024

The Twirp::Service.error_response(twerr) helper was added in release v1.3.0.

from twirp-ruby.

marioizquierdo avatar marioizquierdo commented on August 10, 2024

Added Rack Middleware example in the wiki: https://github.com/twitchtv/twirp-ruby/wiki/Service-Hooks

from twirp-ruby.

iancanderson avatar iancanderson commented on August 10, 2024

Thanks @marioizquierdo !

from twirp-ruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.