Compare commits

...

2 Commits

Author SHA1 Message Date
Jonas Zeunert
313faf1864 Add nice html formatting for search 2024-04-23 17:16:26 +02:00
Jonas Zeunert
19d2a790a5 Add nice html formatting for search 2024-04-23 17:16:00 +02:00
2 changed files with 63 additions and 0 deletions

30
src/search_result.rs Normal file
View File

@@ -0,0 +1,30 @@
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 {
Self {
name: file.filename.clone(),
modified: file.last_modified.clone(),
size: file.filesize,
}
}
}

View File

@@ -56,8 +56,25 @@ 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)
}
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 {
info!("Redirecting request: {:?}", req);
@@ -184,3 +201,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>
"#;