0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-21 21:48:21 -05:00

Moved parsing of cookie_value, config to cache_key

This commit is contained in:
ddotthomas 2024-01-02 16:26:04 -07:00
parent 36e2ac93be
commit 86b0d3d6c9

View File

@ -6,7 +6,7 @@ use crate::{
handler::{file_path, FileType}, handler::{file_path, FileType},
models::{ models::{
aggregation_models::SearchResults, aggregation_models::SearchResults,
engine_models::{EngineError, EngineHandler}, engine_models::EngineHandler,
server_models::{Cookie, SearchParams}, server_models::{Cookie, SearchParams},
}, },
results::aggregator::aggregate, results::aggregator::aggregate,
@ -126,27 +126,27 @@ async fn results(
config.binding_ip, config.port, query, page, safe_search_level config.binding_ip, config.port, query, page, safe_search_level
); );
let mut cookie_engines: Vec<String> = vec![];
let mut config_engines: Vec<String> = config
.upstream_search_engines
.iter()
.filter_map(|(engine, enabled)| enabled.then_some(engine.clone()))
.collect();
config_engines.sort();
// Modify the cache key adding each enabled search engine to the string // Modify the cache key adding each enabled search engine to the string
if let Some(cookie_value) = &cookie_value { if let Some(cookie_value) = &cookie_value {
let mut engines: Vec<String> = cookie_value cookie_engines = cookie_value
.engines .engines
.iter() .iter()
.map(|s| String::from(*s)) .map(|s| String::from(*s))
.collect::<Vec<String>>(); .collect::<Vec<String>>();
// We sort the list of engine so the cache keys will match between users. The cookie's list of engines is unordered. // We sort the list of engine so the cache keys will match between users. The cookie's list of engines is unordered.
engines.sort(); cookie_engines.sort();
cache_key = format!("{}{}", cache_key, engines.join(",")); cache_key = format!("{}{}", cache_key, cookie_engines.join(","));
} else { } else {
let mut engines: Vec<String> = config cache_key = format!("{}{}", cache_key, config_engines.join(","));
.upstream_search_engines
.iter()
.filter(|map| *map.1)
.map(|map| String::from(map.0))
.collect();
engines.sort();
cache_key = format!("{}{}", cache_key, engines.join(","));
} }
// fetch the cached results json. // fetch the cached results json.
@ -175,9 +175,10 @@ async fn results(
// parse the non-empty cookie and grab the user selected engines from the // parse the non-empty cookie and grab the user selected engines from the
// UI and use that. // UI and use that.
let mut results: SearchResults = match cookie_value { let mut results: SearchResults = match cookie_value {
Some(cookie_value) => { // If the cookie was used before
let engines: Vec<EngineHandler> = cookie_value Some(_) => {
.engines // Use the cookie_engines Strings from before to create the EngineHandlers
let engines: Vec<EngineHandler> = cookie_engines
.iter() .iter()
.filter_map(|name| EngineHandler::new(name).ok()) .filter_map(|name| EngineHandler::new(name).ok())
.collect(); .collect();
@ -202,23 +203,22 @@ async fn results(
} }
} }
} }
None => aggregate( // Otherwise, use the config_engines to create the EngineHandlers
query, None => {
page, aggregate(
config.aggregator.random_delay, query,
config.debug, page,
&config config.aggregator.random_delay,
.upstream_search_engines config.debug,
.clone() &config_engines
.into_iter() .into_iter()
.filter_map(|(key, value)| value.then_some(key)) .filter_map(|engine| EngineHandler::new(&engine).ok())
.map(|engine| EngineHandler::new(&engine)) .collect::<Vec<EngineHandler>>(),
.collect::<Result<Vec<EngineHandler>, error_stack::Report<EngineError>>>( config.request_timeout,
)?, safe_search_level,
config.request_timeout, )
safe_search_level, .await?
) }
.await?,
}; };
if results.engine_errors_info().is_empty() if results.engine_errors_info().is_empty()
&& results.results().is_empty() && results.results().is_empty()