Julik
Julik

Reputation: 7856

Rails 5: Rack app mounted in routes.rb bypasses Rails middleware?

I have a few Rack apps mounted within my routes.rb like so:

mount ImageVise, at: '/ivise'
mount ShaderApiV0, at: '/api/v0'

I am currently migrating from a spaghetti custom Rack stack to Rails, so it makes sense for me to keep those apps. I am noticing that the entire Rails middleware stack that is defined on the application (including my Rack::Cache setup) does function when I call a controller, but does not function when I call URLs controlled by those mini-apps. I am almost certain in Rails 4 it worked however.

For example, if I introduce a custom middleware like so:

class Mittel < Struct.new(:app)
  def call(env)
    a, b, c = app.call(env)
    b['X-Kustom'] = 'olala'
    [a,b,c]
  end
end
config.middleware.insert_before Rack::Head, Mittel

I do see the X-Kustom response header when I request a URL that is driven by the Rails controllers, but I do not get it when I request one of the URLs controlled by the mounted mini-apps. Consequently conditional GET and things like that do not work etc. How can I make it work aside from replicating a third of the Rails middleware stack in config.rb and moving these apps mounts there?

Upvotes: 1

Views: 725

Answers (1)

Julik
Julik

Reputation: 7856

Found it. Turns out I was mounting one of these apps in config.ru as well, under the same URL - and then of course the entire Rails stack gets bypassed, as it should be. Lesson learned.

Upvotes: 1

Related Questions