Moved the real handling of the request to the site info struct

This commit is contained in:
p11 2023-07-21 20:06:17 +02:00
parent b157e2c962
commit fdfeae97b2

View File

@ -202,10 +202,35 @@ impl Context {
),
};
info.handle_connection(
stream,
&request.body,
&relative_path,
&path,
&pk_path,
pki_path.as_ref().map(|path| path.as_ref()),
&data_path,
start_level,
)
}
}
impl SiteInfo {
fn handle_connection(
&mut self,
mut stream: TcpStream,
body: &str,
relative_path: &str,
path: &Path,
pk_path: &Path,
pki_path: Option<&Path>,
data_path: &Path,
start_level: usize,
) {
let mut name = None;
let mut text = None;
for entry in request.body.split('&') {
for entry in body.split('&') {
let Some((key, input)) = entry.split_once('=') else {
continue;
};
@ -238,20 +263,20 @@ impl Context {
.into(),
);
}
"up" => info.up += 1,
"down" => info.down += 1,
"up" => self.up += 1,
"down" => self.down += 1,
_ => (),
}
}
if let (Some(name), Some(text)) = (name, text) {
info.comments.push(Comment { name, text });
self.comments.push(Comment { name, text });
}
info.visits += 1;
self.visits += 1;
if let Ok(mut file) = File::create(data_path) {
if to_stream::<PortableSettings, _, _>(info, &mut file).is_err() {
if to_stream::<PortableSettings, _, _>(self, &mut file).is_err() {
eprintln!("Error saving data!");
eprintln!();
}
@ -264,22 +289,20 @@ impl Context {
let _ = writeln!(
stream,
"<p>👁️{} 💖️{} 💔️{}</p>",
info.visits, info.up, info.down
self.visits, self.up, self.down
);
let title = relative_path
.rsplit_once('/')
.map(|(_, title)| title)
.unwrap_or(&relative_path);
.unwrap_or(relative_path);
if !title.is_empty() {
let _ = writeln!(stream, "<h1>{title}</h1>");
}
let path_ref = &path;
let handle_entry = |mut entry: &str, output: &mut TcpStream, level: usize| {
let level = level + 1;
let mut path = path_ref.clone();
let mut path = path.to_path_buf();
if let Some((real_entry, _)) = entry.split_once(':') {
entry = real_entry
}
@ -363,7 +386,7 @@ impl Context {
};
let _ = stream.write_all(html.into_string().as_bytes());
for Comment { name, text } in &info.comments {
for Comment { name, text } in &self.comments {
let _ = writeln!(stream, "<fieldset><legend>{name}</legend>{text}</fieldset>");
}