load port from env variable

This commit is contained in:
blek 2023-10-10 01:21:52 +10:00
parent 78d495cfa2
commit 600093c97d
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 12 additions and 1 deletions

1
Cargo.lock generated
View File

@ -531,6 +531,7 @@ name = "miniqr"
version = "0.1.0"
dependencies = [
"image",
"log",
"qrcode",
"tokio",
"urlencoding",

View File

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

View File

@ -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::<u16>();
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;
}