neon_arch
commited on
Commit
·
fcfd112
1
Parent(s):
8c239e2
🛠️ fix: improve the documentation & move code in the correct files (#244)
Browse files- src/server/routes/search.rs +16 -69
src/server/routes/search.rs
CHANGED
|
@@ -4,43 +4,22 @@ use crate::{
|
|
| 4 |
cache::cacher::SharedCache,
|
| 5 |
config::parser::Config,
|
| 6 |
handler::paths::{file_path, FileType},
|
| 7 |
-
models::{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
results::aggregator::aggregate,
|
| 9 |
};
|
| 10 |
use actix_web::{get, web, HttpRequest, HttpResponse};
|
| 11 |
use handlebars::Handlebars;
|
| 12 |
use regex::Regex;
|
| 13 |
-
use serde::Deserialize;
|
| 14 |
use std::{
|
| 15 |
-
fs::
|
| 16 |
io::{BufRead, BufReader, Read},
|
| 17 |
};
|
| 18 |
use tokio::join;
|
| 19 |
|
| 20 |
-
/// A named struct which deserializes all the user provided search parameters and stores them.
|
| 21 |
-
#[derive(Deserialize)]
|
| 22 |
-
pub struct SearchParams {
|
| 23 |
-
/// It stores the search parameter option `q` (or query in simple words)
|
| 24 |
-
/// of the search url.
|
| 25 |
-
q: Option<String>,
|
| 26 |
-
/// It stores the search parameter `page` (or pageno in simple words)
|
| 27 |
-
/// of the search url.
|
| 28 |
-
page: Option<u32>,
|
| 29 |
-
/// It stores the search parameter `safesearch` (or safe search level in simple words) of the
|
| 30 |
-
/// search url.
|
| 31 |
-
safesearch: Option<u8>,
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
/// Handles the route of index page or main page of the `websurfx` meta search engine website.
|
| 35 |
-
#[get("/")]
|
| 36 |
-
pub async fn index(
|
| 37 |
-
hbs: web::Data<Handlebars<'_>>,
|
| 38 |
-
config: web::Data<Config>,
|
| 39 |
-
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
| 40 |
-
let page_content: String = hbs.render("index", &config.style).unwrap();
|
| 41 |
-
Ok(HttpResponse::Ok().body(page_content))
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
/// Handles the route of any other accessed route/page which is not provided by the
|
| 45 |
/// website essentially the 404 error page.
|
| 46 |
pub async fn not_found(
|
|
@@ -54,18 +33,6 @@ pub async fn not_found(
|
|
| 54 |
.body(page_content))
|
| 55 |
}
|
| 56 |
|
| 57 |
-
/// A named struct which is used to deserialize the cookies fetched from the client side.
|
| 58 |
-
#[allow(dead_code)]
|
| 59 |
-
#[derive(Deserialize)]
|
| 60 |
-
struct Cookie<'a> {
|
| 61 |
-
/// It stores the theme name used in the website.
|
| 62 |
-
theme: &'a str,
|
| 63 |
-
/// It stores the colorscheme name used for the website theme.
|
| 64 |
-
colorscheme: &'a str,
|
| 65 |
-
/// It stores the user selected upstream search engines selected from the UI.
|
| 66 |
-
engines: Vec<&'a str>,
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
/// Handles the route of search page of the `websurfx` meta search engine website and it takes
|
| 70 |
/// two search url parameters `q` and `page` where `page` parameter is optional.
|
| 71 |
///
|
|
@@ -264,6 +231,16 @@ async fn results(
|
|
| 264 |
|
| 265 |
/// A helper function which checks whether the search query contains any keywords which should be
|
| 266 |
/// disallowed/allowed based on the regex based rules present in the blocklist and allowlist files.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
fn is_match_from_filter_list(
|
| 268 |
file_path: &str,
|
| 269 |
query: &str,
|
|
@@ -279,33 +256,3 @@ fn is_match_from_filter_list(
|
|
| 279 |
}
|
| 280 |
Ok(flag)
|
| 281 |
}
|
| 282 |
-
|
| 283 |
-
/// Handles the route of robots.txt page of the `websurfx` meta search engine website.
|
| 284 |
-
#[get("/robots.txt")]
|
| 285 |
-
pub async fn robots_data(_req: HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
| 286 |
-
let page_content: String =
|
| 287 |
-
read_to_string(format!("{}/robots.txt", file_path(FileType::Theme)?))?;
|
| 288 |
-
Ok(HttpResponse::Ok()
|
| 289 |
-
.content_type("text/plain; charset=ascii")
|
| 290 |
-
.body(page_content))
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
/// Handles the route of about page of the `websurfx` meta search engine website.
|
| 294 |
-
#[get("/about")]
|
| 295 |
-
pub async fn about(
|
| 296 |
-
hbs: web::Data<Handlebars<'_>>,
|
| 297 |
-
config: web::Data<Config>,
|
| 298 |
-
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
| 299 |
-
let page_content: String = hbs.render("about", &config.style)?;
|
| 300 |
-
Ok(HttpResponse::Ok().body(page_content))
|
| 301 |
-
}
|
| 302 |
-
|
| 303 |
-
/// Handles the route of settings page of the `websurfx` meta search engine website.
|
| 304 |
-
#[get("/settings")]
|
| 305 |
-
pub async fn settings(
|
| 306 |
-
hbs: web::Data<Handlebars<'_>>,
|
| 307 |
-
config: web::Data<Config>,
|
| 308 |
-
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
| 309 |
-
let page_content: String = hbs.render("settings", &config.style)?;
|
| 310 |
-
Ok(HttpResponse::Ok().body(page_content))
|
| 311 |
-
}
|
|
|
|
| 4 |
cache::cacher::SharedCache,
|
| 5 |
config::parser::Config,
|
| 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,
|
| 19 |
io::{BufRead, BufReader, Read},
|
| 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(
|
|
|
|
| 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 |
///
|
|
|
|
| 231 |
|
| 232 |
/// A helper function which checks whether the search query contains any keywords which should be
|
| 233 |
/// disallowed/allowed based on the regex based rules present in the blocklist and allowlist files.
|
| 234 |
+
///
|
| 235 |
+
/// # Arguments
|
| 236 |
+
///
|
| 237 |
+
/// * `file_path` - It takes the file path of the list as the argument.
|
| 238 |
+
/// * `query` - It takes the search query to be checked against the list as an argument.
|
| 239 |
+
///
|
| 240 |
+
/// # Error
|
| 241 |
+
///
|
| 242 |
+
/// Returns a bool indicating whether the results were found in the list or not on success
|
| 243 |
+
/// otherwise returns a standard error type on a failure.
|
| 244 |
fn is_match_from_filter_list(
|
| 245 |
file_path: &str,
|
| 246 |
query: &str,
|
|
|
|
| 256 |
}
|
| 257 |
Ok(flag)
|
| 258 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|