finishing server side engine selection
This commit is contained in:
parent
e666316995
commit
0faf8c19a9
6 changed files with 32 additions and 27 deletions
|
@ -80,3 +80,18 @@ impl From<&Engines> for Vec<EngineHandler> {
|
||||||
v
|
v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Engines {
|
||||||
|
/// Returns a list of all engines
|
||||||
|
pub fn list(&self) -> Box<[&'static str]> {
|
||||||
|
Box::new([
|
||||||
|
"duckduckgo",
|
||||||
|
"searx",
|
||||||
|
"brave",
|
||||||
|
"startpage",
|
||||||
|
"librex",
|
||||||
|
"mojeek",
|
||||||
|
"bing",
|
||||||
|
])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
//! This module provides the error enum to handle different errors associated while requesting data from
|
//! This module provides the error enum to handle different errors associated while requesting data from
|
||||||
//! the upstream search engines with the search query provided by the user.
|
//! the upstream search engines with the search query provided by the user.
|
||||||
|
|
||||||
use crate::engines;
|
|
||||||
|
|
||||||
use super::aggregation_models::SearchResult;
|
use super::aggregation_models::SearchResult;
|
||||||
use error_stack::{Report, Result, ResultExt};
|
use error_stack::{Result, ResultExt};
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use std::{fmt, sync::Arc};
|
use std::{fmt, sync::Arc};
|
||||||
|
|
||||||
|
@ -152,6 +150,7 @@ pub trait SearchEngine: Sync + Send {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A named struct which stores the engine struct with the name of the associated engine.
|
/// A named struct which stores the engine struct with the name of the associated engine.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct EngineHandler {
|
pub struct EngineHandler {
|
||||||
/// It stores the engine struct wrapped in a box smart pointer as the engine struct implements
|
/// It stores the engine struct wrapped in a box smart pointer as the engine struct implements
|
||||||
/// the `SearchEngine` trait.
|
/// the `SearchEngine` trait.
|
||||||
|
@ -160,15 +159,6 @@ pub struct EngineHandler {
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for EngineHandler {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
engine: self.engine.clone(),
|
|
||||||
name: self.name.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EngineHandler {
|
impl EngineHandler {
|
||||||
/// Parses an engine name into an engine handler.
|
/// Parses an engine name into an engine handler.
|
||||||
///
|
///
|
||||||
|
|
|
@ -70,7 +70,12 @@ pub async fn settings(
|
||||||
&config.style.colorscheme,
|
&config.style.colorscheme,
|
||||||
&config.style.theme,
|
&config.style.theme,
|
||||||
&config.style.animation,
|
&config.style.animation,
|
||||||
//&config.upstream_search_engines,
|
&config
|
||||||
|
.upstream_search_engines
|
||||||
|
.list()
|
||||||
|
.iter()
|
||||||
|
.map(|n| (*n, true))
|
||||||
|
.collect(),
|
||||||
)?
|
)?
|
||||||
.0,
|
.0,
|
||||||
))
|
))
|
||||||
|
|
|
@ -5,14 +5,12 @@ use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
engines::Engines,
|
engines::Engines,
|
||||||
models::{
|
models::{
|
||||||
aggregation_models::SearchResults,
|
aggregation_models::SearchResults, engine_models::EngineHandler,
|
||||||
engine_models::EngineHandler,
|
server_models::SearchParams,
|
||||||
server_models::{self, SearchParams},
|
|
||||||
},
|
},
|
||||||
results::aggregator::aggregate,
|
results::aggregator::aggregate,
|
||||||
};
|
};
|
||||||
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
||||||
use std::borrow::Cow;
|
|
||||||
use tokio::join;
|
use tokio::join;
|
||||||
|
|
||||||
/// Handles the route of search page of the `crabbysearch` meta search engine website and it takes
|
/// Handles the route of search page of the `crabbysearch` meta search engine website and it takes
|
||||||
|
@ -50,7 +48,8 @@ pub async fn search(
|
||||||
// Get search settings using the user's cookie or from the server's config
|
// Get search settings using the user's cookie or from the server's config
|
||||||
let search_settings: crate::engines::Engines = cookie
|
let search_settings: crate::engines::Engines = cookie
|
||||||
.and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
|
.and_then(|cookie_value| serde_json::from_str(cookie_value.value()).ok())
|
||||||
.unwrap();
|
.inspect(|e| log::info!("{e:?}"))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
// Closure wrapping the results function capturing local references
|
// Closure wrapping the results function capturing local references
|
||||||
let get_results = |page| results(config.clone(), cache.clone(), query, page, &search_settings);
|
let get_results = |page| results(config.clone(), cache.clone(), query, page, &search_settings);
|
||||||
|
@ -149,7 +148,7 @@ async fn results(
|
||||||
// default selected upstream search engines from the config file otherwise
|
// default selected upstream search engines from the config file otherwise
|
||||||
// parse the non-empty cookie and grab the user selected engines from the
|
// parse the non-empty cookie and grab the user selected engines from the
|
||||||
// UI and use that.
|
// UI and use that.
|
||||||
let mut results: SearchResults = match true {
|
let mut results: SearchResults = match false {
|
||||||
false => aggregate(query, page, config, &Vec::<EngineHandler>::from(upstream)).await?,
|
false => aggregate(query, page, config, &Vec::<EngineHandler>::from(upstream)).await?,
|
||||||
true => {
|
true => {
|
||||||
let mut search_results = SearchResults::default();
|
let mut search_results = SearchResults::default();
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
//! A module that handles the engines tab for setting page view in the `crabbysearch` frontend.
|
//! A module that handles the engines tab for setting page view in the `crabbysearch` frontend.
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
|
|
||||||
/// A functions that handles the html code for the engines tab for the settings page for the search page.
|
/// A functions that handles the html code for the engines tab for the settings page for the search page.
|
||||||
|
@ -14,7 +12,7 @@ use maud::{html, Markup};
|
||||||
/// # Returns
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// It returns the compiled html markup code for the engines tab.
|
/// It returns the compiled html markup code for the engines tab.
|
||||||
pub fn engines(engine_names: &HashMap<String, bool>) -> Markup {
|
pub fn engines(engine_names: &Vec<(&str, bool)>) -> Markup {
|
||||||
html!(
|
html!(
|
||||||
div class="engines tab"{
|
div class="engines tab"{
|
||||||
h1{"Engines"}
|
h1{"Engines"}
|
||||||
|
@ -26,7 +24,7 @@ pub fn engines(engine_names: &HashMap<String, bool>) -> Markup {
|
||||||
// Checks whether all the engines are selected or not if they are then the
|
// Checks whether all the engines are selected or not if they are then the
|
||||||
// checked `select_all` button is rendered otherwise the unchecked version
|
// checked `select_all` button is rendered otherwise the unchecked version
|
||||||
// is rendered.
|
// is rendered.
|
||||||
@if engine_names.values().all(|selected| *selected){
|
@if engine_names.iter().all(|selected| selected.1){
|
||||||
.toggle_btn{
|
.toggle_btn{
|
||||||
label class="switch"{
|
label class="switch"{
|
||||||
input type="checkbox" class="select_all" onchange="toggleAllSelection()" checked;
|
input type="checkbox" class="select_all" onchange="toggleAllSelection()" checked;
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
//! A module that handles the view for the settings page in the `crabbysearch` frontend.
|
//! A module that handles the view for the settings page in the `crabbysearch` frontend.
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use maud::{html, Markup};
|
use maud::{html, Markup};
|
||||||
|
|
||||||
use crate::templates::partials::{
|
use crate::templates::partials::{
|
||||||
|
@ -28,7 +26,7 @@ pub fn settings(
|
||||||
colorscheme: &str,
|
colorscheme: &str,
|
||||||
theme: &str,
|
theme: &str,
|
||||||
animation: &Option<String>,
|
animation: &Option<String>,
|
||||||
//engine_names: &HashMap<String, bool>,
|
engine_names: &Vec<(&'static str, bool)>,
|
||||||
) -> Result<Markup, Box<dyn std::error::Error>> {
|
) -> Result<Markup, Box<dyn std::error::Error>> {
|
||||||
Ok(html!(
|
Ok(html!(
|
||||||
(header(colorscheme, theme, animation))
|
(header(colorscheme, theme, animation))
|
||||||
|
@ -44,7 +42,7 @@ pub fn settings(
|
||||||
}
|
}
|
||||||
.main_container{
|
.main_container{
|
||||||
(user_interface(theme, colorscheme, animation)?)
|
(user_interface(theme, colorscheme, animation)?)
|
||||||
//(engines(engine_names))
|
(engines(&engine_names))
|
||||||
(cookies())
|
(cookies())
|
||||||
p class="message"{}
|
p class="message"{}
|
||||||
button type="submit" onclick="setClientSettings()"{"Save"}
|
button type="submit" onclick="setClientSettings()"{"Save"}
|
||||||
|
|
Loading…
Add table
Reference in a new issue