Simpler way to update site info and create a local variant
This commit is contained in:
parent
8e6efef5bb
commit
7722a5ee95
78
src/main.rs
78
src/main.rs
@ -42,16 +42,57 @@ struct Comment {
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct SiteInfo {
|
||||
struct SharedSiteInfo {
|
||||
comments: Mutex<Vec<Comment>>,
|
||||
visits: AtomicUsize,
|
||||
up: AtomicUsize,
|
||||
down: AtomicUsize,
|
||||
}
|
||||
|
||||
struct SiteInfo {
|
||||
comments: Vec<Comment>,
|
||||
visits: usize,
|
||||
up: usize,
|
||||
down: usize,
|
||||
}
|
||||
|
||||
impl SharedSiteInfo {
|
||||
fn update(
|
||||
self: &Arc<Self>,
|
||||
name: Option<Box<str>>,
|
||||
text: Option<Box<str>>,
|
||||
up: bool,
|
||||
down: bool,
|
||||
) -> SiteInfo {
|
||||
SiteInfo {
|
||||
comments: {
|
||||
if let Ok(mut comments) = self.comments.lock() {
|
||||
if let (Some(name), Some(text)) = (name, text) {
|
||||
comments.push(Comment { name, text });
|
||||
}
|
||||
comments.clone()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
},
|
||||
visits: self.visits.fetch_add(1, Ordering::Relaxed),
|
||||
up: if up {
|
||||
self.up.fetch_add(1, Ordering::Relaxed)
|
||||
} else {
|
||||
self.up.load(Ordering::Relaxed)
|
||||
},
|
||||
down: if down {
|
||||
self.down.fetch_add(1, Ordering::Relaxed)
|
||||
} else {
|
||||
self.down.load(Ordering::Relaxed)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Context {
|
||||
infos: HashMap<Box<str>, Arc<SiteInfo>>,
|
||||
infos: HashMap<Box<str>, Arc<SharedSiteInfo>>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
@ -137,7 +178,7 @@ fn fail(mut stream: TcpStream) {
|
||||
let _ = writeln!(stream, "Page not found!");
|
||||
}
|
||||
|
||||
impl<S: SizeSettings> ToStream<S> for SiteInfo {
|
||||
impl<S: SizeSettings> ToStream<S> for SharedSiteInfo {
|
||||
fn to_stream<W: Write>(&self, stream: &mut W) -> Result<()> {
|
||||
let Ok(comments) = self.comments.lock() else {
|
||||
return Err(std::io::ErrorKind::ResourceBusy.into());
|
||||
@ -161,7 +202,7 @@ impl<S: SizeSettings> ToStream<S> for SiteInfo {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: SizeSettings> FromStream<S> for SiteInfo {
|
||||
impl<S: SizeSettings> FromStream<S> for SharedSiteInfo {
|
||||
fn from_stream<R: Read>(stream: &mut R) -> Result<Self> {
|
||||
let size = S::size_from_stream(stream)?;
|
||||
let comments = Mutex::new(
|
||||
@ -538,7 +579,7 @@ impl<'a> RequestData<'a> {
|
||||
}
|
||||
|
||||
fn handle_relative_connection(
|
||||
info: Arc<SiteInfo>,
|
||||
info: Arc<SharedSiteInfo>,
|
||||
mut stream: TcpStream,
|
||||
body: &str,
|
||||
relative_path: &str,
|
||||
@ -556,27 +597,12 @@ fn handle_relative_connection(
|
||||
progress,
|
||||
} = RequestData::parse(body);
|
||||
|
||||
let comments = {
|
||||
let Ok(mut comments) = info.comments.lock() else {
|
||||
return;
|
||||
};
|
||||
if let (Some(name), Some(text)) = (name, text) {
|
||||
comments.push(Comment { name, text });
|
||||
}
|
||||
comments.clone()
|
||||
};
|
||||
let up = if up {
|
||||
info.up.fetch_add(1, Ordering::Relaxed)
|
||||
} else {
|
||||
info.up.load(Ordering::Relaxed)
|
||||
};
|
||||
let down = if down {
|
||||
info.down.fetch_add(1, Ordering::Relaxed)
|
||||
} else {
|
||||
info.down.load(Ordering::Relaxed)
|
||||
};
|
||||
|
||||
let visits = info.visits.fetch_add(1, Ordering::Relaxed);
|
||||
let SiteInfo {
|
||||
comments,
|
||||
visits,
|
||||
up,
|
||||
down,
|
||||
} = info.update(name, text, up, down);
|
||||
|
||||
if let Ok(mut file) = File::create(file_paths.data) {
|
||||
if to_stream::<PortableSettings, _, _>(&*info, &mut file).is_err() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user