13

I have a simple application with an HTTP endpoint and a connection to a MongoDB database.

use actix_web::{
    middleware, post,
    web::{self},
    App, HttpServer, Responder,
};
use mongodb::{options::ClientOptions, Client};
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct TestBody {
    name: String,
    age: u8,
}

#[post("/test")]
async fn test(query: web::Json<TestBody>, db: web::Data<Client>) -> impl Responder {
    for db_name in db.list_database_names(None, None).await.unwrap() {
        println!("{}", db_name);
    }

    let res = format!("{} {}", query.name, query.age);
    res
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let connection_string = "secret-connection-string";
    let client_options = ClientOptions::parse(connection_string).await.unwrap();
    let client = Client::with_options(client_options).unwrap();

    HttpServer::new(move || {
        App::new()
            .wrap(middleware::Compress::default())
            .app_data(client.clone())
            .app_data(web::JsonConfig::default())
            .service(test)
    })
    .bind("0.0.0.0:7080")?
    .run()
    .await
}

It compiles and runs just fine. But when trying to access localhost:7080/test, I get the following response:

Requested application data is not configured correctly. View/enable debug logs for more details.

I don't see any logs in the console. How do I view or enable the Actix Web logs?

Tobias S.
  • 21,159
  • 4
  • 27
  • 45

1 Answers1

27

To see the logs of Actix Web, add the env_logger dependency to the cargo.toml.

[dependencies]
env_logger = "0.10.0"

You will also have to set the environment variable RUST_LOG to determine the log level. This can be done at runtime using std::env::set_var.

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    std::env::set_var("RUST_LOG", "debug");
    env_logger::init();

    /* ... */
}

This enables debug logging for Rust and Actix Web.


To solve the original issue: You always need to wrap data passed to app_data() with Data::new().

This is how I did it before:

HttpServer::new(move || {
  App::new()
    /* ... */
    .app_data(client.clone())
    /* ... */
})

How it should be instead:

HttpServer::new(move || {
  App::new()
    /* ... */
    .app_data(Data::new(client.clone())) // <-- Data::new() here
    /* ... */
})
Tobias S.
  • 21,159
  • 4
  • 27
  • 45