Make rails middlewares reusable outside of rails

31 views
Skip to first unread message

ChuckE

unread,
Sep 13, 2016, 9:27:05 AM9/13/16
to Ruby on Rails: Core


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 #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, but then again, the requirement is from rack, so maybe this could be deviced there.

Rails version:

5.0.0.1

Tiago Cardoso

unread,
Sep 13, 2016, 10:00:08 AM9/13/16
to rubyonra...@googlegroups.com
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. 

--
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.

Reply all
Reply to author
Forward
0 new messages