fplus_database/core/
setup.rs

1use mongodb::{
2    bson::doc,
3    options::{ClientOptions, ServerApi, ServerApiVersion, Tls, TlsOptions},
4    Client,
5};
6
7pub async fn db_health_check(client: Client) -> mongodb::error::Result<()> {
8    // Ping the server to see if you can connect to the cluster
9    client
10        .database("admin")
11        .run_command(doc! {"ping": 1}, None)
12        .await?;
13    println!("Pinged your deployment. You successfully connected to MongoDB!");
14
15    Ok(())
16}
17
18pub async fn setup() -> mongodb::error::Result<Client> {
19    let key = "MONGODB_URL";
20    let value = std::env::var(key).expect("Expected a MONGODB_URL in the environment");
21    let mut client_options = ClientOptions::parse(value).await?;
22
23	let mut tls_options = TlsOptions::default();
24	tls_options.allow_invalid_hostnames = Some(true);
25	tls_options.allow_invalid_certificates = Some(true);
26	client_options.tls = Some(Tls::Enabled(tls_options));
27
28    // Set the server_api field of the client_options object to Stable API version 1
29    let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
30    client_options.server_api = Some(server_api);
31
32    // Get a handle to the cluster
33    let client = Client::with_options(client_options)?;
34
35    Ok(client)
36}