diff --git a/.gitignore b/.gitignore index d1bb021..499c4be 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,7 @@ Cargo.lock # Added by cargo /target + + +# Added by moi: +secrets \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 787a097..49b1b03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] rocket = "0.5.0-rc.1" +mongodb = "2.0.0" +tokio = "*" \ No newline at end of file diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..5c64637 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,5 @@ +#!/bin/env bash + +source secrets + +cargo run \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f090233..7c66f77 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,7 @@ -#[macro_use] extern crate rocket; +#[macro_use] +extern crate rocket; +use mongodb::{bson::doc, options::ClientOptions, Client}; + #[get("/")] fn index() -> &'static str { @@ -7,5 +10,32 @@ fn index() -> &'static str { #[launch] fn rocket() -> _ { + let mongo_result = start_mongo(); + match mongo_result { + Result => println!("Everything is fine!"), + }; 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(()) } \ No newline at end of file