diff options
author | Carpenter, Adam (CORP) <adam.carpenter@adp.com> | 2021-11-26 15:58:13 -0500 |
---|---|---|
committer | Carpenter, Adam (CORP) <adam.carpenter@adp.com> | 2021-11-26 15:58:13 -0500 |
commit | 6dd3236069d5cbdd6cb946b408cd6efb7e91d0f9 (patch) | |
tree | 8cb01be763deaf682f63bd0f21ab9103d6286d80 /angelsharkd | |
parent | 5b0786905d400a8414dd0b0fe2e4d9ac784ae543 (diff) | |
download | altruistic-angelshark-6dd3236069d5cbdd6cb946b408cd6efb7e91d0f9.tar.xz altruistic-angelshark-6dd3236069d5cbdd6cb946b408cd6efb7e91d0f9.zip |
feat: impl lazy-refreshing cache for quick access to be used for haystack
Diffstat (limited to 'angelsharkd')
-rw-r--r-- | angelsharkd/Cargo.toml | 11 | ||||
-rw-r--r-- | angelsharkd/src/main.rs | 4 | ||||
-rw-r--r-- | angelsharkd/src/routes/extensions/mod.rs | 4 | ||||
-rw-r--r-- | angelsharkd/src/routes/extensions/simple_search.rs | 54 | ||||
-rw-r--r-- | angelsharkd/src/routes/mod.rs | 1 |
5 files changed, 60 insertions, 14 deletions
diff --git a/angelsharkd/Cargo.toml b/angelsharkd/Cargo.toml index 9ca459e..8e617e7 100644 --- a/angelsharkd/Cargo.toml +++ b/angelsharkd/Cargo.toml @@ -6,9 +6,8 @@ authors = ["Adam T. Carpenter <adam.carpenter@adp.com>"] description = "A HTTP interface into one or more Communication Managers" [features] -extensions = [] -simple_search = ["extensions"] -simple_deprov = ["extensions"] +simple_search = ["cached"] +simple_deprov = [] [dependencies.libangelshark] path = "../libangelshark" @@ -33,3 +32,9 @@ features = ["derive"] [dependencies.anyhow] version = "1" + +[dependencies.cached] +optional = true +version = "0.26" +default-features = false +features = ["proc_macro"] diff --git a/angelsharkd/src/main.rs b/angelsharkd/src/main.rs index fb075ab..790b641 100644 --- a/angelsharkd/src/main.rs +++ b/angelsharkd/src/main.rs @@ -30,6 +30,7 @@ async fn main() -> Result<()> { let routes = routes::index() .or(routes::ossi(&config)) + .or(routes::extensions::filter(&config)) .with(if config.debug_mode || config.origin == "*" { warp::cors() .allow_any_origin() @@ -41,9 +42,6 @@ async fn main() -> Result<()> { }) .with(warp::log("angelsharkd")); - #[cfg(feature = "extensions")] - let routes = routes.or(routes::extensions::filter(&config)); - // Create server with shutdown signal. let (addr, server) = warp::serve(routes).bind_with_graceful_shutdown(config.bind_addr, async { signal::ctrl_c() diff --git a/angelsharkd/src/routes/extensions/mod.rs b/angelsharkd/src/routes/extensions/mod.rs index 5dac1cf..defbca9 100644 --- a/angelsharkd/src/routes/extensions/mod.rs +++ b/angelsharkd/src/routes/extensions/mod.rs @@ -13,7 +13,9 @@ pub fn filter(config: &Config) -> impl Filter<Extract = impl Reply, Error = Reje let filters = default().or(default()); #[cfg(feature = "simple_search")] - let filters = filters.or(simple_search::filter()); + let filters = filters + .or(simple_search::search(config)) + .or(simple_search::refresh()); #[cfg(feature = "simple_deprov")] let filters = filters.or(simple_deprov::filter()); 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 } diff --git a/angelsharkd/src/routes/mod.rs b/angelsharkd/src/routes/mod.rs index 52373d6..cee0657 100644 --- a/angelsharkd/src/routes/mod.rs +++ b/angelsharkd/src/routes/mod.rs @@ -13,7 +13,6 @@ use warp::{ }; mod dtos; -#[cfg(feature = "extensions")] pub mod extensions; /// GET / -> Name and version # of app. |