1
0
mirror of https://github.com/actix/examples synced 2025-02-20 08:20:32 +01:00

fixed env variable not loading

This commit is contained in:
kunal171 2024-01-02 19:52:30 +05:30
parent 887d3779f4
commit 871b3470a7
3 changed files with 13 additions and 2 deletions

11
Cargo.lock generated
View File

@ -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",
]

View File

@ -7,3 +7,4 @@ edition = "2021"
actix-web.workspace = true
mongodb = "2"
serde.workspace = true
dotenv = "0.15"

View File

@ -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");