Defining middleware with fairings
Middleware can be configured using the Fairing trait. As you may recall, we used fairings when connecting our database, as shown in the following code:
fn main() { Â Â Â Â rocket::ignite() Â Â Â Â Â Â Â Â .mount("/", routes![hello, bye]) Â Â Â Â Â Â Â Â .mount("/items", routes![get_items]) Â Â Â Â Â Â Â Â .attach(DbConn::fairing()) Â Â Â Â Â Â Â Â .launch(); }
The fairing
trait hooks into the request life cycle, thereby receiving callbacks for incoming and outgoing requests. Fairings can edit or record requests coming in and out. However, what they are not used for is terminating/responding to requests. They also cannot inject non-request data into a request. Note that fairings can prevent a server from launching or a server from being configured. Looking at our database, we can use...