...

/

Logger, Cors and Sessions

Logger, Cors and Sessions

Learn to add middleware to an actix-web application.

We'll cover the following...

Sometimes, we require middleware to integrate some functionality to our software. We’ll integrate three of them in this lesson.

Logger

Logger is useful for registering requests and their respective responses.

Luckily, it’s integrated into the framework, so we just need to configure it to work. We can see an example of a default logger.

Press + to interact
use actix_web::middleware::Logger;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
HttpServer::new(|| {
App::new()
.wrap(Logger::default()) // This is to register a middleware
.wrap(Logger::new("%a %{User-Agent}i"))
.data(establish_connection())
.route("products", web::get().to(product_list))
})
.bind(("127.0.0.1", 9400))?
.run()
.await
}

We ...