From 160c0208b840e9c93069ebc0e590110984c049e0 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 19 Aug 2021 12:10:58 +0200 Subject: api loading db w/ db access in endpoint handlers --- api/Cargo.lock | 12 ++++++------ api/src/db.rs | 5 +++-- api/src/main.rs | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/api/Cargo.lock b/api/Cargo.lock index b6ff59e..274b234 100644 --- a/api/Cargo.lock +++ b/api/Cargo.lock @@ -1018,9 +1018,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.4.1" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68" +checksum = "7ba8d84e9efea6aedae6fed9b6d9cfcaac6c53992b437d79a87a549d5537fea9" [[package]] name = "httpdate" @@ -1231,9 +1231,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "mime" @@ -1414,9 +1414,9 @@ dependencies = [ [[package]] name = "object" -version = "0.26.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c55827317fb4c08822499848a14237d2874d6f139828893017237e7ab93eb386" +checksum = "ee2766204889d09937d00bfbb7fec56bb2a199e2ade963cab19185d8a6104c7c" dependencies = [ "memchr", ] diff --git a/api/src/db.rs b/api/src/db.rs index 59b86c5..932a5f0 100644 --- a/api/src/db.rs +++ b/api/src/db.rs @@ -1,3 +1,4 @@ +use dotenv::dotenv; use mongodb::{error::Error, options::ClientOptions, Client}; use std::env; @@ -10,6 +11,7 @@ pub struct User { } pub async fn init() -> Result { + dotenv().ok(); let host = env::var("MONGO_HOST").expect("MONGO_HOST is not set"); let port = env::var("MONGO_PORT").expect("MONGO_PORT is not set"); @@ -17,6 +19,5 @@ pub async fn init() -> Result { client_options.app_name = Some("pressure-api".to_string()); - let client = Client::with_options(client_options)?; - Ok(client) + return Client::with_options(client_options); } diff --git a/api/src/main.rs b/api/src/main.rs index 8d4f537..ad55992 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -18,7 +18,7 @@ fn init_log() { #[actix_rt::main] async fn main() -> Result<()> { init_log(); - let client = web::Data::new(Mutex::new(db::init())); + let client = web::Data::new(Mutex::new(db::init().await.unwrap())); HttpServer::new(move || { App::new() .app_data(client.clone()) -- cgit v1.2.3