diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-09-04 15:52:52 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-09-04 15:52:52 +0200 |
commit | fbffca727dde4d27e2e5232d6e5ed2c351257698 (patch) | |
tree | 1085f93dd40ed1c046922aa3a6edac7725b5e0c8 | |
parent | aedafd1490a0447afc29b96d879be7a28dc09a1d (diff) |
Add passing bind addr and port via ENV
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 18 |
3 files changed, 24 insertions, 2 deletions
@@ -279,6 +279,12 @@ dependencies = [ ] [[package]] +name = "anyhow" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28ae2b3dec75a406790005a200b1bd89785afc02517a00ca99ecfe093ee9e6cf" + +[[package]] name = "async-trait" version = "0.1.51" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1842,6 +1848,7 @@ name = "water-levels" version = "0.1.0" dependencies = [ "actix-web", + "anyhow", "env_logger", "float-cmp", "log", @@ -6,6 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1" actix-web = "3.3" env_logger = "0.9" maud = { version = "0.22", features = ["actix-web"] } diff --git a/src/main.rs b/src/main.rs index 370b9a4..cf87858 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,30 @@ +use std::str::FromStr; + use actix_web::App; use actix_web::HttpServer; use actix_web::middleware::Logger; +use anyhow::Context; mod frontend; mod backend; #[actix_web::main] -async fn main() -> std::io::Result<()> { +async fn main() -> anyhow::Result<()> { if let Err(std::env::VarError::NotPresent) = std::env::var("RUST_LOG") { std::env::set_var("RUST_LOG", "actix_web=info"); } env_logger::init(); + let bind = match std::env::var("WATER_LEVELS_HOST") { + Ok(var) => var, + Err(e) => anyhow::bail!("WATER_LEVELS_HOST not available: {:?}", e), + }; + + let port = match std::env::var("WATER_LEVELS_PORT") { + Ok(var) => u16::from_str(&var).context("Parsing port to u16")?, + Err(e) => anyhow::bail!("WATER_LEVELS_PORT not available: {:?}", e), + }; + HttpServer::new(|| { App::new() .wrap(Logger::default()) @@ -20,7 +33,8 @@ async fn main() -> std::io::Result<()> { .service(crate::frontend::make_landscape) .service(crate::frontend::calculate) }) - .bind(("127.0.0.1", 8080))? + .bind((bind, port))? .run() .await + .map_err(anyhow::Error::from) } |