neon_arch commited on
Commit
38ba4bd
1 Parent(s): 64c4d2c

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

Browse files
Files changed (1) hide show
  1. src/server/routes/search.rs +30 -37
src/server/routes/search.rs CHANGED
@@ -6,13 +6,12 @@ use crate::{
6
  handler::paths::{file_path, FileType},
7
  models::{
8
  aggregation_models::SearchResults,
9
- engine_models::EngineHandler,
10
  server_models::{Cookie, SearchParams},
11
  },
12
  results::aggregator::aggregate,
13
  };
14
  use actix_web::{get, web, HttpRequest, HttpResponse};
15
- use handlebars::Handlebars;
16
  use regex::Regex;
17
  use std::{
18
  fs::File,
@@ -20,19 +19,6 @@ use std::{
20
  };
21
  use tokio::join;
22
 
23
- /// Handles the route of any other accessed route/page which is not provided by the
24
- /// website essentially the 404 error page.
25
- pub async fn not_found(
26
- hbs: web::Data<Handlebars<'_>>,
27
- config: web::Data<Config>,
28
- ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
29
- let page_content: String = hbs.render("404", &config.style)?;
30
-
31
- Ok(HttpResponse::Ok()
32
- .content_type("text/html; charset=utf-8")
33
- .body(page_content))
34
- }
35
-
36
  /// Handles the route of search page of the `websurfx` meta search engine website and it takes
37
  /// two search url parameters `q` and `page` where `page` parameter is optional.
38
  ///
@@ -49,7 +35,6 @@ pub async fn not_found(
49
  /// ```
50
  #[get("/search")]
51
  pub async fn search(
52
- hbs: web::Data<Handlebars<'_>>,
53
  req: HttpRequest,
54
  config: web::Data<Config>,
55
  cache: web::Data<SharedCache>,
@@ -58,7 +43,7 @@ pub async fn search(
58
  match &params.q {
59
  Some(query) => {
60
  if query.trim().is_empty() {
61
- return Ok(HttpResponse::Found()
62
  .insert_header(("location", "/"))
63
  .finish());
64
  }
@@ -112,10 +97,17 @@ pub async fn search(
112
  )
113
  );
114
 
115
- let page_content: String = hbs.render("search", &results?)?;
116
- Ok(HttpResponse::Ok().body(page_content))
 
 
 
 
 
 
 
117
  }
118
- None => Ok(HttpResponse::Found()
119
  .insert_header(("location", "/"))
120
  .finish()),
121
  }
@@ -150,7 +142,7 @@ async fn results(
150
  // check if fetched cache results was indeed fetched or it was an error and if so
151
  // handle the data accordingly.
152
  match cached_results {
153
- Ok(results) => Ok(results),
154
  Err(_) => {
155
  let mut safe_search_level: u8 = match config.safe_search {
156
  3..=4 => config.safe_search,
@@ -171,8 +163,6 @@ async fn results(
171
 
172
  if _flag {
173
  results.set_disallowed();
174
- results.add_style(&config.style);
175
- results.set_page_query(query);
176
  cache.cache_results(&results, &url).await?;
177
  results.set_safe_search_level(safe_search_level);
178
  return Ok(results);
@@ -221,23 +211,27 @@ async fn results(
221
  true => {
222
  let mut search_results = SearchResults::default();
223
  search_results.set_no_engines_selected();
224
- search_results.set_page_query(query);
225
  search_results
226
  }
227
  }
228
  }
229
- None => {
230
- aggregate(
231
- query,
232
- page,
233
- config.aggregator.random_delay,
234
- config.debug,
235
- &config.upstream_search_engines,
236
- config.request_timeout,
237
- safe_search_level,
238
- )
239
- .await?
240
- }
 
 
 
 
 
241
  };
242
  if results.engine_errors_info().is_empty()
243
  && results.results().is_empty()
@@ -245,7 +239,6 @@ async fn results(
245
  {
246
  results.set_filtered();
247
  }
248
- results.add_style(&config.style);
249
  cache
250
  .cache_results(&results, &(format!("{url}{safe_search_level}")))
251
  .await?;
 
6
  handler::paths::{file_path, FileType},
7
  models::{
8
  aggregation_models::SearchResults,
9
+ engine_models::{EngineError, EngineHandler},
10
  server_models::{Cookie, SearchParams},
11
  },
12
  results::aggregator::aggregate,
13
  };
14
  use actix_web::{get, web, HttpRequest, HttpResponse};
 
15
  use regex::Regex;
16
  use std::{
17
  fs::File,
 
19
  };
20
  use tokio::join;
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  /// Handles the route of search page of the `websurfx` meta search engine website and it takes
23
  /// two search url parameters `q` and `page` where `page` parameter is optional.
24
  ///
 
35
  /// ```
36
  #[get("/search")]
37
  pub async fn search(
 
38
  req: HttpRequest,
39
  config: web::Data<Config>,
40
  cache: web::Data<SharedCache>,
 
43
  match &params.q {
44
  Some(query) => {
45
  if query.trim().is_empty() {
46
+ return Ok(HttpResponse::TemporaryRedirect()
47
  .insert_header(("location", "/"))
48
  .finish());
49
  }
 
97
  )
98
  );
99
 
100
+ Ok(HttpResponse::Ok().body(
101
+ crate::templates::views::search::search(
102
+ &config.style.colorscheme,
103
+ &config.style.theme,
104
+ query,
105
+ &results?,
106
+ )
107
+ .0,
108
+ ))
109
  }
110
+ None => Ok(HttpResponse::TemporaryRedirect()
111
  .insert_header(("location", "/"))
112
  .finish()),
113
  }
 
142
  // check if fetched cache results was indeed fetched or it was an error and if so
143
  // handle the data accordingly.
144
  match cached_results {
145
+ Ok(results) => Ok(dbg!(results.clone())),
146
  Err(_) => {
147
  let mut safe_search_level: u8 = match config.safe_search {
148
  3..=4 => config.safe_search,
 
163
 
164
  if _flag {
165
  results.set_disallowed();
 
 
166
  cache.cache_results(&results, &url).await?;
167
  results.set_safe_search_level(safe_search_level);
168
  return Ok(results);
 
211
  true => {
212
  let mut search_results = SearchResults::default();
213
  search_results.set_no_engines_selected();
 
214
  search_results
215
  }
216
  }
217
  }
218
+ None => aggregate(
219
+ query,
220
+ page,
221
+ config.aggregator.random_delay,
222
+ config.debug,
223
+ &config
224
+ .upstream_search_engines
225
+ .clone()
226
+ .into_iter()
227
+ .filter_map(|(key, value)| value.then_some(key))
228
+ .map(|engine| EngineHandler::new(&engine))
229
+ .collect::<Result<Vec<EngineHandler>, error_stack::Report<EngineError>>>(
230
+ )?,
231
+ config.request_timeout,
232
+ safe_search_level,
233
+ )
234
+ .await?,
235
  };
236
  if results.engine_errors_info().is_empty()
237
  && results.results().is_empty()
 
239
  {
240
  results.set_filtered();
241
  }
 
242
  cache
243
  .cache_results(&results, &(format!("{url}{safe_search_level}")))
244
  .await?;