ddotthomas commited on
Commit
86b0d3d
1 Parent(s): 36e2ac9

Moved parsing of cookie_value, config to cache_key

Browse files
Files changed (1) hide show
  1. src/server/routes/search.rs +33 -33
src/server/routes/search.rs CHANGED
@@ -6,7 +6,7 @@ use crate::{
6
  handler::{file_path, FileType},
7
  models::{
8
  aggregation_models::SearchResults,
9
- engine_models::{EngineError, EngineHandler},
10
  server_models::{Cookie, SearchParams},
11
  },
12
  results::aggregator::aggregate,
@@ -126,27 +126,27 @@ async fn results(
126
  config.binding_ip, config.port, query, page, safe_search_level
127
  );
128
 
 
 
 
 
 
 
 
 
129
  // Modify the cache key adding each enabled search engine to the string
130
  if let Some(cookie_value) = &cookie_value {
131
- let mut engines: Vec<String> = cookie_value
132
  .engines
133
  .iter()
134
  .map(|s| String::from(*s))
135
  .collect::<Vec<String>>();
136
 
137
  // We sort the list of engine so the cache keys will match between users. The cookie's list of engines is unordered.
138
- engines.sort();
139
- cache_key = format!("{}{}", cache_key, engines.join(","));
140
  } else {
141
- let mut engines: Vec<String> = config
142
- .upstream_search_engines
143
- .iter()
144
- .filter(|map| *map.1)
145
- .map(|map| String::from(map.0))
146
- .collect();
147
-
148
- engines.sort();
149
- cache_key = format!("{}{}", cache_key, engines.join(","));
150
  }
151
 
152
  // fetch the cached results json.
@@ -175,9 +175,10 @@ async fn results(
175
  // parse the non-empty cookie and grab the user selected engines from the
176
  // UI and use that.
177
  let mut results: SearchResults = match cookie_value {
178
- Some(cookie_value) => {
179
- let engines: Vec<EngineHandler> = cookie_value
180
- .engines
 
181
  .iter()
182
  .filter_map(|name| EngineHandler::new(name).ok())
183
  .collect();
@@ -202,23 +203,22 @@ async fn results(
202
  }
203
  }
204
  }
205
- None => aggregate(
206
- query,
207
- page,
208
- config.aggregator.random_delay,
209
- config.debug,
210
- &config
211
- .upstream_search_engines
212
- .clone()
213
- .into_iter()
214
- .filter_map(|(key, value)| value.then_some(key))
215
- .map(|engine| EngineHandler::new(&engine))
216
- .collect::<Result<Vec<EngineHandler>, error_stack::Report<EngineError>>>(
217
- )?,
218
- config.request_timeout,
219
- safe_search_level,
220
- )
221
- .await?,
222
  };
223
  if results.engine_errors_info().is_empty()
224
  && results.results().is_empty()
 
6
  handler::{file_path, FileType},
7
  models::{
8
  aggregation_models::SearchResults,
9
+ engine_models::EngineHandler,
10
  server_models::{Cookie, SearchParams},
11
  },
12
  results::aggregator::aggregate,
 
126
  config.binding_ip, config.port, query, page, safe_search_level
127
  );
128
 
129
+ let mut cookie_engines: Vec<String> = vec![];
130
+ let mut config_engines: Vec<String> = config
131
+ .upstream_search_engines
132
+ .iter()
133
+ .filter_map(|(engine, enabled)| enabled.then_some(engine.clone()))
134
+ .collect();
135
+ config_engines.sort();
136
+
137
  // Modify the cache key adding each enabled search engine to the string
138
  if let Some(cookie_value) = &cookie_value {
139
+ cookie_engines = cookie_value
140
  .engines
141
  .iter()
142
  .map(|s| String::from(*s))
143
  .collect::<Vec<String>>();
144
 
145
  // We sort the list of engine so the cache keys will match between users. The cookie's list of engines is unordered.
146
+ cookie_engines.sort();
147
+ cache_key = format!("{}{}", cache_key, cookie_engines.join(","));
148
  } else {
149
+ cache_key = format!("{}{}", cache_key, config_engines.join(","));
 
 
 
 
 
 
 
 
150
  }
151
 
152
  // fetch the cached results json.
 
175
  // parse the non-empty cookie and grab the user selected engines from the
176
  // UI and use that.
177
  let mut results: SearchResults = match cookie_value {
178
+ // If the cookie was used before
179
+ Some(_) => {
180
+ // Use the cookie_engines Strings from before to create the EngineHandlers
181
+ let engines: Vec<EngineHandler> = cookie_engines
182
  .iter()
183
  .filter_map(|name| EngineHandler::new(name).ok())
184
  .collect();
 
203
  }
204
  }
205
  }
206
+ // Otherwise, use the config_engines to create the EngineHandlers
207
+ None => {
208
+ aggregate(
209
+ query,
210
+ page,
211
+ config.aggregator.random_delay,
212
+ config.debug,
213
+ &config_engines
214
+ .into_iter()
215
+ .filter_map(|engine| EngineHandler::new(&engine).ok())
216
+ .collect::<Vec<EngineHandler>>(),
217
+ config.request_timeout,
218
+ safe_search_level,
219
+ )
220
+ .await?
221
+ }
 
222
  };
223
  if results.engine_errors_info().is_empty()
224
  && results.results().is_empty()