From b297785dd5117dd0923f95ea8758b16b4f0ccc5f Mon Sep 17 00:00:00 2001 From: Compositr Date: Sat, 2 Nov 2024 17:33:44 +1100 Subject: [PATCH] feat: /info endpoint --- .config/cspell/dictionary.txt | 3 ++- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 38 +++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/.config/cspell/dictionary.txt b/.config/cspell/dictionary.txt index 5c871b5..e830248 100644 --- a/.config/cspell/dictionary.txt +++ b/.config/cspell/dictionary.txt @@ -1,4 +1,5 @@ luciders skia usvg -resvg \ No newline at end of file +resvg +indoc diff --git a/Cargo.lock b/Cargo.lock index ccd3047..85a1d9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,6 +185,12 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edcd27d72f2f071c64249075f42e205ff93c9a4c5f6c6da53e79ed9f9832c285" +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "kurbo" version = "0.11.1" @@ -218,6 +224,7 @@ name = "luciders" version = "0.1.0" dependencies = [ "ctrlc", + "indoc", "regex", "resvg", ] diff --git a/Cargo.toml b/Cargo.toml index f438601..a2319a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,6 @@ edition = "2021" [dependencies] ctrlc = "3.4.5" +indoc = "2.0.5" regex = "1.11.1" resvg = "0.44.0" diff --git a/src/main.rs b/src/main.rs index 9bcd74b..6eff9c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use resvg::{ usvg, }; +use indoc::indoc; + mod handlers; mod http; mod icons; @@ -44,6 +46,35 @@ fn main() { Response::new(req, 200, Body::Static("OK - luciders is running")) }); + const INFO: &str = indoc!( + " + luciders - Lucide SVG icons server + + GET /info - Get server information + GET /icons/[icon].svg - Get an icon in SVG format (equivalent to fetching them directly from Lucide) + GET /icons/[icon].png - Get an icon rendered in PNG format + Query parameters: + scale - integer - Scale the icon (default: 1, min: 1, max: 100) + padding - integer - Add padding (px) around the icon (default: 0, min: 0, max: 100) + background - hex - Set the background color in hex w/o the hashtag (e.g. FFFFFF) (default: transparent) + discord_compatibility - boolean - Set padding to 8px for Discord compatibility. Typically for use in embed author icons as these have a circle clip applied by Discord. Overrides padding if set. (default: false) + cache_key - string - Technically this can have any name. The server does not interpret this at all. Useful for cache busting. (default: none) + Example: + /icons/apple.png?scale=2&background=FF0000&discord_compatibility + + + *** mantained with ❤️ by Compositr (Jim) *** + " + ); + + handlers.add_handler("/info", |req| { + if req.method != "GET" { + return Response::new(req, 405, Body::Static("Method Not Allowed")); + } + + Response::new(req, 200, Body::Static(INFO)) + }); + handlers.add_handler("/icons/[icon].svg", { let icons = Rc::clone(&icons_rc); move |req| { @@ -142,6 +173,13 @@ fn main() { None => {} } + match req.url.query.get("discord_compatibility") { + Some(_) => { + padding = 8; + } + None => {} + } + let mut background = Color::TRANSPARENT; match req.url.query.get("background") { Some(background_str) => {