From 600093c97db8c86e2602369f83d28455338391f3 Mon Sep 17 00:00:00 2001 From: blek Date: Tue, 10 Oct 2023 01:21:52 +1000 Subject: [PATCH] load port from env variable --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 11 ++++++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index c9354b3..18a3068 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -531,6 +531,7 @@ name = "miniqr" version = "0.1.0" dependencies = [ "image", + "log", "qrcode", "tokio", "urlencoding", diff --git a/Cargo.toml b/Cargo.toml index 643aaa8..4f55daf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] image = "0.23.0" +log = "0.4.20" qrcode = "0.12.0" tokio = { version = "1.33.0", features = ["full"] } urlencoding = "2.1.3" diff --git a/src/main.rs b/src/main.rs index b15a439..28c56e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,14 @@ use warp::{Filter, reply::Response}; #[tokio::main] async fn main() { + + let port = std::env::var("PORT").unwrap_or("80".to_string()).parse::(); + if port.is_err() { + log::warn!("Could not parse port from environment, falling back to port 80"); + log::warn!("Port parse error message: {}", port.clone().unwrap_err()); + } + let port = port.unwrap_or(80); + // Match any request and return hello world! let routes = warp::path::end() @@ -38,5 +46,6 @@ async fn main() { }) ); - warp::serve(routes).run(([127, 0, 0, 1], 3030)).await; + log::info!("Running on http://0.0.0.0:{port}"); + warp::serve(routes).run(([0, 0, 0, 0], port)).await; } \ No newline at end of file