diff --git a/src/icons.rs b/src/icons.rs index 399bcea..d3c061e 100644 --- a/src/icons.rs +++ b/src/icons.rs @@ -53,7 +53,6 @@ impl Icons { pub fn get_icon(&self, icon: &str) -> Option> { let icon_path = format!("{}/{}.svg", self.icons_dir_path, icon); - println!("Reading icon: {}", icon_path); fs::read(icon_path).ok() } } diff --git a/src/main.rs b/src/main.rs index 34f84a4..9bcd74b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,8 +15,13 @@ mod icons; fn main() { println!("luciders starting..."); - let listener = - TcpListener::bind("[::]:7878").unwrap_or_else(|_| fatal("Failed to bind to address")); + let port = match std::env::var("PORT") { + Ok(port) => port, + Err(_) => "7878".to_string(), + }; + + let listener = TcpListener::bind(format!("[::]:{}", port)) + .unwrap_or_else(|_| fatal("Failed to bind to address")); ctrlc::set_handler(move || { println!("Shutting down..."); @@ -59,7 +64,10 @@ fn main() { let icon = match icons.get_icon(icon_name) { Some(icon) => icon, - None => return Response::new(req, 500, Body::Static("Failed to read icon")), + None => { + error("Failed to read icon"); + return Response::new(req, 500, Body::Static("Failed to read icon")); + } }; return Response::new(req, 200, Body::Bytes(icon, "image/svg+xml")); @@ -86,12 +94,16 @@ fn main() { let icon = match icons.get_icon(icon_name) { Some(icon) => icon, - None => return Response::new(req, 500, Body::Static("Failed to read icon")), + None => { + error("Failed to read icon"); + return Response::new(req, 500, Body::Static("Failed to read icon")) + }, }; let tree = match usvg::Tree::from_data(&icon, &usvg::Options::default()) { Ok(tree) => tree, - Err(_) => { + Err(e) => { + error(&format!("Failed to load icon into tree: {}", e)); return Response::new(req, 500, Body::Static("Failed to load icon into tree")) } }; @@ -107,9 +119,7 @@ fn main() { } scale = scale_val; } - Err(_) => { - return Response::new(req, 400, Body::Static("Invalid scale value. Scale value must be an integer > 0 and <= 100")); - } + Err(_) => return Response::new(req, 400, Body::Static("Invalid scale value. Scale value must be an integer > 0 and <= 100")) } } None => {} @@ -126,9 +136,7 @@ fn main() { padding = padding_val; } - Err(_) => { - return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100")); - } + Err(_) => return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100")) } } None => {} @@ -147,9 +155,7 @@ fn main() { let blue = (background_val & 0xFF) as u8; background = Color::from_rgba8(red, green, blue, 255); } - Err(_) => { - return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color")); - } + Err(_) => return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color")) } } None => {} @@ -159,7 +165,10 @@ fn main() { let mut pixmap = match Pixmap::new(length, length) { Some(pixmap) => pixmap, - None => return Response::new(req, 500, Body::Static("Failed to create pixmap")), + None => { + error("Failed to create pixmap"); + return Response::new(req, 500, Body::Static("Failed to create pixmap")) + }, }; let mut pixmap_mut = pixmap.as_mut(); @@ -173,7 +182,10 @@ fn main() { ); let png = match pixmap.encode_png() { Ok(png) => png, - Err(_) => return Response::new(req, 500, Body::Static("Failed to encode PNG")), + Err(e) => { + error(&format!("Failed to encode PNG: {}", e)); + return Response::new(req, 500, Body::Static("Failed to encode PNG")) + }, }; return Response::new(req, 200, Body::Bytes(png, "image/png")); @@ -181,6 +193,9 @@ fn main() { }); handlers.bind(listener); + + println!("luciders started on port {}", port); + println!("Serving {} icons", icons_rc.icons_len()) } fn fatal(msg: &'static str) -> ! { @@ -188,6 +203,6 @@ fn fatal(msg: &'static str) -> ! { process::exit(1); } -fn error(msg: &'static str) { +fn error(msg: &str) { eprintln!("[ERROR] {}", msg); }