Count and show views

This commit is contained in:
p11 2023-07-19 19:50:27 +02:00
parent d0a4bbf349
commit 7c1969fa0b

View File

@ -43,16 +43,10 @@ struct Comment {
text: Box<str>, text: Box<str>,
} }
#[derive(Default)]
struct SiteInfo { struct SiteInfo {
comments: Vec<Comment>, comments: Vec<Comment>,
} visits: usize,
impl SiteInfo {
fn new() -> Self {
Self {
comments: Vec::new(),
}
}
} }
#[derive(Default)] #[derive(Default)]
@ -140,7 +134,7 @@ impl Context {
use Entry::*; use Entry::*;
let info = match self.infos.entry(relative_path.clone().into_boxed_str()) { let info = match self.infos.entry(relative_path.clone().into_boxed_str()) {
Occupied(o) => o.into_mut(), Occupied(o) => o.into_mut(),
Vacant(v) => v.insert(SiteInfo::new()), Vacant(v) => v.insert(SiteInfo::default()),
}; };
let mut entries = request.body.split('&'); let mut entries = request.body.split('&');
@ -175,16 +169,20 @@ impl Context {
} }
} }
info.visits += 1;
let _ = write!(stream, "HTTP/1.1 200 OK\r\n\r\n"); let _ = write!(stream, "HTTP/1.1 200 OK\r\n\r\n");
let _ = writeln!(stream, "<meta charset=\"utf-8\">"); let _ = writeln!(stream, "<meta charset=\"utf-8\">");
let _ = writeln!(stream, "<p>👁️{}</p>", info.visits);
let title = relative_path let title = relative_path
.rsplit_once('/') .rsplit_once('/')
.map(|(_, title)| title) .map(|(_, title)| title)
.unwrap_or(&relative_path); .unwrap_or(&relative_path);
if !title.is_empty() { if !title.is_empty() {
let _ = write!(stream, "<h1>{title}</h1>"); let _ = writeln!(stream, "<h1>{title}</h1>");
} }
let path_ref = &path; let path_ref = &path;