miniqr/src/main.rs

42 lines
1.6 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() {
// 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
);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}