diff --git a/Cargo.lock b/Cargo.lock index 79a4da5..2aa0066 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -231,6 +231,8 @@ dependencies = [ "env_logger", "ipnet", "log", + "serde", + "toml", ] [[package]] @@ -262,6 +264,16 @@ version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + [[package]] name = "serde_core" version = "1.0.228" @@ -282,6 +294,15 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_spanned" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776" +dependencies = [ + "serde_core", +] + [[package]] name = "strsim" version = "0.11.1" @@ -299,6 +320,37 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "toml" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "399b1124a3c9e16766831c6bba21e50192572cdd98706ea114f9502509686ffc" +dependencies = [ + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.0.0+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32c2555c699578a4f59f0cc68e5116c8d7cabbd45e1409b989d4be085b53f13e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.0.9+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702d4415e08923e7e1ef96cd5727c0dfed80b4d2fa25db9647fe5eb6f7c5a4c4" +dependencies = [ + "winnow", +] + [[package]] name = "unicode-ident" version = "1.0.24" @@ -325,3 +377,9 @@ checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ "windows-link", ] + +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" diff --git a/Cargo.toml b/Cargo.toml index 7fd094a..1488d70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,11 @@ clap = { version = "4.5.60", features = ["derive"] } env_logger = "0.11.9" ipnet = "2.11.0" log = "0.4.29" +toml = { version = "1.0.6", default-features = false, features = [ + "serde", + "parse", +] } +serde = { version = "1.0.228", features = ["derive"] } [profile.release] opt-level = "z" diff --git a/Dockerfile b/Dockerfile index a29ab01..7bafc51 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,12 +12,12 @@ RUN cargo build --target x86_64-unknown-linux-musl --release FROM alpine:3.21 -# Create app directory +# Create app directory (also cd's us into this dir) WORKDIR /app -# Expose the 8000 port for HTTP traffic EXPOSE 9696 +# Copy the binary compiled in the builder image COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/recipe-sync-server-rs ./ ENTRYPOINT [ "/app/recipe-sync-server-rs" ] diff --git a/docker-compose.yml b/docker-compose.yml index f00260c..df3249b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,11 +9,3 @@ services: - ./recipes:/app/recipes environment: - RUST_LOG=info - networks: - - sync-net - -networks: - sync-net: - ipam: - config: - - subnet: 172.20.0.0/24 diff --git a/justfile b/justfile new file mode 100644 index 0000000..0513632 --- /dev/null +++ b/justfile @@ -0,0 +1,12 @@ +set export +set quiet + +USERNAME := "pixelatedw" +IMAGE := "recipe-sync-server-rs" +VERSION := `cargo get package.version` + +run path: + RUST_LOG=info cargo run -- -e {{path}} + +build: + docker build -t "${USERNAME}/${IMAGE}:${VERSION}" "." diff --git a/src/main.rs b/src/main.rs index 61fa989..1587af3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::{ }; use clap::Parser; +use toml::Table; use crate::{buffer::ByteBuffer, cli::CliArgs}; @@ -73,8 +74,6 @@ fn handle_connection( let files_sent = paths.len(); - log::info!("Sending {files_sent} files to {remote_ip}"); - buffer.write_usize(files_sent); for file in paths { @@ -99,6 +98,9 @@ fn handle_connection( }; } + let buf_size = buffer.len(); + log::info!("Sending {files_sent} files to {remote_ip} totalling {buf_size} bytes"); + let _ = conn.write_all(&buffer); let _ = conn.flush(); @@ -112,9 +114,57 @@ fn walk_dir>(path: P, file_paths: &mut Vec) { continue; } - let file_path = entry.path().to_str().unwrap().to_string(); + if let Some(ext) = entry.path().extension() + && ext == "md" + { + let path = entry.path(); + let file_path = path.to_str(); + let parent_dir = path.parent(); - file_paths.push(file_path); + if let Some(file_path) = file_path { + if let Some(parent_dir) = parent_dir { + parse_md_file(file_path, parent_dir, file_paths); + } + + file_paths.push(file_path.to_string()); + } + } else { + continue; + } + } +} + +fn parse_md_file(file_path: &str, parent_dir: &Path, file_paths: &mut Vec) { + // Generate a string that only contains the TOML header + let mut toml_header = String::new(); + let content = fs::read_to_string(file_path).unwrap(); + let lines = content.lines(); + for (i, line) in lines.enumerate() { + if i > 0 && line == "+++" { + break; + } else if i == 0 { + continue; + } + + toml_header.push_str(line); + toml_header.push('\n'); + } + + let toml_header = toml_header.as_str(); + let recipe = toml_header.parse::().unwrap(); + + // Take all the pics found and add their paths to the map + if recipe.contains_key("pics") + && let Some(pics) = recipe["pics"].as_array() + { + for pic in pics { + if let Some(pic) = pic.as_str() { + let mut dir = PathBuf::from(parent_dir); + dir.push(pic); + + file_paths.push(dir.to_str().unwrap().to_string()); + } + } } }