From 7c1969fa0beb8b038ba8d2edc8c3dc4bf0973aaf Mon Sep 17 00:00:00 2001 From: p11 Date: Wed, 19 Jul 2023 19:50:27 +0200 Subject: [PATCH] Count and show views --- src/main.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index ce1f4c3..f2dc480 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,16 +43,10 @@ struct Comment { text: Box, } +#[derive(Default)] struct SiteInfo { comments: Vec, -} - -impl SiteInfo { - fn new() -> Self { - Self { - comments: Vec::new(), - } - } + visits: usize, } #[derive(Default)] @@ -140,7 +134,7 @@ impl Context { use Entry::*; let info = match self.infos.entry(relative_path.clone().into_boxed_str()) { Occupied(o) => o.into_mut(), - Vacant(v) => v.insert(SiteInfo::new()), + Vacant(v) => v.insert(SiteInfo::default()), }; 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 _ = writeln!(stream, ""); + let _ = writeln!(stream, "

👁️{}

", info.visits); + let title = relative_path .rsplit_once('/') .map(|(_, title)| title) .unwrap_or(&relative_path); if !title.is_empty() { - let _ = write!(stream, "

{title}

"); + let _ = writeln!(stream, "

{title}

"); } let path_ref = &path;