Spaces:
Runtime error
Runtime error
ddotthomas
commited on
Commit
•
d912bff
1
Parent(s):
6e9250c
change: revert Cookie Strings back to &str
Browse files
src/models/server_models.rs
CHANGED
@@ -21,24 +21,24 @@ pub struct SearchParams {
|
|
21 |
/// A named struct which is used to deserialize the cookies fetched from the client side.
|
22 |
#[allow(dead_code)]
|
23 |
#[derive(Deserialize)]
|
24 |
-
pub struct Cookie {
|
25 |
/// It stores the theme name used in the website.
|
26 |
-
pub theme:
|
27 |
/// It stores the colorscheme name used for the website theme.
|
28 |
-
pub colorscheme:
|
29 |
/// It stores the user selected upstream search engines selected from the UI.
|
30 |
pub engines: Vec<String>,
|
31 |
/// It stores the user selected safe search level from the UI.
|
32 |
pub safe_search_level: u8,
|
33 |
}
|
34 |
|
35 |
-
impl Cookie {
|
36 |
/// server_models::Cookie contructor function
|
37 |
-
pub fn build(style: &Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
|
38 |
engines.sort();
|
39 |
Self {
|
40 |
-
theme: style.theme
|
41 |
-
colorscheme: style.colorscheme
|
42 |
engines,
|
43 |
safe_search_level,
|
44 |
}
|
|
|
21 |
/// A named struct which is used to deserialize the cookies fetched from the client side.
|
22 |
#[allow(dead_code)]
|
23 |
#[derive(Deserialize)]
|
24 |
+
pub struct Cookie<'a> {
|
25 |
/// It stores the theme name used in the website.
|
26 |
+
pub theme: &'a str,
|
27 |
/// It stores the colorscheme name used for the website theme.
|
28 |
+
pub colorscheme: &'a str,
|
29 |
/// It stores the user selected upstream search engines selected from the UI.
|
30 |
pub engines: Vec<String>,
|
31 |
/// It stores the user selected safe search level from the UI.
|
32 |
pub safe_search_level: u8,
|
33 |
}
|
34 |
|
35 |
+
impl<'a> Cookie<'a> {
|
36 |
/// server_models::Cookie contructor function
|
37 |
+
pub fn build(style: &'a Style, mut engines: Vec<String>, safe_search_level: u8) -> Self {
|
38 |
engines.sort();
|
39 |
Self {
|
40 |
+
theme: &style.theme,
|
41 |
+
colorscheme: &style.colorscheme,
|
42 |
engines,
|
43 |
safe_search_level,
|
44 |
}
|
src/server/routes/search.rs
CHANGED
@@ -62,9 +62,11 @@ pub async fn search(
|
|
62 |
)
|
63 |
};
|
64 |
|
|
|
|
|
65 |
// Get search settings using the user's cookie or from the server's config
|
66 |
-
let mut search_settings: server_models::Cookie = match
|
67 |
-
Some(cookie_value) => {
|
68 |
match serde_json::from_str(cookie_value.value()) {
|
69 |
Ok(cookie) => cookie,
|
70 |
// If there's an issue parsing the cookie's value, default to the config
|
@@ -127,10 +129,10 @@ async fn results(
|
|
127 |
cache: &web::Data<SharedCache>,
|
128 |
query: &str,
|
129 |
page: u32,
|
130 |
-
|
131 |
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
132 |
// eagerly parse cookie value to evaluate safe search level
|
133 |
-
let safe_search_level =
|
134 |
|
135 |
let cache_key = format!(
|
136 |
"http://{}:{}/search?q={}&page={}&safesearch={}&engines={}",
|
@@ -139,7 +141,7 @@ async fn results(
|
|
139 |
query,
|
140 |
page,
|
141 |
safe_search_level,
|
142 |
-
|
143 |
);
|
144 |
|
145 |
// fetch the cached results json.
|
@@ -167,14 +169,14 @@ async fn results(
|
|
167 |
// default selected upstream search engines from the config file otherwise
|
168 |
// parse the non-empty cookie and grab the user selected engines from the
|
169 |
// UI and use that.
|
170 |
-
let mut results: SearchResults = match
|
171 |
false => {
|
172 |
aggregate(
|
173 |
query,
|
174 |
page,
|
175 |
config.aggregator.random_delay,
|
176 |
config.debug,
|
177 |
-
&
|
178 |
.engines
|
179 |
.clone()
|
180 |
.into_iter()
|
|
|
62 |
)
|
63 |
};
|
64 |
|
65 |
+
let cookie = req.cookie("appCookie");
|
66 |
+
|
67 |
// Get search settings using the user's cookie or from the server's config
|
68 |
+
let mut search_settings: server_models::Cookie<'_> = match cookie {
|
69 |
+
Some(ref cookie_value) => {
|
70 |
match serde_json::from_str(cookie_value.value()) {
|
71 |
Ok(cookie) => cookie,
|
72 |
// If there's an issue parsing the cookie's value, default to the config
|
|
|
129 |
cache: &web::Data<SharedCache>,
|
130 |
query: &str,
|
131 |
page: u32,
|
132 |
+
search_settings: &server_models::Cookie<'_>,
|
133 |
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
134 |
// eagerly parse cookie value to evaluate safe search level
|
135 |
+
let safe_search_level = search_settings.safe_search_level;
|
136 |
|
137 |
let cache_key = format!(
|
138 |
"http://{}:{}/search?q={}&page={}&safesearch={}&engines={}",
|
|
|
141 |
query,
|
142 |
page,
|
143 |
safe_search_level,
|
144 |
+
search_settings.engines.join(",")
|
145 |
);
|
146 |
|
147 |
// fetch the cached results json.
|
|
|
169 |
// default selected upstream search engines from the config file otherwise
|
170 |
// parse the non-empty cookie and grab the user selected engines from the
|
171 |
// UI and use that.
|
172 |
+
let mut results: SearchResults = match search_settings.engines.is_empty() {
|
173 |
false => {
|
174 |
aggregate(
|
175 |
query,
|
176 |
page,
|
177 |
config.aggregator.random_delay,
|
178 |
config.debug,
|
179 |
+
&search_settings
|
180 |
.engines
|
181 |
.clone()
|
182 |
.into_iter()
|