summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs18
3 files changed, 24 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b75d2a1..87420dc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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",
diff --git a/Cargo.toml b/Cargo.toml
index 5a30e9e..1c086b4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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)
}