From beef46abfb8d2cbb215037d2efa317635dffd746 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 14 Apr 2022 19:33:12 -0700 Subject: feat(main): add a search engine! --- src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 582b032..3875ac0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -233,5 +233,43 @@ async fn main() -> Result<(), Box> { }), ); + track_mount( + &mut router, + "/search", + "A search engine for this Gemini capsule", + Box::new(|context| { + let mut query_pairs = context.url.query_pairs(); + let mut response = + String::from("# SEARCH\n\n=> /search?action=go Search!"); + + if let Some(query) = query_pairs.next() { + if query.0 == "action" && query.1 == "go" { + return Response::Input( + "What would you like to search for?".to_string(), + ); + } + + let results = (*ROUTES.lock().unwrap()) + .iter() + .map(|(r, d)| format!("=> {} {}", r, d)) + .filter(|r| r.to_lowercase().contains(&query.0.to_string())) + .collect::>() + .join("\n"); + + response += &format!( + "\n\nYou searched for \"{}\"!\n\n## RESULTS\n\n{}", + query.0, + if results.is_empty() { + "There are no results for your query...".to_string() + } else { + results + }, + ); + } + + success!(response, context) + }), + ); + router.run().await } -- cgit v1.2.3