We’ve been creating a set of Rack applications that we are tying together using Padrino.
Each application represents an API endpoint for a set of related services on a distinct resource, e.g.
This is nice. But we still need to share some common API-level functions, such as request timing etc. between the all applications. This means for every API call in every application we want to log the time the request started at, store the time in the request and then log the elapsed time when the request ended. So:
- Before filter: Log time, store in request as
- After filter: Read
@start_timefrom request and log the elapsed time between it and now
Now that we had this, how could we apply this to all applications? In other words, how do you avoid plastering these before and after filters into every application?
Well we were already creating all applications as a sub-class of
BaseApp – mostly in order to share settings. This was working great, we could set lots of configuration up in the
BaseApp class and the
CustomerApp class would just inherit it, and override it if required. But you can’t use it to share the filters, because filters are not inherited.
It worked great. The Ruby community to the rescue again!