use qrcode::QrCode; use image::{Luma, png::PngEncoder}; use urlencoding::decode; 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() .map(|| "Hello, World!") .or( warp::path!(String) .and( warp::get() ) .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::>() .quiet_zone(false) .module_dimensions(1, 1) .build(); let mut out = Vec::::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")); res.headers_mut().insert("Date", warp::http::HeaderValue::from_static("Sun, 01 Jan 1984 00:00:00 GMT")); res }) ); log::info!("Running on http://0.0.0.0:{port}"); warp::serve(routes).run(([0, 0, 0, 0], port)).await; }