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) end end api = CustomBuilder.new do run App::API end # 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. 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 #reverse
implementation 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, 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 rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.