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

♻️ refactor(routes): serve the new maud file for search page and remove unwanted functions and pass values to the maud html template directly (#302)

This commit is contained in:
neon_arch 2023-11-17 22:16:17 +03:00
parent 64c4d2c23a
commit 38ba4bd6cb

View File

@ -6,13 +6,12 @@ use crate::{
handler::paths::{file_path, FileType}, handler::paths::{file_path, FileType},
models::{ models::{
aggregation_models::SearchResults, aggregation_models::SearchResults,
engine_models::EngineHandler, engine_models::{EngineError, EngineHandler},
server_models::{Cookie, SearchParams}, server_models::{Cookie, SearchParams},
}, },
results::aggregator::aggregate, results::aggregator::aggregate,
}; };
use actix_web::{get, web, HttpRequest, HttpResponse}; use actix_web::{get, web, HttpRequest, HttpResponse};
use handlebars::Handlebars;
use regex::Regex; use regex::Regex;
use std::{ use std::{
fs::File, fs::File,
@ -20,19 +19,6 @@ use std::{
}; };
use tokio::join; use tokio::join;
/// Handles the route of any other accessed route/page which is not provided by the
/// website essentially the 404 error page.
pub async fn not_found(
hbs: web::Data<Handlebars<'_>>,
config: web::Data<Config>,
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
let page_content: String = hbs.render("404", &config.style)?;
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(page_content))
}
/// Handles the route of search page of the `websurfx` meta search engine website and it takes /// Handles the route of search page of the `websurfx` meta search engine website and it takes
/// two search url parameters `q` and `page` where `page` parameter is optional. /// two search url parameters `q` and `page` where `page` parameter is optional.
/// ///
@ -49,7 +35,6 @@ pub async fn not_found(
/// ``` /// ```
#[get("/search")] #[get("/search")]
pub async fn search( pub async fn search(
hbs: web::Data<Handlebars<'_>>,
req: HttpRequest, req: HttpRequest,
config: web::Data<Config>, config: web::Data<Config>,
cache: web::Data<SharedCache>, cache: web::Data<SharedCache>,
@ -58,7 +43,7 @@ pub async fn search(
match &params.q { match &params.q {
Some(query) => { Some(query) => {
if query.trim().is_empty() { if query.trim().is_empty() {
return Ok(HttpResponse::Found() return Ok(HttpResponse::TemporaryRedirect()
.insert_header(("location", "/")) .insert_header(("location", "/"))
.finish()); .finish());
} }
@ -112,10 +97,17 @@ pub async fn search(
) )
); );
let page_content: String = hbs.render("search", &results?)?; Ok(HttpResponse::Ok().body(
Ok(HttpResponse::Ok().body(page_content)) crate::templates::views::search::search(
&config.style.colorscheme,
&config.style.theme,
query,
&results?,
)
.0,
))
} }
None => Ok(HttpResponse::Found() None => Ok(HttpResponse::TemporaryRedirect()
.insert_header(("location", "/")) .insert_header(("location", "/"))
.finish()), .finish()),
} }
@ -150,7 +142,7 @@ async fn results(
// check if fetched cache results was indeed fetched or it was an error and if so // check if fetched cache results was indeed fetched or it was an error and if so
// handle the data accordingly. // handle the data accordingly.
match cached_results { match cached_results {
Ok(results) => Ok(results), Ok(results) => Ok(dbg!(results.clone())),
Err(_) => { Err(_) => {
let mut safe_search_level: u8 = match config.safe_search { let mut safe_search_level: u8 = match config.safe_search {
3..=4 => config.safe_search, 3..=4 => config.safe_search,
@ -171,8 +163,6 @@ async fn results(
if _flag { if _flag {
results.set_disallowed(); results.set_disallowed();
results.add_style(&config.style);
results.set_page_query(query);
cache.cache_results(&results, &url).await?; cache.cache_results(&results, &url).await?;
results.set_safe_search_level(safe_search_level); results.set_safe_search_level(safe_search_level);
return Ok(results); return Ok(results);
@ -221,23 +211,27 @@ async fn results(
true => { true => {
let mut search_results = SearchResults::default(); let mut search_results = SearchResults::default();
search_results.set_no_engines_selected(); search_results.set_no_engines_selected();
search_results.set_page_query(query);
search_results search_results
} }
} }
} }
None => { None => aggregate(
aggregate( query,
query, page,
page, config.aggregator.random_delay,
config.aggregator.random_delay, config.debug,
config.debug, &config
&config.upstream_search_engines, .upstream_search_engines
config.request_timeout, .clone()
safe_search_level, .into_iter()
) .filter_map(|(key, value)| value.then_some(key))
.await? .map(|engine| EngineHandler::new(&engine))
} .collect::<Result<Vec<EngineHandler>, error_stack::Report<EngineError>>>(
)?,
config.request_timeout,
safe_search_level,
)
.await?,
}; };
if results.engine_errors_info().is_empty() if results.engine_errors_info().is_empty()
&& results.results().is_empty() && results.results().is_empty()
@ -245,7 +239,6 @@ async fn results(
{ {
results.set_filtered(); results.set_filtered();
} }
results.add_style(&config.style);
cache cache
.cache_results(&results, &(format!("{url}{safe_search_level}"))) .cache_results(&results, &(format!("{url}{safe_search_level}")))
.await?; .await?;