Search⌘ K
AI Features

Logger, Cors and Sessions

Explore how to integrate Logger, Cors, and Sessions middleware in Rust using Actix Web. Understand how to log requests, configure cross-origin resource sharing, and manage user sessions to enhance your backend application's functionality and security.

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.

Rust 1.40.0
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 require the middleware and ...