summaryrefslogtreecommitdiff
path: root/angelsharkd/src/routes/extensions/simple_search.rs
diff options
context:
space:
mode:
Diffstat (limited to 'angelsharkd/src/routes/extensions/simple_search.rs')
-rw-r--r--angelsharkd/src/routes/extensions/simple_search.rs54
1 files changed, 48 insertions, 6 deletions
diff --git a/angelsharkd/src/routes/extensions/simple_search.rs b/angelsharkd/src/routes/extensions/simple_search.rs
index 454dba3..58f6e52 100644
--- a/angelsharkd/src/routes/extensions/simple_search.rs
+++ b/angelsharkd/src/routes/extensions/simple_search.rs
@@ -1,14 +1,56 @@
-use crate::config::Config;
+use crate::{config::Config, routes::with_runner};
+use cached::proc_macro::cached;
+use libangelshark::AcmRunner;
use std::convert::Infallible;
-use warp::{path, post, Filter, Rejection, Reply};
+use warp::{
+ body::{content_length_limit, json},
+ get, path, post, Filter, Rejection, Reply,
+};
const CMD_LIST_EXT: &str = "list extension-type";
const CMD_LIST_STAT: &str = "list station";
-pub fn filter() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
- path("search").and(post()).and_then(handle_simple_search)
+type Terms = Vec<String>;
+
+pub fn search(config: &Config) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+ let runner = config.runner.clone();
+ // TODO: anti-caching headers on resp?
+
+ path("search")
+ .and(post())
+ .and(content_length_limit(1024 * 16))
+ .and(json::<Terms>())
+ .and(with_runner(runner))
+ .and_then(handle_simple_search)
}
-async fn handle_simple_search() -> Result<impl Reply, Infallible> {
- Ok("Search!")
+pub fn refresh() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+ path!("search" / "refresh")
+ .and(get())
+ .and_then(handle_refresh)
+}
+
+async fn handle_refresh() -> Result<impl Reply, Infallible> {
+ fetch(String::from("test"), true);
+ Ok("")
+}
+
+async fn handle_simple_search(terms: Terms, runner: AcmRunner) -> Result<impl Reply, Infallible> {
+ Ok(fetch(terms[0].clone(), false))
+}
+
+#[cached]
+fn fetch(param: String, refresh: bool) -> String {
+ if refresh {
+ let param = param.clone();
+ tokio::spawn(async move {
+ std::thread::sleep_ms(10000);
+ let mut handle = FETCH.lock().unwrap();
+ handle.cache_clear();
+ handle.cache_set((param.clone(), false), format!("blargh {}", param.clone()));
+ eprintln!("evicted");
+ });
+ }
+
+ param
}