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

♻️ refactor(API): provide a minor redesign to the internal api to support parsing of json data (#427)

This commit is contained in:
neon_arch 2024-11-02 21:59:22 +03:00
parent 65fabe5209
commit 3221264324
3 changed files with 21 additions and 22 deletions

View File

@ -10,7 +10,7 @@
/// order to allow the deserializing the json back to struct in aggregate function in /// order to allow the deserializing the json back to struct in aggregate function in
/// aggregator.rs and create a new struct out of it and then serialize it back to json and pass /// aggregator.rs and create a new struct out of it and then serialize it back to json and pass
/// it to the template files. /// it to the template files.
#[derive(Default)] #[derive(Default, Clone)]
pub struct Style { pub struct Style {
/// It stores the parsed theme option used to set a theme for the website. /// It stores the parsed theme option used to set a theme for the website.
pub theme: String, pub theme: String,

View File

@ -1,17 +1,15 @@
//! This module provides the models to parse cookies and search parameters from the search //! This module provides the models to parse cookies and search parameters from the search
//! engine website. //! engine website.
use std::borrow::Cow;
use serde::Deserialize;
use super::parser_models::Style; use super::parser_models::Style;
use serde::{Deserialize, Serialize};
/// A named struct which deserializes all the user provided search parameters and stores them. /// A named struct which deserializes all the user provided search parameters and stores them.
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct SearchParams { pub struct SearchParams {
/// It stores the search parameter option `q` (or query in simple words) /// It stores the search parameter option `q` (or query in simple words)
/// of the search url. /// of the search url.
pub q: Option<Cow<'static, str>>, pub q: Option<String>,
/// It stores the search parameter `page` (or pageno in simple words) /// It stores the search parameter `page` (or pageno in simple words)
/// of the search url. /// of the search url.
pub page: Option<u32>, pub page: Option<u32>,
@ -22,26 +20,29 @@ pub struct SearchParams {
/// A named struct which is used to deserialize the cookies fetched from the client side. /// A named struct which is used to deserialize the cookies fetched from the client side.
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Deserialize)] #[derive(Deserialize, Serialize)]
pub struct Cookie<'a> { pub struct Cookie {
/// It stores the theme name used in the website. /// It stores the theme name used in the website.
pub theme: Cow<'a, str>, pub theme: String,
/// It stores the colorscheme name used for the website theme. /// It stores the colorscheme name used for the website theme.
pub colorscheme: Cow<'a, str>, pub colorscheme: String,
/// It stores the animation name used for the website theme.
pub animation: Option<String>,
/// It stores the user selected upstream search engines selected from the UI. /// It stores the user selected upstream search engines selected from the UI.
pub engines: Cow<'a, [Cow<'a, str>]>, pub engines: Vec<String>,
/// It stores the user selected safe search level from the UI. /// It stores the user selected safe search level from the UI.
pub safe_search_level: u8, pub safe_search_level: u8,
} }
impl<'a> Cookie<'a> { impl Cookie {
/// server_models::Cookie contructor function /// server_models::Cookie contructor function
pub fn build(style: &'a Style, mut engines: Vec<Cow<'a, str>>, safe_search_level: u8) -> Self { pub fn build(style: Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
engines.sort(); engines.sort();
Self { Self {
theme: Cow::Borrowed(&style.theme), theme: style.theme.clone(),
colorscheme: Cow::Borrowed(&style.colorscheme), colorscheme: style.colorscheme.clone(),
engines: Cow::Owned(engines), animation: style.animation.clone(),
engines,
safe_search_level, safe_search_level,
} }
} }

View File

@ -15,7 +15,7 @@ use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
use itertools::Itertools; use itertools::Itertools;
use regex::Regex; use regex::Regex;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use std::{borrow::Cow, time::Duration}; use tokio::time::Duration;
use tokio::{ use tokio::{
fs::File, fs::File,
io::{AsyncBufReadExt, BufReader}, io::{AsyncBufReadExt, BufReader},
@ -54,17 +54,15 @@ pub async fn search(
let cookie = req.cookie("appCookie"); let cookie = req.cookie("appCookie");
// 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 mut search_settings: server_models::Cookie<'_> = cookie let mut search_settings: server_models::Cookie = 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_or_else(|| { .unwrap_or_else(|| {
server_models::Cookie::build( server_models::Cookie::build(
&config.style, config.style.clone(),
config config
.upstream_search_engines .upstream_search_engines
.iter() .iter()
.filter_map(|(engine, enabled)| { .filter_map(|(engine, enabled)| enabled.then_some(engine.clone()))
enabled.then_some(Cow::Borrowed(engine.as_str()))
})
.collect(), .collect(),
config.safe_search, config.safe_search,
) )
@ -160,7 +158,7 @@ async fn results(
cache: &'static SharedCache, cache: &'static SharedCache,
query: &str, query: &str,
page: u32, page: u32,
search_settings: &server_models::Cookie<'_>, search_settings: &server_models::Cookie,
) -> Result<(SearchResults, String, bool), Box<dyn std::error::Error>> { ) -> Result<(SearchResults, String, bool), Box<dyn std::error::Error>> {
// eagerly parse cookie value to evaluate safe search level // eagerly parse cookie value to evaluate safe search level
let safe_search_level = search_settings.safe_search_level; let safe_search_level = search_settings.safe_search_level;