如何在引擎中使用引擎中的中间件

Sor*_*Hat 14 ruby middleware ruby-on-rails rails-engines rack-middleware

由于不同的宝石如何在我的系统中相互作用,我将引擎安装到导轨应用程序上.我最近开始研究一种提供一些中间件功能的新gem.

有点像:

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
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法让NewMiddlewareEngine显示在中间件中.我测试了这个:

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    
Run Code Online (Sandbox Code Playgroud)

和:

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
Run Code Online (Sandbox Code Playgroud)

这两个都很好.问题是当我尝试通过引擎安装NewMiddlewareEngine时.

有谁知道如何配置这样的东西?

这是MyMiddlewareEngine安装:

module MyMiddlewareEngine
  class Railtie < Rails::Railtie
    initializer "add_my_middleware_engine_route_middleware" do |app|
      app.middleware.use 'MyMiddlewareEngine'
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

Nic*_*ach 4

来自Rails::Engine Edge API文档:

 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
Run Code Online (Sandbox Code Playgroud)

我倾向于将 Rails 中间件配置为在应用程序级别运行,而不是在引擎中运行(作为职责分离)。我使用这种方法将身份验证与已安装的 Rails 应用程序分开(请参阅示例)。其配置位于application.rb

 module MyApp
   class Application < Rails::Application
     ..
     config.middleware.use NewMiddlewareEngine
Run Code Online (Sandbox Code Playgroud)

运行rails console时您是否看到任何异常?您是否尝试使用上面的常量名称而不是String