From f777646394e81eb74544f7d46c3f6a141358e0cb Mon Sep 17 00:00:00 2001 From: DataHearth Date: Wed, 9 Aug 2023 16:30:41 +0200 Subject: [PATCH] add possibility to load env from .env file --- Cargo.lock | 8 ++++++++ bot/Cargo.toml | 1 + bot/src/main.rs | 7 +++++++ database/Cargo.toml | 1 + database/src/main.rs | 15 ++++++++++++++- 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 73e649c..5fcbb2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -467,6 +467,7 @@ name = "bot" version = "0.1.0" dependencies = [ "chrono", + "dotenvy", "graphql_client", "poise", "reqwest", @@ -732,6 +733,7 @@ dependencies = [ "actix-cors", "actix-web", "chrono", + "dotenvy", "env_logger", "juniper 0.16.0-dev", "juniper_actix", @@ -786,6 +788,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dotenvy" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" + [[package]] name = "either" version = "1.9.0" diff --git a/bot/Cargo.toml b/bot/Cargo.toml index 6356024..103d7d7 100644 --- a/bot/Cargo.toml +++ b/bot/Cargo.toml @@ -7,6 +7,7 @@ description = "Discord bot" [dependencies] chrono = { version = "0.4", features = ["serde"] } +dotenvy = "0.15" graphql_client = { version = "0.13", features = ["reqwest"] } poise = "0.5" reqwest = { version = "0.11", features = ["json"] } diff --git a/bot/src/main.rs b/bot/src/main.rs index 8eef196..43b5fac 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -8,6 +8,13 @@ use reqwest::Client; #[tokio::main] async fn main() { + match dotenvy::dotenv() { + Ok(_) => {} + Err(e) => { + println!("Failed to load .env file: {}", e); + } + } + let framework = poise::Framework::builder() .options(poise::FrameworkOptions { commands: vec![help(), add(), list(), search(), remove(), update()], diff --git a/database/Cargo.toml b/database/Cargo.toml index 714effa..4e8fe1a 100644 --- a/database/Cargo.toml +++ b/database/Cargo.toml @@ -9,6 +9,7 @@ description = "Database endpoint with GraphQL for tech-bot" actix-cors = "0.6" actix-web = "4.3" chrono = { version = "0.4", features = ["serde"] } +dotenvy = "0.15" env_logger = "0.10" # until v0.16 is released, use master for uuid support juniper = { git = "https://github.com/graphql-rust/juniper.git", features = [ diff --git a/database/src/main.rs b/database/src/main.rs index 47dad2b..b7a1719 100644 --- a/database/src/main.rs +++ b/database/src/main.rs @@ -40,6 +40,13 @@ static CONTEXT: OnceLock = OnceLock::new(); #[actix_web::main] async fn main() -> io::Result<()> { + match dotenvy::dotenv() { + Ok(_) => {} + Err(e) => { + println!("Failed to load .env file: {}", e); + } + } + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); // Create Juniper schema @@ -59,7 +66,13 @@ async fn main() -> io::Result<()> { .wrap(Cors::permissive()) .wrap(middleware::Logger::default()) }) - .bind(("127.0.0.1", 8080))? + .bind(( + std::env::var("HOST").unwrap_or(String::from("127.0.0.1")), + std::env::var("PORT") + .unwrap_or(String::from("8080")) + .parse::() + .expect("failed to convert port to u16"), + ))? .run() .await }