I am building a website using actix-web and mongodb crate v1.1.1. At first, I tested the driver for connecting to the database, get the collection handle, create and read form this collection. That test was in the main fn and it works. But, when adding the client and collection handles as an application state extractor the build process is hanging.
public_types.rs
main.rs
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use mongodb::bson::{self, doc, Bson};
use mongodb::error::Error;
use std::env;
use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
use futures::stream::{StreamExt, Next};
use std::sync::Arc;
use mongodb::Client;
use public_types::public_types::AppState;
mod routing;
mod public_types;#[actix_web::main]
async fn main() → std::io::Result<()> {// Load the MongoDB connection string from an environment variable:
let client_uri = “mongodb+srv://admin-zc:EeecOlo773bgzlY7@cluster0.mivex.mongodb.net/test?retryWrites=true&w=majority”;
// env::var(“MONGODB_URI”).expect(“You must set the MONGODB_URI environment var!”);
// A Client is needed to connect to MongoDB:
let client = mongodb::Client::with_uri_str(client_uri.as_ref()).await;
let client = client.unwrap();
let reservation_collection = client.database(“zcdb”).collection(“reserveation”);let data = AppState {
client: Arc::new(client),
reservation_col: Arc::new(reservation_collection),
};HttpServer::new(move || {
App::new()
.data(data.clone())
.configure(routing::routing::home_routing)
.configure(routing::routing::book_routing)
.configure(routing::routing::query_routing)
.configure(routing::routing::login_routing)
})
.bind(“127.0.0.1:8080”)?
.run()
.await
}