diff options
| author | Fuwn <[email protected]> | 2022-04-14 19:33:12 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-14 19:33:12 -0700 |
| commit | beef46abfb8d2cbb215037d2efa317635dffd746 (patch) | |
| tree | 9af55949d3e2048802121185bc1b2abae1926694 /src/main.rs | |
| parent | refactor(macros): use success! in mount_page! (diff) | |
| download | locus-beef46abfb8d2cbb215037d2efa317635dffd746.tar.xz locus-beef46abfb8d2cbb215037d2efa317635dffd746.zip | |
feat(main): add a search engine!
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 38 |
1 files changed, 38 insertions, 0 deletions
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<dyn std::error::Error>> { }), ); + 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::<Vec<_>>() + .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 } |