add possibility to load env from .env file

This commit is contained in:
DataHearth 2023-08-09 16:30:41 +02:00
parent 186b456d2a
commit f777646394
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
5 changed files with 31 additions and 1 deletions

8
Cargo.lock generated
View File

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

View File

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

View File

@ -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()],

View File

@ -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 = [

View File

@ -40,6 +40,13 @@ static CONTEXT: OnceLock<DB> = 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::<u16>()
.expect("failed to convert port to u16"),
))?
.run()
.await
}