ctrl c handler (a.k.a. docker shutdown)

This commit is contained in:
blek 2023-10-10 01:44:23 +10:00
parent 600093c97d
commit 165ffa065a
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 272 additions and 5 deletions

256
Cargo.lock generated
View File

@ -56,6 +56,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
[[package]]
name = "block-buffer"
version = "0.10.4"
@ -65,6 +71,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "bytemuck"
version = "1.14.0"
@ -171,6 +183,16 @@ dependencies = [
"typenum",
]
[[package]]
name = "ctrlc"
version = "3.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82e95fbd621905b854affdc67943b043a0fbb6ed7385fd5a25650d19a8a6cfdf"
dependencies = [
"nix",
"windows-sys",
]
[[package]]
name = "data-encoding"
version = "2.4.0"
@ -212,6 +234,31 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "erased-serde"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c138974f9d5e7fe373eb04df7cae98833802ae4b11c24ac7039a21d5af4b26c"
dependencies = [
"serde",
]
[[package]]
name = "femme"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc04871e5ae3aa2952d552dae6b291b3099723bf779a8054281c1366a54613ef"
dependencies = [
"cfg-if",
"js-sys",
"log",
"serde",
"serde_derive",
"serde_json",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -473,6 +520,15 @@ dependencies = [
"rayon",
]
[[package]]
name = "js-sys"
version = "0.3.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5f195fe497f702db0f318b07fdd68edb16955aed830df8363d837542f8f935a"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.149"
@ -494,6 +550,10 @@ name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
dependencies = [
"serde",
"value-bag",
]
[[package]]
name = "memchr"
@ -530,6 +590,8 @@ dependencies = [
name = "miniqr"
version = "0.1.0"
dependencies = [
"ctrlc",
"femme",
"image",
"log",
"qrcode",
@ -595,6 +657,17 @@ dependencies = [
"version_check",
]
[[package]]
name = "nix"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
"bitflags 2.4.0",
"cfg-if",
"libc",
]
[[package]]
name = "num-integer"
version = "0.1.45"
@ -728,7 +801,7 @@ version = "0.16.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6"
dependencies = [
"bitflags",
"bitflags 1.3.2",
"crc32fast",
"deflate",
"miniz_oxide 0.3.7",
@ -824,7 +897,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
"bitflags",
"bitflags 1.3.2",
]
[[package]]
@ -886,6 +959,15 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_fmt"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d4ddca14104cd60529e8c7f7ba71a2c8acd8f7f5cfcdc2faf97eeb7c3010a4"
dependencies = [
"serde",
]
[[package]]
name = "serde_json"
version = "1.0.107"
@ -970,6 +1052,74 @@ version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]]
name = "sval"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55089b73dfa822e1eb6b635f8795215512cca94bfae11aee3a1a06228bc88bb"
[[package]]
name = "sval_buffer"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df307823073d63f1fb126895439fead41afc493ea35d636cceedef9f6b32ba81"
dependencies = [
"sval",
"sval_ref",
]
[[package]]
name = "sval_dynamic"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5f8e4c4d6d028d3cbff66c2bb3d98181d031d312b7df4550eea7142d7036f37"
dependencies = [
"sval",
]
[[package]]
name = "sval_fmt"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad53f8eb502b0a3051fea001ae2e3723044699868ebfe06ea81b45545db392c2"
dependencies = [
"itoa",
"ryu",
"sval",
]
[[package]]
name = "sval_json"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f913253c9f6cd27645ba9a0b6788039b5d4338eae0833c64b42ef178168d2862"
dependencies = [
"itoa",
"ryu",
"sval",
]
[[package]]
name = "sval_ref"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66a9661412d06740ebe81512a527b3d9220460eb7685f4399232c0e670108cb7"
dependencies = [
"sval",
]
[[package]]
name = "sval_serde"
version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8d077e98c1c8dfa466837ae0ec1e03c78138d42ac75662dac05e1bf0aebae20"
dependencies = [
"serde",
"sval",
"sval_buffer",
"sval_fmt",
]
[[package]]
name = "syn"
version = "2.0.38"
@ -1205,6 +1355,42 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "value-bag"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d92ccd67fb88503048c01b59152a04effd0782d035a83a6d256ce6085f08f4a3"
dependencies = [
"value-bag-serde1",
"value-bag-sval2",
]
[[package]]
name = "value-bag-serde1"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0b9f3feef403a50d4d67e9741a6d8fc688bcbb4e4f31bd4aab72cc690284394"
dependencies = [
"erased-serde",
"serde",
"serde_fmt",
]
[[package]]
name = "value-bag-sval2"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30b24f4146b6f3361e91cbf527d1fb35e9376c3c0cef72ca5ec5af6d640fad7d"
dependencies = [
"sval",
"sval_buffer",
"sval_dynamic",
"sval_fmt",
"sval_json",
"sval_ref",
"sval_serde",
]
[[package]]
name = "version_check"
version = "0.9.4"
@ -1257,6 +1443,72 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7706a72ab36d8cb1f80ffbf0e071533974a60d0a308d01a5d0375bf60499a342"
dependencies = [
"cfg-if",
"serde",
"serde_json",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ef2b6d3c510e9625e5fe6f509ab07d66a760f0885d858736483c32ed7809abd"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dee495e55982a3bd48105a7b947fd2a9b4a8ae3010041b9e0faab3f9cd028f1d"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[package]]
name = "web-sys"
version = "0.3.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "weezl"
version = "0.1.7"

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ctrlc = "3.4.1"
femme = "2.2.1"
image = "0.23.0"
log = "0.4.20"
qrcode = "0.12.0"

View File

@ -1,3 +1,5 @@
use std::process::exit;
use qrcode::QrCode;
use image::{Luma, png::PngEncoder};
use urlencoding::decode;
@ -7,6 +9,8 @@ use warp::{Filter, reply::Response};
#[tokio::main]
async fn main() {
femme::with_level(log::LevelFilter::Info);
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");
@ -46,6 +50,15 @@ async fn main() {
})
);
log::info!("Running on http://0.0.0.0:{port}");
warp::serve(routes).run(([0, 0, 0, 0], port)).await;
}
let (addr, server) = warp::serve(routes)
.bind_with_graceful_shutdown(
([0, 0, 0, 0], port),
async move {
tokio::signal::ctrl_c().await.map_err(|err| format!("Failed to listen to Ctrl-C: {}", err.to_string())).unwrap()
}
);
log::info!("Running on http://{}", addr);
server.await;
println!(); // print a new line before exiting
}