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