I've started a pull request in rack: https://github.com/rack/rack/pull/1112, with the goal to support suck a feature. According to the spec, the middleware stack would have to implement the #to_app method. The other necessity would be to make the Rails.appliction.build_request a public method, as non-rails apps would have to have the env pre-initialized for its custom rack middlewares.
2016-09-13 16:27 GMT+03:00 ChuckE <[email protected]>: > > This can be considered chore/feature. > > As of now, Rails middlewares are unusable outside of a Rails app. I've > been trying to use Rack::URLMap to map subpaths to different apps: > > # config.ru > app = Rack::URLMap.new( > "/" => Rails.application, > "/api" => App::API > ) > > run app > > this works, but I lose all the middlewares of the rails app, one of them > being the authentication layer, which is handled by devise. > > so I went further and have been trying to do something like this: > > class CustomBuilder < Rack::Builder > def call(env) > req = Rails.application.send(:build_request, env) > super(req.env) > endend > > api = CustomBuilder.new do > run App::APIend > # ugly, but rack doesn't allow me anything else > api.instance_variable_get(:@use, Rack.application.config.middleware) > > app = Rack::URLMap.new( > "/" => Rails.application, > "/api" => api > > This will break, because Rack::Builder#to_app requires the middleware to > be reversable > <https://github.com/rack/rack/blob/master/lib/rack/builder.rb#L147>. From > the same link, you can see that the middleware must respond to []. > > The first approach seems to be to implement ActionDispatch:: > MiddlewareStack#reverse (it is an Enumerable, not an Array), and to > implement ActionDispatch::MiddlewareStack::Middleware#[](could be a > simple alias to ActionDispatch::MiddlewareStack::Middleware#build). The > #reverseimplementation is however "dangerous", because it means that > existing #use calls which do global mutation will be executed as many > times as the number of apps mapped in my Rack::URLMap app. This is why this > would break <https://github.com/plataformatec/devise/issues/4291>, but > then again, the requirement is from rack, so maybe this could be deviced > there. > > Rails version: > > 5.0.0.1 > > -- > You received this message because you are subscribed to a topic in the > Google Groups "Ruby on Rails: Core" group. > To unsubscribe from this topic, visit https://groups.google.com/d/ > topic/rubyonrails-core/sIamWxrwHFo/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.
