title: How to use Middleware from an Engine in an Engine
body: <p>Because of how the different gems interact in my system, I have an engine mounted onto a rails application. I recently started working on a new gem that provides some middleware functionality.</p>
  
  <p>Sort of like this:</p>
  
  <pre><code>BaseApp
  \
    Engine
    \
     NewMiddlewareEngine
  
  # BaseApp/Gemfile
  gem 'Engine'
  
  # Engine/Gemfile
  gem 'NewMiddlewareEngine'
  
  # rake middleware output:
  user@laptop[BaseApp]$ bundle exec rake middleware
  use Rack::Sendfile
  use ActionDispatch::Static
  use Rack::Lock
  use #&lt;ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x6ebf30e1&gt;
  use Rack::Runtime
  use Rack::MethodOverride
  use ActionDispatch::RequestId
  use Rails::Rack::Logger
  use ActionDispatch::ShowExceptions
  use ActionDispatch::DebugExceptions
  use ActionDispatch::RemoteIp
  use ActionDispatch::Reloader
  use ActionDispatch::Callbacks
  use ActiveRecord::Migration::CheckPending
  use ActiveRecord::ConnectionAdapters::ConnectionManagement
  use ActiveRecord::QueryCache
  use ActionDispatch::Cookies
  use ActionDispatch::Session::CookieStore
  use ActionDispatch::Flash
  use CatchJsonParseErrors
  use ActionDispatch::ParamsParser
  use Rack::Head
  use Rack::ConditionalGet
  use Rack::ETag
  run BaseApp::Application.routes
  </code></pre>
  
  <p>However, I can't seem to get the NewMiddlewareEngine to show up in middleware. I've tested mounting this:</p>
  
  <pre><code>BaseApp
  \
   NewMiddlewareEngine
  
  # BaseApp/Gemfile
  gem 'NewMiddlewareEngine'
  
  # rake middleware output:
  user@laptop[BaseApp]$ bundle exec rake middleware
  use Rack::Sendfile
  use ActionDispatch::Static
  use Rack::Lock
  use #&lt;ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x2f9795d8&gt;
  use Rack::Runtime
  use Rack::MethodOverride
  use ActionDispatch::RequestId
  use Rails::Rack::Logger
  use ActionDispatch::ShowExceptions
  use ActionDispatch::DebugExceptions
  use ActionDispatch::RemoteIp
  use ActionDispatch::Reloader
  use ActionDispatch::Callbacks
  use ActiveRecord::Migration::CheckPending
  use ActiveRecord::ConnectionAdapters::ConnectionManagement
  use ActiveRecord::QueryCache
  use ActionDispatch::Cookies
  use ActionDispatch::Session::CookieStore
  use ActionDispatch::Flash
  use CatchJsonParseErrors
  use ActionDispatch::ParamsParser
  use Rack::Head
  use Rack::ConditionalGet
  use Rack::ETag
  use NewMiddlewareEngine    # Notice it mounts fine on it's own
  run BaseApp::Application.routes    
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>BaseApp
  \
   Engine
  
  # BaseApp/Gemfile
  gem 'Engine'
  
  # rake middleware output:
  user@laptop[BaseApp]$ bundle exec rake middleware
  use Rack::Sendfile
  use ActionDispatch::Static
  use Rack::Lock
  use #&lt;ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x6ebf30e1&gt;
  use Rack::Runtime
  use Rack::MethodOverride
  use ActionDispatch::RequestId
  use Rails::Rack::Logger
  use ActionDispatch::ShowExceptions
  use ActionDispatch::DebugExceptions
  use ActionDispatch::RemoteIp
  use ActionDispatch::Reloader
  use ActionDispatch::Callbacks
  use ActiveRecord::Migration::CheckPending
  use ActiveRecord::ConnectionAdapters::ConnectionManagement
  use ActiveRecord::QueryCache
  use ActionDispatch::Cookies
  use ActionDispatch::Session::CookieStore
  use ActionDispatch::Flash
  use CatchJsonParseErrors
  use ActionDispatch::ParamsParser
  use Rack::Head
  use Rack::ConditionalGet
  use Rack::ETag
  run BaseApp::Application.routes
  </code></pre>
  
  <p>Which both work fine. The issue is when I try to mount the NewMiddlewareEngine 'through' the Engine.</p>
  
  <p>Does anyone know how to configure something like this?</p>
  
  <p>Here's MyMiddlewareEngine mounting:</p>
  
  <pre><code>module MyMiddlewareEngine
    class Railtie &lt; Rails::Railtie
      initializer "add_my_middleware_engine_route_middleware" do |app|
        app.middleware.use 'MyMiddlewareEngine'
      end
    end
  end
  </code></pre>