Alternative controllers with middleware
Coach improves your controller code by encouraging:
before_filter's and interdependent concerns. Build
Middleware that does a single job, and does it well.provide/require interface to guarantee that your
middlewares load data in the right order when you first boot your app.For our policy on compatibility with Ruby and Rails versions, see COMPATIBILITY.md.
To get started, just add Coach to your Gemfile, and then run bundle:
gem 'coach'Coach works with Ruby versions 3.1 and onwards.
The best way to see the benefits of Coach is with a demonstration.
…For routes that represent resource actions, Coach provides some syntactic sugar to allow concise mapping of endpoint to handler in Rails apps.
# config/routes.rb
Example::Application.routes.draw do
router = Coach::Router.new(self)
router.draw(Routes::Users,
base: "/users",
actions: [
:index,
:show,
:create,
:update,
disable: { method: :post, url: "/:id/actions/disable" }
])
endDefault actions that conform to standard REST principles can be easily loaded, with the users resource being mapped to:
| Method | URL | Description |
|---|---|---|
GET |
/users |
Index all users |
GET |
/users/:id |
Get user by ID |
POST |
/users |
Create new user |
PUT |
/users/:id |
Update user details |
POST |
/users/:id/actions/disable |
Custom action routed to the given path suffix |
If you're using Zeitwerk, you can pass the name of the module to #draw, instead of the module
itself.
# config/routes.rb
Example::Application.routes.draw do
router = Coach::Router.new(self)
router.draw("Routes::Users",
base: "/users",
actions: [
:index,
:show,
:create,
:update,
disable: { method: :post, url: "/:id/actions/disable" }
])
endBy now you'll probably agree that the rack response format isn't the nicest way to render responses. Coach comes sans renderer, and for a good reason.
We initially built a Coach::Renderer module, but soon realised that doing so would
prevent us from open sourcing. Our Renderer was 90% logic specific to the way our APIs
function, including handling/formatting of validation errors, logging of unusual events
etc.
What worked well for us is a standalone Renderer class that we could require in all our
middleware that needed to format responses. This pattern also led to clearer code -
consistent with our preference for explicit code, stating Renderer.new_resource(...) is
instantly more debuggable than an inherited method on all middlewares.
Coach uses ActiveSupport::Notifications to issue events that can be used to profile
middleware.
Information for how to use ActiveSupports notifications can be found
here.
| Event | Arguments |
|---|---|
start_handler.coach |
event(:middleware, :request) |
start_middleware.coach |
event(:middleware, :request) |
finish_middleware.coach |
start, finish, id, event(:middleware, :request) |
finish_handler.coach |
start, finish, id, event(:middleware, :request) |
request.coach |
event containing request data and benchmarking |
Of special interest is request.coach, which publishes statistics on an entire
middleware chain and request. This data is particularly useful for logging, and is our
solution to Rails process_action.action_controller event emitted on controller requests.
The benchmarking data includes information on how long each middleware took to process, along with the total duration of the chain.
For coach to emit request.coach events, it first needs to be subscribed to handler/middleware events:
Coach::Notifications.subscribe!
# Now you can subscribe to and use request.coach events, e.g.
ActiveSupport::Notifications.subscribe("request.coach") do |_, event|
Rails.logger.info(event)
endYou can add additional metadata to the notifications published by Coach by calling the
log_metadata method from inside your Coach middlewares.
class Tracking UserDecorator -> AuthenticationIf there are multiple paths to a middleware that provides that value, all of them will be listed. Similarly, if multiple middlewares provide the same value, all of them will be listed.
Given that the Coach CLI is mostly aimed at large Rails apps using Coach, it would be an oversight for us not to integrate it with Spring.
To enable the use of Spring with the Coach CLI, add the following to config/spring.rb or
an equivalent Rails config file.
require "spring/commands/coach"On GoCardless' main Rails app, using Spring reduces the time to run coach commands from
around 15s to 1s.
While we think the commands we've already built are useful, we do have some ideas to go further, including:
requires
statement)GoCardless ♥ open source. If you do too, come join us.
No open issues yet, or sync has not completed.