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:
parent
65fabe5209
commit
3221264324
@ -10,7 +10,7 @@
|
||||
/// 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
|
||||
/// it to the template files.
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Style {
|
||||
/// It stores the parsed theme option used to set a theme for the website.
|
||||
pub theme: String,
|
||||
|
@ -1,17 +1,15 @@
|
||||
//! This module provides the models to parse cookies and search parameters from the search
|
||||
//! engine website.
|
||||
use std::borrow::Cow;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::parser_models::Style;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A named struct which deserializes all the user provided search parameters and stores them.
|
||||
#[derive(Deserialize)]
|
||||
pub struct SearchParams {
|
||||
/// It stores the search parameter option `q` (or query in simple words)
|
||||
/// 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)
|
||||
/// of the search url.
|
||||
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.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Deserialize)]
|
||||
pub struct Cookie<'a> {
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Cookie {
|
||||
/// 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.
|
||||
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.
|
||||
pub engines: Cow<'a, [Cow<'a, str>]>,
|
||||
pub engines: Vec<String>,
|
||||
/// It stores the user selected safe search level from the UI.
|
||||
pub safe_search_level: u8,
|
||||
}
|
||||
|
||||
impl<'a> Cookie<'a> {
|
||||
impl Cookie {
|
||||
/// 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();
|
||||
Self {
|
||||
theme: Cow::Borrowed(&style.theme),
|
||||
colorscheme: Cow::Borrowed(&style.colorscheme),
|
||||
engines: Cow::Owned(engines),
|
||||
theme: style.theme.clone(),
|
||||
colorscheme: style.colorscheme.clone(),
|
||||
animation: style.animation.clone(),
|
||||
engines,
|
||||
safe_search_level,
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse};
|
||||
use itertools::Itertools;
|
||||
use regex::Regex;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{borrow::Cow, time::Duration};
|
||||
use tokio::time::Duration;
|
||||
use tokio::{
|
||||
fs::File,
|
||||
io::{AsyncBufReadExt, BufReader},
|
||||
@ -54,17 +54,15 @@ pub async fn search(
|
||||
let cookie = req.cookie("appCookie");
|
||||
|
||||
// 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())
|
||||
.unwrap_or_else(|| {
|
||||
server_models::Cookie::build(
|
||||
&config.style,
|
||||
config.style.clone(),
|
||||
config
|
||||
.upstream_search_engines
|
||||
.iter()
|
||||
.filter_map(|(engine, enabled)| {
|
||||
enabled.then_some(Cow::Borrowed(engine.as_str()))
|
||||
})
|
||||
.filter_map(|(engine, enabled)| enabled.then_some(engine.clone()))
|
||||
.collect(),
|
||||
config.safe_search,
|
||||
)
|
||||
@ -160,7 +158,7 @@ async fn results(
|
||||
cache: &'static SharedCache,
|
||||
query: &str,
|
||||
page: u32,
|
||||
search_settings: &server_models::Cookie<'_>,
|
||||
search_settings: &server_models::Cookie,
|
||||
) -> Result<(SearchResults, String, bool), Box<dyn std::error::Error>> {
|
||||
// eagerly parse cookie value to evaluate safe search level
|
||||
let safe_search_level = search_settings.safe_search_level;
|
||||
|
Loading…
Reference in New Issue
Block a user