Skip to content
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

Positioning a new middleware in stack #1659

Open
marcelobfonseca opened this issue Nov 1, 2020 · 1 comment
Open

Positioning a new middleware in stack #1659

marcelobfonseca opened this issue Nov 1, 2020 · 1 comment

Comments

@marcelobfonseca
Copy link

marcelobfonseca commented Nov 1, 2020

Hello, I'd like to place a particular middleware first in the middleware stack but i notice that theres not a method in Sinatra::Base to accomplish this. The only method available is def use(middleware, *args, &block) which will be placed at the bottom of the stack. I was looking for something similar to Rails config.middleware.insert_before(existing_middleware, new_middleware, args). Can we have a feature for this? In the meanwhile, how can I extend Sinatra / Base to insert a middleware at a certain position on the stack?

@dentarg
Copy link
Member

dentarg commented Jan 8, 2022

I've seen this being done by adding behavior to the setup_default_middleware method

See https://github.com/rapid7/metasploit-framework/pull/12980/files#diff-450c1438fd1939e93afa67f321933d2b44a8b254ad6ce4616d08269588b623e6 for the example

I tried it myself over at https://github.com/dentarg/gists/tree/10d569f00b83d549395c18a4cb87462670b91e1c/gists/sinatra-insert-middleware and it seems to work still

You can also call use <middleware> in config.ru to add middlewares that should run before Sinatra does

dentarg added a commit to dentarg/sinatra that referenced this issue Jan 5, 2024
Also remove the very old[1] `does not include ...` comment.

Fighting the test I had to change made me realize just how much the
order of middlewares matters. Not very intuitive 😞.
Maybe someday someone will get to sinatra#1659

Close sinatra#1930

1: sinatra@0985552
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants