From 434c793edf69803e58f4e64bd5a9a94e8d12e844 Mon Sep 17 00:00:00 2001 From: Ross Andrews Date: Mon, 1 Sep 2025 17:32:45 -0500 Subject: [PATCH] checkpoint --- Cargo.lock | 354 +++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 4 +- src/main.rs | 23 +++- src/renderer.rs | 11 +- src/server.rs | 51 +++++++ 5 files changed, 429 insertions(+), 14 deletions(-) create mode 100644 src/server.rs diff --git a/Cargo.lock b/Cargo.lock index 1467572..3b6b265 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -108,6 +108,15 @@ dependencies = [ "winit", ] +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + [[package]] name = "adler2" version = "2.0.1" @@ -447,6 +456,75 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "axum" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5" +dependencies = [ + "axum-core", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "rustversion", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "backtrace" +version = "0.3.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6806a6321ec58106fea15becdad98371e28d92ccbc7c8f1b3b6dd724fe8f1002" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + [[package]] name = "bit-set" version = "0.8.0" @@ -1054,15 +1132,23 @@ dependencies = [ name = "fleen" version = "0.1.0" dependencies = [ + "axum", "eframe", "egui_ltreeview", "markdown", "rfd", "serde", "thiserror 2.0.16", + "tokio", "toml", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "foldhash" version = "0.1.5" @@ -1191,9 +1277,15 @@ dependencies = [ "cfg-if", "libc", "r-efi", - "wasi", + "wasi 0.14.2+wasi-0.2.4", ] +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + [[package]] name = "gl_generator" version = "0.14.0" @@ -1378,6 +1470,89 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" +[[package]] +name = "http" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3aa54a13a0dfe7fbe3a59e0c76093041720fdc77b110cc0fc260fafb4dc51e" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "pin-utils", + "smallvec", + "tokio", +] + +[[package]] +name = "hyper-util" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d9b05277c7e8da2c93a568989bb6207bef0112e8d17df7a6eda4a3cf143bc5e" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "hyper", + "pin-project-lite", + "tokio", + "tower-service", +] + [[package]] name = "icu_collections" version = "2.0.0" @@ -1508,6 +1683,23 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "io-uring" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "046fa2d4d00aea763528b4950358d0ead425372445dc8ff86312b3c69ff7727b" +dependencies = [ + "bitflags 2.9.2", + "cfg-if", + "libc", +] + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + [[package]] name = "jni" version = "0.21.1" @@ -1664,6 +1856,12 @@ dependencies = [ "unicode-id", ] +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + [[package]] name = "memchr" version = "2.7.5" @@ -1703,6 +1901,12 @@ dependencies = [ "paste", ] +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + [[package]] name = "miniz_oxide" version = "0.8.9" @@ -1713,6 +1917,17 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "mio" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" +dependencies = [ + "libc", + "wasi 0.11.1+wasi-snapshot-preview1", + "windows-sys 0.59.0", +] + [[package]] name = "naga" version = "25.0.1" @@ -2109,6 +2324,15 @@ dependencies = [ "objc2-foundation 0.2.2", ] +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.21.3" @@ -2452,6 +2676,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "rustc-demangle" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -2496,6 +2726,12 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + [[package]] name = "same-file" version = "1.0.6" @@ -2550,6 +2786,28 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.143" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d401abef1d108fbd9cbaebc3e46611f4b1021f714a0597a71f41ee463f5f4a5a" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59fab13f937fa393d08645bf3a84bdfe86e296747b506ada67bb15f10f218b2a" +dependencies = [ + "itoa", + "serde", +] + [[package]] name = "serde_repr" version = "0.1.20" @@ -2570,6 +2828,18 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "shlex" version = "1.3.0" @@ -2657,6 +2927,16 @@ dependencies = [ "serde", ] +[[package]] +name = "socket2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233504af464074f9d066d7b5416c5f9b894a5862a6506e306f7b816cdd6f1807" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "spirv" version = "0.3.0+sdk-1.3.268.0" @@ -2717,6 +2997,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + [[package]] name = "synstructure" version = "0.13.2" @@ -2836,6 +3122,37 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tokio" +version = "1.47.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038" +dependencies = [ + "backtrace", + "bytes", + "io-uring", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "slab", + "socket2", + "tokio-macros", + "windows-sys 0.59.0", +] + +[[package]] +name = "tokio-macros" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml" version = "0.9.5" @@ -2892,12 +3209,41 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcc842091f2def52017664b53082ecbbeb5c7731092bad69d2c63050401dfd64" +[[package]] +name = "tower" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + [[package]] name = "tracing" version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -3013,6 +3359,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "wasi" version = "0.14.2+wasi-0.2.4" diff --git a/Cargo.toml b/Cargo.toml index 4e0ec05..e50e84a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,6 @@ rfd = "0.15.4" thiserror = "2.0.16" toml = "0.9.5" markdown = "1.0.0" -serde = { version = "1.0.219", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0.219", features = ["derive"] } +axum = "0.8.4" +tokio = { version = "1.47.1", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index db12e61..b05ed3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,17 @@ mod fleen_app; mod renderer; +mod server; use std::path::{Path, PathBuf}; use eframe::egui::{Color32, Context, Id, RichText}; use eframe::{egui, Frame}; use egui_ltreeview::Action; +use tokio::task::JoinHandle; use crate::fleen_app::{FileType, FleenApp, FleenError, TreeEntry}; +use crate::server::start_server; -fn main() { +#[tokio::main] +async fn main() { let native_options = eframe::NativeOptions::default(); eframe::run_native("Fleen", native_options, Box::new(|_cc| { Ok(Box::new(FleenUi::default())) @@ -25,7 +29,8 @@ struct FleenUi { app: Option, error: Option, selected_file: Option, - dialog_mode: Option + dialog_mode: Option, + server_handle: Option> } impl FleenUi { @@ -34,15 +39,21 @@ impl FleenUi { ui.label("No site selected!"); if ui.button("Open site...").clicked() && let Some(path) = rfd::FileDialog::new().pick_folder() { - match FleenApp::open(path) { - Ok(app) => { self.app = Some(app) } + match FleenApp::open(path.clone()) { + Ok(app) => { + self.app = Some(app); + self.server_handle = Some(tokio::spawn(start_server(path))) + } Err(err) => { self.error = Some(err) } } } if ui.button("New site...").clicked() && let Some(path) = rfd::FileDialog::new().pick_folder() { - match FleenApp::create(path) { - Ok(app) => { self.app = Some(app) } + match FleenApp::create(path.clone()) { + Ok(app) => { + self.app = Some(app); + self.server_handle = Some(tokio::spawn(start_server(path))) + } Err(err) => { self.error = Some(err) } } } diff --git a/src/renderer.rs b/src/renderer.rs index 925124c..629c09b 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -30,7 +30,7 @@ pub struct Frontmatter { } impl Frontmatter { - fn apply_layout(self, content: String, filename: PathBuf, root: PathBuf) -> Result { + fn apply_layout(self, content: String, filename: PathBuf, root: &PathBuf) -> Result { let title = self.title.unwrap_or(String::new()); let wrapped = if let Some(layout) = self.layout { let absolute_layout = root.join(layout); @@ -61,8 +61,7 @@ pub enum RenderError { /// (if it exists) or index.md. Note that this does not guarantee that the file actually exists, it /// just chooses that because those are the files which can become index.html in the output and /// index.html is what a reasonable webserver will pick as the default file. -pub fn default_path(root: impl Into) -> PathBuf { - let root: PathBuf = root.into(); +pub fn default_path(root: &PathBuf) -> PathBuf { if let Ok(true) = fs::exists(root.join("/index.html")) { "index.html".into() } else { @@ -71,7 +70,7 @@ pub fn default_path(root: impl Into) -> PathBuf { } /// Take a source file path (relative to the root) and the root path, and return a RenderOutput for it -pub fn render(source: PathBuf, root: PathBuf) -> Result { +pub fn render(source: PathBuf, root: &PathBuf) -> Result { if skipped_path(source.clone()) { return Ok(RenderOutput::NoOutput) } @@ -97,7 +96,7 @@ fn skipped_path(source: PathBuf) -> bool { } // This gets called by `render` if the source path extension is md -fn render_as_markdown(source: PathBuf, root: PathBuf) -> Result { +fn render_as_markdown(source: PathBuf, root: &PathBuf) -> Result { let absolute_source = root.join(source.clone()); let contents = fs::read_to_string(absolute_source.clone()).map_err(|e| RenderError::FileReadError(e, source.clone()))?; let options = markdown_options(); @@ -144,7 +143,7 @@ mod tests { use super::*; fn render_file(path: impl Into) -> RenderOutput { - match render(path.into(), "./testdata".into()) { + match render(path.into(), &("./testdata".into())) { Ok(ro) => ro, Err(e) => { println!("{}", e); diff --git a/src/server.rs b/src/server.rs new file mode 100644 index 0000000..b26eeed --- /dev/null +++ b/src/server.rs @@ -0,0 +1,51 @@ +use std::path::PathBuf; +use std::sync::Arc; +use axum::extract::State; +use axum::response::{IntoResponse, Response}; +use axum::Router; +use axum::routing::get; +use crate::renderer::{default_path, render, RenderOutput}; + +pub async fn start_server(root: PathBuf) { + let state = Arc::new(root); + let app = Router::new() + .route("/", get(handler)) + .with_state(state); + + // run our app with hyper, listening globally on port 3000 + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + axum::serve(listener, app).await.unwrap(); +} + +async fn handler( + State(root): State>, +) -> impl IntoResponse { + let path = default_path(&root); + serve_path(String::from(path.to_str().unwrap()), root) +} + +fn serve_path(path: String, root: impl AsRef) -> impl IntoResponse { + let render = render(path.into(), root.as_ref()); + match render { + Ok(RenderOutput::Rendered(file, content)) | + Ok(RenderOutput::Hidden(file, content)) => { + Response::builder() + .status(200) + .header("Content-Type", "text/html") + .body(content).unwrap() + } + Ok(RenderOutput::RawFile(file)) => { todo!() } + Ok(RenderOutput::NoOutput) | + Ok(RenderOutput::Dir(_)) => { + Response::builder() + .status(404) + .body(String::from("Not Found")).unwrap() + } + Err(err) => { + Response::builder() + .status(500) + .body(format!("{}", err)).unwrap() + } + } +} +