From 871b3470a79620ef1366bc59c89df18048b59318 Mon Sep 17 00:00:00 2001 From: kunal171 Date: Tue, 2 Jan 2024 19:52:30 +0530 Subject: [PATCH] fixed env variable not loading --- Cargo.lock | 11 +++++++++-- databases/mongodb/Cargo.toml | 1 + databases/mongodb/src/main.rs | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3cf13198..a911cfb7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2839,6 +2839,12 @@ dependencies = [ "log", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dotenvy" version = "0.15.7" @@ -4834,6 +4840,7 @@ name = "mongodb" version = "1.0.0" dependencies = [ "actix-web", + "dotenv", "mongodb 2.7.1", "serde", ] @@ -8197,8 +8204,8 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if 0.1.10", - "rand 0.7.3", + "cfg-if 1.0.0", + "rand 0.8.5", "static_assertions", ] diff --git a/databases/mongodb/Cargo.toml b/databases/mongodb/Cargo.toml index 82bc6500..03c07fb7 100644 --- a/databases/mongodb/Cargo.toml +++ b/databases/mongodb/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" actix-web.workspace = true mongodb = "2" serde.workspace = true +dotenv = "0.15" \ No newline at end of file diff --git a/databases/mongodb/src/main.rs b/databases/mongodb/src/main.rs index 2fbbf2f3..5e73618e 100644 --- a/databases/mongodb/src/main.rs +++ b/databases/mongodb/src/main.rs @@ -7,6 +7,7 @@ mod test; use actix_web::{get, post, web, App, HttpResponse, HttpServer}; use model::User; use mongodb::{bson::doc, options::IndexOptions, Client, Collection, IndexModel}; +use dotenv::dotenv; const DB_NAME: &str = "myApp"; const COLL_NAME: &str = "users"; @@ -56,6 +57,8 @@ async fn create_username_index(client: &Client) { #[actix_web::main] async fn main() -> std::io::Result<()> { + dotenv().ok(); //loading .env file + let uri = std::env::var("MONGODB_URI").unwrap_or_else(|_| "mongodb://localhost:27017".into()); let client = Client::with_uri_str(uri).await.expect("failed to connect");