Fairings: callbacks at launch, liftoff, request, and response time.
Fairings allow for structured interposition at various points in the application lifetime. Fairings can be seen as a restricted form of “middleware”. A fairing is an arbitrary structure with methods representing callbacks that Rocket will run at requested points in a program. You can use fairings to rewrite or record information about requests and responses, or to perform an action once a Rocket application has launched.
You must inform Rocket about fairings that you wish to be active by calling
Rocket::attach() method on the application’s
Rocket instance and
passing in the appropriate
Fairing. For instance, to attach fairings
res_fairing to a new Rocket instance, you might
let rocket = rocket::build() .attach(req_fairing) .attach(res_fairing);
Once a fairing is attached, Rocket will execute it at the appropriate time,
which varies depending on the fairing implementation. See the
trait documentation for more information on the dispatching of fairing
Fairings are executed in the order in which they are attached: the first
attached fairing has its callbacks executed before all others. A fairing can
be attached any number of times. Except for singleton
fairings, all attached instances are polled at runtime.
Fairing callbacks may not be commutative; the order in which fairings are
attached may be significant. It is thus important to communicate specific
fairing functionality clearly.
Fairing should take care to act locally so that the actions
Fairings are not jeopardized. For instance, unless it is made
abundantly clear, a fairing should not rewrite every request.
- A ad-hoc fairing that can be created from a function or closure.
- Information about a
- A bitset representing the kinds of callbacks a
Fairingwishes to receive.
- Trait implemented by fairings: Rocket’s structured middleware.
- A type alias for the return