This commit is contained in:
BurnyLlama 2021-12-06 20:45:44 +01:00
parent bc26955ae4
commit c4aafe4e40
4 changed files with 42 additions and 1 deletions

4
.gitignore vendored
View File

@ -16,3 +16,7 @@ Cargo.lock
# Added by cargo # Added by cargo
/target /target
# Added by moi:
secrets

View File

@ -7,3 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
rocket = "0.5.0-rc.1" rocket = "0.5.0-rc.1"
mongodb = "2.0.0"
tokio = "*"

5
scripts/run.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/env bash
source secrets
cargo run

View File

@ -1,4 +1,7 @@
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
use mongodb::{bson::doc, options::ClientOptions, Client};
#[get("/")] #[get("/")]
fn index() -> &'static str { fn index() -> &'static str {
@ -7,5 +10,32 @@ fn index() -> &'static str {
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
let mongo_result = start_mongo();
match mongo_result {
Result => println!("Everything is fine!"),
};
rocket::build().mount("/", routes![index]) rocket::build().mount("/", routes![index])
} }
#[tokio::main]
async fn start_mongo() -> mongodb::error::Result<()> {
// Parse your connection string into an options struct
let mut client_options =
ClientOptions::parse(env!("MONGODB_URI"))
.await?;
// Manually set an option
client_options.app_name = Some("Rust Demo".to_string());
// Get a handle to the cluster
let client = Client::with_options(client_options)?;
// Ping the server to see if you can connect to the cluster
client
.database("admin")
.run_command(doc! {"ping": 1}, None)
.await?;
println!("Connected successfully.");
// List the names of the databases in that cluster
for db_name in client.list_database_names(None, None).await? {
println!("{}", db_name);
}
Ok(())
}