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/src/db.rs | 5 +++-- api/src/main.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'api/src') 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