♻️ Clean some code, fix bug (#396)

* ♻️ Improve src/handler

Removes unnecessary submoduling & adjusts some weird code

* ♻️ Cleaner code

* 🐛 Fixed issue where code would overflow if page=0
This commit is contained in:
víctor 2023-11-27 07:45:28 +01:00 committed by GitHub
parent 7e1a80dc7e
commit 5e2669b6de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 147 additions and 179 deletions

View file

@ -3,7 +3,7 @@
use crate::{
cache::cacher::SharedCache,
config::parser::Config,
handler::paths::{file_path, FileType},
handler::{file_path, FileType},
models::{
aggregation_models::SearchResults,
engine_models::{EngineError, EngineHandler},
@ -47,54 +47,25 @@ pub async fn search(
.insert_header(("location", "/"))
.finish());
}
let page = match &params.page {
Some(page) => *page,
None => 1,
};
let (_, results, _) = join!(
let get_results = |page| {
results(
format!(
"http://{}:{}/search?q={}&page={}&safesearch=",
config.binding_ip,
config.port,
query,
page - 1,
),
&config,
&cache,
query,
page - 1,
req.clone(),
&params.safesearch
),
results(
format!(
"http://{}:{}/search?q={}&page={}&safesearch=",
config.binding_ip, config.port, query, page
),
&config,
&cache,
query,
page,
req.clone(),
&params.safesearch
),
results(
format!(
"http://{}:{}/search?q={}&page={}&safesearch=",
config.binding_ip,
config.port,
query,
page + 1,
),
&config,
&cache,
query,
page + 1,
req.clone(),
&params.safesearch
&params.safesearch,
)
};
// .max(1) makes sure that the page > 0.
let page = params.page.unwrap_or(1).max(1);
let (_, results, _) = join!(
get_results(page - 1),
get_results(page),
get_results(page + 1)
);
Ok(HttpResponse::Ok().body(
@ -129,7 +100,6 @@ pub async fn search(
/// It returns the `SearchResults` struct if the search results could be successfully fetched from
/// the cache or from the upstream search engines otherwise it returns an appropriate error.
async fn results(
url: String,
config: &Config,
cache: &web::Data<SharedCache>,
query: &str,
@ -137,6 +107,14 @@ async fn results(
req: HttpRequest,
safe_search: &Option<u8>,
) -> Result<SearchResults, Box<dyn std::error::Error>> {
let url = format!(
"http://{}:{}/search?q={}&page={}&safesearch=",
config.binding_ip,
config.port,
query,
page - 1,
);
// fetch the cached results json.
let cached_results = cache.cached_json(&url).await;
// check if fetched cache results was indeed fetched or it was an error and if so
@ -157,11 +135,11 @@ async fn results(
if safe_search_level == 4 {
let mut results: SearchResults = SearchResults::default();
let mut _flag: bool =
is_match_from_filter_list(file_path(FileType::BlockList)?, query)?;
_flag = !is_match_from_filter_list(file_path(FileType::AllowList)?, query)?;
if _flag {
let flag: bool =
!is_match_from_filter_list(file_path(FileType::BlockList)?, query)?;
if flag {
results.set_disallowed();
cache.cache_results(&results, &url).await?;
results.set_safe_search_level(safe_search_level);
@ -264,14 +242,13 @@ fn is_match_from_filter_list(
file_path: &str,
query: &str,
) -> Result<bool, Box<dyn std::error::Error>> {
let mut flag = false;
let mut reader = BufReader::new(File::open(file_path)?);
for line in reader.by_ref().lines() {
let re = Regex::new(&line?)?;
if re.is_match(query) {
flag = true;
break;
return Ok(true);
}
}
Ok(flag)
Ok(false)
}