miniqr/src/main.rs

63 lines
2.4 KiB
Rust
Raw Normal View History

2023-10-09 17:02:45 +02:00
use qrcode::QrCode;
use image::{Luma, png::PngEncoder};
2023-10-09 17:12:11 +02:00
use urlencoding::decode;
2023-10-09 17:02:45 +02:00
use warp::{Filter, reply::Response};
2023-10-09 16:40:00 +02:00
#[tokio::main]
async fn main() {
2023-10-09 17:21:52 +02:00
femme::with_level(log::LevelFilter::Info);
2023-10-09 17:21:52 +02:00
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);
2023-10-09 16:40:00 +02:00
// Match any request and return hello world!
let routes =
warp::path::end()
.map(|| "Hello, World!")
.or(
warp::path!(String)
.and(
warp::get()
)
2023-10-09 17:12:11 +02:00
.map(|x: String| {
let text = decode(x.as_str()).unwrap().to_string();
let code = QrCode::with_error_correction_level(text, qrcode::EcLevel::M).unwrap();
let img = code
.render::<Luma<u8>>()
.quiet_zone(false)
.module_dimensions(1, 1)
.build();
2023-10-09 17:02:45 +02:00
let mut out = Vec::<u8>::new();
let enc = PngEncoder::new(&mut out);
let (width, height) = (img.width(), img.height());
enc.encode(&img.into_raw(), width, height, image::ColorType::L8).unwrap();
let mut res = Response::new(out.into());
res.headers_mut().insert("Content-Type", warp::http::HeaderValue::from_static("image/png"));
2023-10-09 17:15:34 +02:00
res.headers_mut().insert("Date", warp::http::HeaderValue::from_static("Sun, 01 Jan 1984 00:00:00 GMT"));
2023-10-09 17:02:45 +02:00
res
})
2023-10-09 16:40:00 +02:00
);
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
}