Compare commits
3 Commits
94d4425661
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3046ea6d97 | ||
|
|
313faf1864 | ||
|
|
19d2a790a5 |
38
src/search_result.rs
Normal file
38
src/search_result.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::elastic_response::File;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize)]
|
||||||
|
pub struct SearchResult {
|
||||||
|
pub name: String,
|
||||||
|
pub modified: String,
|
||||||
|
pub size: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SearchResult {
|
||||||
|
pub fn to_html(&self) -> String {
|
||||||
|
format!(
|
||||||
|
"<li><span><a href=\"/{name}\">{name}</a>({size} Bytes)</li>{modified}</span>",
|
||||||
|
name = self.name,
|
||||||
|
size = self.size,
|
||||||
|
modified = self.modified
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<&File> for SearchResult {
|
||||||
|
fn from(file: &File) -> Self {
|
||||||
|
let name = file
|
||||||
|
.filename
|
||||||
|
.split('.')
|
||||||
|
.next()
|
||||||
|
.ok_or(file.filename.clone())
|
||||||
|
.unwrap()
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
name,
|
||||||
|
modified: file.last_modified.clone(),
|
||||||
|
size: file.filesize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -56,8 +56,26 @@ fn search(req: &Request) -> Response {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if let Some(_) = req.get_param("pretty") {
|
||||||
|
return Response::html(search_to_html(response)).with_status_code(200);
|
||||||
|
}
|
||||||
Response::json(&response).with_status_code(200)
|
Response::json(&response).with_status_code(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn search_to_html(results: Vec<SearchResult>) -> String {
|
||||||
|
let res_html = results
|
||||||
|
.iter()
|
||||||
|
.map(|res| res.to_html())
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join(" \n");
|
||||||
|
let body = format!(
|
||||||
|
"<div>Yay, found the following {} lists!</div><div><ul>{}</ul></div>",
|
||||||
|
results.len(),
|
||||||
|
res_html
|
||||||
|
);
|
||||||
|
HTML_TEMPLATE.replace("{body}", &body)
|
||||||
|
}
|
||||||
|
|
||||||
fn redirect(req: &Request) -> Response {
|
fn redirect(req: &Request) -> Response {
|
||||||
info!("Redirecting request: {:?}", req);
|
info!("Redirecting request: {:?}", req);
|
||||||
|
|
||||||
@@ -79,18 +97,13 @@ fn redirect(req: &Request) -> Response {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let Some(first) = response.get(0) else {
|
let Some(first) = response.get(0) else {
|
||||||
error!("No search results");
|
error!("No search results. Should not happen because we checked before!");
|
||||||
return Response::text("Could not find any awesome page to redirect you to :()")
|
return Response::text("Could not find any awesome page to redirect you to :()")
|
||||||
.with_status_code(404);
|
.with_status_code(404);
|
||||||
};
|
};
|
||||||
|
|
||||||
let Some(name) = first.name.split('.').next() else {
|
info!("Redirecting to: /{}", first.name);
|
||||||
error!("Error getting name from response!");
|
Response::redirect_303(format!("/{}", first.name))
|
||||||
return Response::text("No fitting awesome list found :(").with_status_code(404);
|
|
||||||
};
|
|
||||||
|
|
||||||
info!("Redirecting to: /{}", name);
|
|
||||||
Response::redirect_303(format!("/{}", name))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn query_elastic_search(query: String) -> Result<Vec<SearchResult>, Box<dyn Error>> {
|
fn query_elastic_search(query: String) -> Result<Vec<SearchResult>, Box<dyn Error>> {
|
||||||
@@ -184,3 +197,19 @@ mod test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static HTML_TEMPLATE: &'static str = r#"
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>AWESM Search Results</title>
|
||||||
|
<link rel="stylesheet" href="/style/search.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{body}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"#;
|
||||||
|
|||||||
Reference in New Issue
Block a user