neon_arch
commited on
Commit
•
b2c72bd
1
Parent(s):
13ce420
⚙️ refactor: replace pass by value with pass by reference (#180)(#178)
Browse files- src/server/routes.rs +23 -23
src/server/routes.rs
CHANGED
@@ -62,10 +62,10 @@ pub async fn not_found(
|
|
62 |
/// * `engines` - It stores the user selected upstream search engines selected from the UI.
|
63 |
#[allow(dead_code)]
|
64 |
#[derive(Deserialize)]
|
65 |
-
struct Cookie {
|
66 |
-
theme:
|
67 |
-
colorscheme:
|
68 |
-
engines: Vec
|
69 |
}
|
70 |
|
71 |
/// Handles the route of search page of the `websurfx` meta search engine website and it takes
|
@@ -111,9 +111,9 @@ pub async fn search(
|
|
111 |
page - 1
|
112 |
),
|
113 |
&config,
|
114 |
-
query
|
115 |
page - 1,
|
116 |
-
req
|
117 |
),
|
118 |
results(
|
119 |
format!(
|
@@ -121,9 +121,9 @@ pub async fn search(
|
|
121 |
config.binding_ip, config.port, query, page
|
122 |
),
|
123 |
&config,
|
124 |
-
query
|
125 |
page,
|
126 |
-
req
|
127 |
),
|
128 |
results(
|
129 |
format!(
|
@@ -134,9 +134,9 @@ pub async fn search(
|
|
134 |
page + 1
|
135 |
),
|
136 |
&config,
|
137 |
-
query
|
138 |
page + 1,
|
139 |
-
req
|
140 |
)
|
141 |
);
|
142 |
|
@@ -154,30 +154,28 @@ pub async fn search(
|
|
154 |
async fn results(
|
155 |
url: String,
|
156 |
config: &Config,
|
157 |
-
query:
|
158 |
page: u32,
|
159 |
-
req: HttpRequest,
|
160 |
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
161 |
//Initialize redis cache connection struct
|
162 |
-
let mut redis_cache = RedisCache::new(config.redis_url.
|
163 |
// fetch the cached results json.
|
164 |
-
let cached_results_json = redis_cache.cached_json(&url);
|
165 |
// check if fetched cache results was indeed fetched or it was an error and if so
|
166 |
// handle the data accordingly.
|
167 |
match cached_results_json {
|
168 |
-
Ok(results) => Ok(serde_json::from_str::<SearchResults>(&results)
|
169 |
Err(_) => {
|
170 |
// check if the cookie value is empty or not if it is empty then use the
|
171 |
// default selected upstream search engines from the config file otherwise
|
172 |
// parse the non-empty cookie and grab the user selected engines from the
|
173 |
// UI and use that.
|
174 |
-
let mut results:
|
175 |
-
.cookie("appCookie")
|
176 |
-
{
|
177 |
Some(cookie_value) => {
|
178 |
let cookie_value: Cookie = serde_json::from_str(cookie_value.name_value().1)?;
|
179 |
|
180 |
-
let engines = cookie_value
|
181 |
.engines
|
182 |
.iter()
|
183 |
.filter_map(|name| EngineHandler::new(name))
|
@@ -188,7 +186,7 @@ async fn results(
|
|
188 |
page,
|
189 |
config.aggregator.random_delay,
|
190 |
config.debug,
|
191 |
-
engines,
|
192 |
config.request_timeout,
|
193 |
)
|
194 |
.await?
|
@@ -199,14 +197,16 @@ async fn results(
|
|
199 |
page,
|
200 |
config.aggregator.random_delay,
|
201 |
config.debug,
|
202 |
-
config.upstream_search_engines
|
203 |
config.request_timeout,
|
204 |
)
|
205 |
.await?
|
206 |
}
|
207 |
};
|
208 |
-
results.add_style(config.style
|
209 |
-
redis_cache
|
|
|
|
|
210 |
Ok(results)
|
211 |
}
|
212 |
}
|
|
|
62 |
/// * `engines` - It stores the user selected upstream search engines selected from the UI.
|
63 |
#[allow(dead_code)]
|
64 |
#[derive(Deserialize)]
|
65 |
+
struct Cookie<'a> {
|
66 |
+
theme: &'a str,
|
67 |
+
colorscheme: &'a str,
|
68 |
+
engines: Vec<&'a str>,
|
69 |
}
|
70 |
|
71 |
/// Handles the route of search page of the `websurfx` meta search engine website and it takes
|
|
|
111 |
page - 1
|
112 |
),
|
113 |
&config,
|
114 |
+
query,
|
115 |
page - 1,
|
116 |
+
&req,
|
117 |
),
|
118 |
results(
|
119 |
format!(
|
|
|
121 |
config.binding_ip, config.port, query, page
|
122 |
),
|
123 |
&config,
|
124 |
+
query,
|
125 |
page,
|
126 |
+
&req,
|
127 |
),
|
128 |
results(
|
129 |
format!(
|
|
|
134 |
page + 1
|
135 |
),
|
136 |
&config,
|
137 |
+
query,
|
138 |
page + 1,
|
139 |
+
&req,
|
140 |
)
|
141 |
);
|
142 |
|
|
|
154 |
async fn results(
|
155 |
url: String,
|
156 |
config: &Config,
|
157 |
+
query: &str,
|
158 |
page: u32,
|
159 |
+
req: &HttpRequest,
|
160 |
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
161 |
//Initialize redis cache connection struct
|
162 |
+
let mut redis_cache = RedisCache::new(&config.redis_url, 5).await?;
|
163 |
// fetch the cached results json.
|
164 |
+
let cached_results_json = redis_cache.cached_json(&url).await;
|
165 |
// check if fetched cache results was indeed fetched or it was an error and if so
|
166 |
// handle the data accordingly.
|
167 |
match cached_results_json {
|
168 |
+
Ok(results) => Ok(serde_json::from_str::<SearchResults>(&results)?),
|
169 |
Err(_) => {
|
170 |
// check if the cookie value is empty or not if it is empty then use the
|
171 |
// default selected upstream search engines from the config file otherwise
|
172 |
// parse the non-empty cookie and grab the user selected engines from the
|
173 |
// UI and use that.
|
174 |
+
let mut results: SearchResults = match req.cookie("appCookie") {
|
|
|
|
|
175 |
Some(cookie_value) => {
|
176 |
let cookie_value: Cookie = serde_json::from_str(cookie_value.name_value().1)?;
|
177 |
|
178 |
+
let engines: Vec<EngineHandler> = cookie_value
|
179 |
.engines
|
180 |
.iter()
|
181 |
.filter_map(|name| EngineHandler::new(name))
|
|
|
186 |
page,
|
187 |
config.aggregator.random_delay,
|
188 |
config.debug,
|
189 |
+
&engines,
|
190 |
config.request_timeout,
|
191 |
)
|
192 |
.await?
|
|
|
197 |
page,
|
198 |
config.aggregator.random_delay,
|
199 |
config.debug,
|
200 |
+
&config.upstream_search_engines,
|
201 |
config.request_timeout,
|
202 |
)
|
203 |
.await?
|
204 |
}
|
205 |
};
|
206 |
+
results.add_style(&config.style);
|
207 |
+
redis_cache
|
208 |
+
.cache_results(&serde_json::to_string(&results)?, &url)
|
209 |
+
.await?;
|
210 |
Ok(results)
|
211 |
}
|
212 |
}
|