[Feature Request] Make ActionController's middleware stack configurable per-controller basis

79 views
Skip to first unread message

Nando Vieira

unread,
Sep 28, 2016, 1:36:54 PM9/28/16
to Ruby on Rails: Core
Right now `--api` is all or nothing. Most applications that have an API also have a companion web site. If you plan to keep separated applications, that's fine. The problem lies on using the same app for both your web site and API. 

My proposal is using whatever is defined as the `middleware_stack` on the controller, without injecting/merging any default behavior. In practice, we would set `ActionController::Base.middleware_stack` to `Rails::Application::DefaultMiddlewareStack.new` on railties, allowing replacement per controller basis, like the following:

```ruby
class ActionController::Base
  self.middleware_stack = Rails::Application::DefaultMiddlewareStack.new
end

class ApplicationController < ActionController::Base
end

class ApiController < ActionController::Base
  # This would override the super class (ActionController::Base) middleware stack.
  self.middleware_stack = ActionController::MiddlewareStack.new

  # This controller would have just the following middleware.
  use ::Rack::Runtime
end
```

I'm glad to hear your suggestions. I can happily jump on this.

Cheers,
--
Nando Vieira
Reply all
Reply to author
Forward
0 new messages