summaryrefslogtreecommitdiff
path: root/angelsharkd/src/routes
diff options
context:
space:
mode:
authorCarpenter, Adam (CORP) <adam.carpenter@adp.com>2021-11-29 13:52:11 -0500
committerCarpenter, Adam (CORP) <adam.carpenter@adp.com>2021-11-29 13:52:11 -0500
commitd65e36a4b6f2e28d25ed2996a94db17a7ae9abd9 (patch)
tree687a232a06cc6e5a5eda1fac77892c969eb66536 /angelsharkd/src/routes
parent91d8810402ae8e1173a3c064519c9f1b03e13224 (diff)
downloadaltruistic-angelshark-d65e36a4b6f2e28d25ed2996a94db17a7ae9abd9.tar.xz
altruistic-angelshark-d65e36a4b6f2e28d25ed2996a94db17a7ae9abd9.zip
refactor: static global and once_cell depends
Diffstat (limited to 'angelsharkd/src/routes')
-rw-r--r--angelsharkd/src/routes/extensions/mod.rs10
-rw-r--r--angelsharkd/src/routes/extensions/simple_search.rs43
2 files changed, 29 insertions, 24 deletions
diff --git a/angelsharkd/src/routes/extensions/mod.rs b/angelsharkd/src/routes/extensions/mod.rs
index 5628588..b1c5aa3 100644
--- a/angelsharkd/src/routes/extensions/mod.rs
+++ b/angelsharkd/src/routes/extensions/mod.rs
@@ -1,3 +1,5 @@
+use std::sync::{Arc, Mutex};
+
use crate::config::Config;
use warp::{path, Filter, Rejection, Reply};
@@ -13,9 +15,13 @@ pub fn filter(config: &Config) -> impl Filter<Extract = impl Reply, Error = Reje
let filters = default().or(default());
#[cfg(feature = "simple_search")]
+ let runner = config.runner.clone();
+ #[cfg(feature = "simple_search")]
+ let haystack = simple_search::Haystack::new();
+ #[cfg(feature = "simple_search")]
let filters = filters
- .or(simple_search::search(config))
- .or(simple_search::refresh(config));
+ .or(simple_search::search(haystack.clone()))
+ .or(simple_search::refresh(runner, haystack));
#[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 a92c47c..7b547d3 100644
--- a/angelsharkd/src/routes/extensions/simple_search.rs
+++ b/angelsharkd/src/routes/extensions/simple_search.rs
@@ -1,6 +1,4 @@
-use crate::{config::Config, routes::with_runner};
use libangelshark::AcmRunner;
-use once_cell::sync::Lazy;
use std::{
convert::Infallible,
sync::{Arc, Mutex},
@@ -10,55 +8,56 @@ use warp::{
get, path, post, Filter, Rejection, Reply,
};
-static HAYSTACK_CACHE: Lazy<Haystack> = Lazy::new(Haystack::new);
-
/// Collection of search terms
type Needle = 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?
+pub fn search(haystack: Haystack) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+ // TODO: discourage caching response thru headers
path("search")
.and(post())
.and(content_length_limit(1024 * 16))
- .and(json::<Needle>())
- .and(with_runner(runner))
- .and_then(handle_search)
+ .and(json())
+ .and_then(move |terms: Needle| handle_search(haystack.clone(), terms))
}
-pub fn refresh(config: &Config) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
- let runner = config.runner.clone();
+pub fn refresh(
+ runner: AcmRunner,
+ haystack: Haystack,
+) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
path!("search" / "refresh")
.and(get())
- .and(with_runner(runner))
- .and_then(handle_refresh)
+ .and_then(move || handle_refresh(haystack.clone(), runner.clone()))
}
-async fn handle_search(terms: Needle, runner: AcmRunner) -> Result<impl Reply, Infallible> {
- HAYSTACK_CACHE.search(Vec::new());
- Ok("")
+async fn handle_search(haystack: Haystack, needle: Needle) -> Result<impl Reply, Infallible> {
+ Ok(haystack.search(Vec::new()))
}
-async fn handle_refresh(runner: AcmRunner) -> Result<impl Reply, Infallible> {
- HAYSTACK_CACHE.refresh();
+async fn handle_refresh(haystack: Haystack, runner: AcmRunner) -> Result<impl Reply, Infallible> {
+ haystack.refresh();
Ok("Refresh scheduled")
}
/// A lazy-loaded, asynchronously-refreshed exension-type haystack cache.
-struct Haystack {
+#[derive(Clone)]
+pub struct Haystack {
inner: Arc<Mutex<String>>,
}
impl Haystack {
- fn new() -> Self {
+ pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(String::with_capacity(0))),
}
}
pub fn search(&self, needle: Needle) -> String {
- (*self.inner.lock().unwrap()).clone()
+ if let Ok(matches) = self.inner.lock() {
+ matches.clone()
+ } else {
+ String::from("stale")
+ }
}
pub fn refresh(&self) {