diff --git a/Cargo.lock b/Cargo.lock index ae9cfaf..de243fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -416,12 +416,20 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "670fdfda89751bc4a84ac13eaa63e205cf0fd22b4c9a5fbfa085b63c1f1d3a30" +[[package]] +name = "memo-map" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d1115007560874e373613744c6fba374c17688327a71c1476d1a5954cc857b" + [[package]] name = "minijinja" version = "2.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12ea9ac0a51fb5112607099560fdf0f90366ab088a2a9e6e8ae176794e9806aa" dependencies = [ + "memo-map", + "self_cell", "serde", ] @@ -511,6 +519,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "self_cell" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16c2f82143577edb4921b71ede051dac62ca3c16084e918bf7b40c96ae10eb33" + [[package]] name = "serde" version = "1.0.228" diff --git a/Cargo.toml b/Cargo.toml index 31f934e..60569ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ bytes = "1.11.0" http-body-util = "0.1.3" hyper = { version = "1.8.1", features = ["http1", "http2", "server"] } macro_rules_attribute = "0.2.2" -minijinja = { version = "2.14.0", features = ["loop_controls"] } +minijinja = { version = "2.14.0", features = ["loop_controls", "loader"] } serde = { version = "1.0.228", features = ["derive"] } smol = "2.0.2" smol-hyper = "0.1.1" diff --git a/src/loader.rs b/src/loader.rs index dcf0d1b..b903682 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,4 +1,5 @@ use std::future::IntoFuture; +use std::sync::Arc; use crate::AppState; use anyhow::anyhow; @@ -11,8 +12,8 @@ use smol::{ use toml::Table; pub struct Page { - header: Header, - content: String, + pub header: Header, + // content: String, } #[derive(Deserialize, Debug)] @@ -26,13 +27,13 @@ pub enum PageType { Base, } -#[derive(Deserialize, Debug)] +#[derive(Deserialize, Debug, Clone)] pub struct Header { - id: String, + pub id: String, // r#type: PageType, r#type: String, title: Option, - path: Option, + pub path: Option, // TODO impl this later // extras: Table, } @@ -41,8 +42,8 @@ pub struct Root { id: String, // r#type: PageType, r#type: String, - title: Option, - path: Option, + pub title: Option, + pub path: Option, // TODO impl this later // extras: Table, } @@ -61,6 +62,25 @@ impl AppState<'_> { if entry.file_type().await?.is_dir() { let entry_opened = fs::read_dir(entry.path()).await?; println!("Opened {}", entry.file_name().to_str().unwrap()); + let mut config_path = entry.path(); + config_path.push("config.toml"); + + let config_file = fs::read_to_string(config_path).await?; + let header: Header = toml::from_str(&config_file)?; + + let mut index = entry.path(); + index.push("index.html"); + if let Ok(mreoww) = fs::read_to_string(index).await { + self.env.add_template_owned(header.id.clone(), mreoww)?; + } else { + println!( + "{}, doesn't have a index.html ??", + entry.file_name().to_str().unwrap() + ); + continue; + } + let page = Page { header: header }; + self.pages.push(page); } else { println!("Not parsing {}", entry.file_name().to_str().unwrap()); } diff --git a/src/main.rs b/src/main.rs index fbff61c..ab76d16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ mod loader; use crate::loader::*; struct AppState<'a> { - pages: Vec, + pub pages: Vec, env: Environment<'a>, } @@ -28,18 +28,29 @@ struct AppState<'a> { async fn serve(req: Request, state: Arc>) -> Result>> { println!("Serving {}", req.uri()); - let tmpl = state.env.get_template("hello").unwrap(); - let guh = tmpl.render(context!(name => "meowmeowmeowmeow!")).unwrap(); - let guh2 = state.env.get_template("meow").unwrap(); - let guh3 = guh2.render(context!(meow => "meow")).unwrap(); - let path = req.uri().path(); - let reply: Bytes = match path { - "/" => guh.into(), - "/mreow" => guh3.into(), - _ => "Invalid path!".into(), + let mreow = state.pages.iter().find(|&x| { + if let Some(path_x) = &x.header.path { + println!("{},{}", path_x, path); + if path_x == path { + true + } else { + false + } + } else { + false + } + }); + let reply = match mreow { + Some(x) => state + .env + .get_template(&x.header.id) + .unwrap() + .render(context! {}) + .unwrap(), + None => "?".to_string(), }; - Ok(Response::new(Full::new(reply))) + Ok(Response::new(Full::new(reply.into()))) } async fn handle_client(client: Async, state: Arc>) -> Result<()> { diff --git a/template-serve/home/config.toml b/template-serve/home/config.toml index e69de29..3a8d1b3 100644 --- a/template-serve/home/config.toml +++ b/template-serve/home/config.toml @@ -0,0 +1,3 @@ +id = "meowOne" +type = "Root" +path = "/" diff --git a/template-serve/home/index.html b/template-serve/home/index.html new file mode 100644 index 0000000..df6dd36 --- /dev/null +++ b/template-serve/home/index.html @@ -0,0 +1 @@ +MREOW diff --git a/template-serve/second/config.toml b/template-serve/second/config.toml new file mode 100644 index 0000000..9287845 --- /dev/null +++ b/template-serve/second/config.toml @@ -0,0 +1,3 @@ +id = "meow" +type = "Root" +path = "/2" diff --git a/template-serve/second/index.html b/template-serve/second/index.html new file mode 100644 index 0000000..7b55a00 --- /dev/null +++ b/template-serve/second/index.html @@ -0,0 +1 @@ +MREOW2