Rails Engine Adds Host-Only Middleware

I design a gem which is also Rails::Engine

I would like the engine to add custom middleware to the host application, and I did it with the following code

module MyModule
  class Engine < ::Rails::Engine
    isolate_namespace MyModule

    initializer "my_gem.middleware" do |app|
      app.config.app_middleware.use "MyModule::MyMiddleware"
    end
  end
end

However, this also adds middleware to those routes defined in the routes.rb Engine file. How can i avoid this? I want middleware to be added to the host application.

For example, consider the following routes defined in the host application.

Rails.application.routes.draw do
  mount MyModule::Engine => "/engine"
  root :to => Proc.new { |env| [200, {'Content-Type' => 'text/html'}, ["Hello World"]] }
end

Everything under /engineshould NOT passMyMiddleware

I probably go the wrong way to achieve this, and maybe I should look at some other solution?

+4
1

, , . . URL- , , , URL-/. , app.config.middleware.use, app.config.app_middleware.use

+3

Source: https://habr.com/ru/post/1536068/


All Articles