Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Middleware from an Engine in an Engine

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.

Sort of like this:

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 #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x6ebf30e1>
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

However, I can't seem to get the NewMiddlewareEngine to show up in middleware. I've tested mounting this:

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 #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x2f9795d8>
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    

And:

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 #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x6ebf30e1>
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

Which both work fine. The issue is when I try to mount the NewMiddlewareEngine 'through' the Engine.

Does anyone know how to configure something like this?

Here's MyMiddlewareEngine mounting:

module MyMiddlewareEngine
  class Railtie < Rails::Railtie
    initializer "add_my_middleware_engine_route_middleware" do |app|
      app.middleware.use 'MyMiddlewareEngine'
    end
  end
end
like image 426
SortingHat Avatar asked Jan 28 '16 20:01

SortingHat


1 Answers

From Rails::Engine Edge API documentation:

 class MyEngine < Rails::Engine
   # Add a load path for this specific Engine
   config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)

   initializer "my_engine.add_middleware" do |app|
     app.middleware.use MyEngine::Middleware
   end
 end

I tend to configure rails middleware to run at the application level and not in an engine (as a separation of responsibilities). I used this approach to separate authentication from a mounted rails application (see example). The configuration for this is in application.rb:

 module MyApp
   class Application < Rails::Application
     ..
     config.middleware.use NewMiddlewareEngine

Do you see any exceptions when you run rails console? Did you try using the constant name like the above instead of a String?

like image 75
Nick Aschenbach Avatar answered Nov 09 '22 01:11

Nick Aschenbach