use mongodb::options::Acknowledgment;
use mongodb::options::WriteConcern;
use crate::core::error2::Error;
use crate::core::error2::Result;
pub async fn make_mongo(settings: &super::MongoSettings) -> Result<mongodb::Client> {
let mut client_options = match mongodb::options::ClientOptions::parse(settings.server()).await {
Ok(client) => client,
Err(e) => {
log::error!(
"mongodb_initialize_failed: server={}, error={:?}",
settings,
e
);
return Err(Error::UnexpectedError(anyhow::anyhow!(e)));
}
};
let wc = WriteConcern::builder()
.w(Acknowledgment::Majority)
.journal(true)
.build();
client_options.min_pool_size = Some(5);
client_options.max_pool_size = Some(10);
client_options.write_concern = Some(wc);
match mongodb::Client::with_options(client_options) {
Ok(client) => {
log::info!("mongodb_connect_successful: server={}", settings);
Ok(client)
}
Err(e) => {
log::error!("mongodb_connect_failed: server={}, error={:?}", settings, e);
Err(Error::UnexpectedError(anyhow::anyhow!(e)))
}
}
}