|
5 | 5 | extern crate mongodb;
|
6 | 6 |
|
7 | 7 | #[cfg(feature = "tokio-runtime")]
|
8 |
| -// CONNECTION EXAMPLE STARTS HERE |
9 |
| -#[tokio::main] |
10 |
| -async fn main() -> mongodb::error::Result<()> { |
| 8 | +mod async_scram { |
| 9 | + // ASYNC SCRAM CONNECTION EXAMPLE STARTS HERE |
11 | 10 | use mongodb::{options::ClientOptions, Client};
|
12 |
| - let client_options = ClientOptions::parse( |
13 |
| - "mongodb+srv://<username>:<password>@<cluster-url>/<dbname>?w=majority", |
14 |
| - ) |
15 |
| - .await?; |
16 |
| - let client = Client::with_options(client_options)?; |
17 |
| - let database = client.database("test"); |
18 |
| - Ok(()) |
| 11 | + |
| 12 | + #[tokio::main] |
| 13 | + async fn main() -> mongodb::error::Result<()> { |
| 14 | + let client_options = ClientOptions::parse( |
| 15 | + "mongodb+srv://<username>:<password>@<cluster-url>/<dbname>?w=majority", |
| 16 | + ) |
| 17 | + .await?; |
| 18 | + let client = Client::with_options(client_options)?; |
| 19 | + let database = client.database("test"); |
| 20 | + // do something with database |
| 21 | + |
| 22 | + Ok(()) |
| 23 | + } |
| 24 | + // CONNECTION EXAMPLE ENDS HERE |
| 25 | +} |
| 26 | + |
| 27 | +#[cfg(feature = "tokio-runtime")] |
| 28 | +mod async_x509 { |
| 29 | + // ASYNC X509 CONNECTION EXAMPLE STARTS HERE |
| 30 | + use mongodb::{ |
| 31 | + options::{AuthMechanism, ClientOptions, Credential, Tls, TlsOptions}, |
| 32 | + Client, |
| 33 | + }; |
| 34 | + use std::path::PathBuf; |
| 35 | + |
| 36 | + #[tokio::main] |
| 37 | + async fn main() -> mongodb::error::Result<()> { |
| 38 | + let mut client_options = |
| 39 | + ClientOptions::parse("mongodb+srv://<cluster-url>/<dbname>?w=majority").await?; |
| 40 | + client_options.credential = Some( |
| 41 | + Credential::builder() |
| 42 | + .mechanism(AuthMechanism::MongoDbX509) |
| 43 | + .build(), |
| 44 | + ); |
| 45 | + let tls_options = TlsOptions::builder() |
| 46 | + .ca_file_path(PathBuf::from("/path/to/ca-cert")) |
| 47 | + .cert_key_file_path(PathBuf::from("/path/to/cert")) |
| 48 | + .build(); |
| 49 | + client_options.tls = Some(Tls::Enabled(tls_options)); |
| 50 | + let client = Client::with_options(client_options)?; |
| 51 | + |
| 52 | + let database = client.database("test"); |
| 53 | + // do something with database |
| 54 | + |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | + // CONNECTION EXAMPLE ENDS HERE |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(feature = "sync")] |
| 61 | +mod sync_scram { |
| 62 | + // SYNC SCRAM CONNECTION EXAMPLE STARTS HERE |
| 63 | + use mongodb::{options::ClientOptions, sync::Client}; |
| 64 | + |
| 65 | + fn main() -> mongodb::error::Result<()> { |
| 66 | + let client_options = ClientOptions::parse( |
| 67 | + "mongodb+srv://<username>:<password>@<cluster-url>/<dbname>?w=majority", |
| 68 | + )?; |
| 69 | + let client = Client::with_options(client_options)?; |
| 70 | + let database = client.database("test"); |
| 71 | + // do something with database |
| 72 | + |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | + // CONNECTION EXAMPLE ENDS HERE |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(feature = "sync")] |
| 79 | +mod sync_x509 { |
| 80 | + // SYNC X509 CONNECTION EXAMPLE STARTS HERE |
| 81 | + use mongodb::{ |
| 82 | + options::{AuthMechanism, ClientOptions, Credential, Tls, TlsOptions}, |
| 83 | + sync::Client, |
| 84 | + }; |
| 85 | + use std::path::PathBuf; |
| 86 | + |
| 87 | + fn main() -> mongodb::error::Result<()> { |
| 88 | + let mut client_options = |
| 89 | + ClientOptions::parse("mongodb+srv://<cluster-url>/<dbname>?w=majority")?; |
| 90 | + client_options.credential = Some( |
| 91 | + Credential::builder() |
| 92 | + .mechanism(AuthMechanism::MongoDbX509) |
| 93 | + .build(), |
| 94 | + ); |
| 95 | + let tls_options = TlsOptions::builder() |
| 96 | + .ca_file_path(PathBuf::from("/path/to/ca-cert")) |
| 97 | + .cert_key_file_path(PathBuf::from("/path/to/cert")) |
| 98 | + .build(); |
| 99 | + client_options.tls = Some(Tls::Enabled(tls_options)); |
| 100 | + let client = Client::with_options(client_options)?; |
| 101 | + |
| 102 | + let database = client.database("test"); |
| 103 | + // do something with database |
| 104 | + |
| 105 | + Ok(()) |
| 106 | + } |
| 107 | + // CONNECTION EXAMPLE ENDS HERE |
19 | 108 | }
|
20 |
| -// CONNECTION EXAMPLE ENDS HERE |
|
0 commit comments