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...