diff --git a/bleh.txt b/bleh.txt index 2932c27..b2e81b5 100644 --- a/bleh.txt +++ b/bleh.txt @@ -1,47 +1,13 @@ -- templete-serve +Each dir defines a path, asset storage or blog index + +# page +Pages are single file html Jinja2 template files. They may also include an asset dir for assets local to that dir which can be accessed using a jinja function `` get_local_asset("img.png") `` +you may also include assets within the page dir directly and use `` get_local_asset("../img.png") `` + +this is how you'd define a page +- main_page/ - config.toml - - home - - id: "MAINHOME" - - type: root - - assets - - pngs & mp4's - - base - - config.toml - - id: "base" - - tags: [] - - title: "" - - type: page - - path: Empty, Not meant to be visited - - comment: "Base page that others can extend from" - - extras - - index.html - - home - - config.toml - - id: "mainHome" - - tags: ["home"] - - title: "home" - - type: page - - path: "/" - - extras - - web stamps - - extra data yk - - index.html - - second - - config.toml - - id: "secondPage" - - tags: ["second", "2"] - - title: "bleh" - - type: page - - path: "/second" - - extras - - web stamps - - extra data yk - - index.html - - 404 - - config.toml - - id: "404page" - - tags: ["fallback"] - - title: "home" - - type: page - - path: "/404" - - index.html + - index.html + - assets/ + - img.png + - text.txt diff --git a/src/assets.rs b/src/assets.rs new file mode 100644 index 0000000..0219d8d --- /dev/null +++ b/src/assets.rs @@ -0,0 +1,34 @@ +use std::default; +use std::path::Path; +use std::path::PathBuf; + +use anyhow::anyhow; +use anyhow::Result; +use bytes::Bytes; +use smol::fs::File; +use smol::io::AsyncReadExt; + +#[derive(Default)] +pub struct AssetStore { + location: PathBuf, +} + +impl AssetStore { + pub fn new(path: PathBuf) -> AssetStore { + AssetStore { location: path } + } + pub async fn load_asset(&self, name: &str) -> Result { + let mut explodes = self.location.clone(); + explodes.push(name); + print!("{:#?}", explodes); + let file = File::open(explodes).await; + match file { + Ok(mut asset) => { + let mut vec: Vec = Vec::new(); + asset.read(&mut vec).await.unwrap(); + return Ok(Bytes::from(vec)); + } + Err(e) => return Err(e.into()), + } + } +} diff --git a/src/loader.rs b/src/loader.rs index 75c1450..38dfa33 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,6 +1,7 @@ use std::future::IntoFuture; use std::sync::Arc; +use crate::page::*; use crate::AppState; use anyhow::anyhow; use minijinja::Value; @@ -51,8 +52,10 @@ pub struct Root { impl AppState<'_> { pub async fn load_from_fs(&mut self) -> anyhow::Result<()> { + println!("mreow"); let mut dir = fs::read_dir("./template-serve").await?; + println!("mreow"); // Find config.toml from within dir let config = dir .find(|x| x.as_ref().unwrap().file_name().eq("config.toml")) @@ -62,6 +65,9 @@ impl AppState<'_> { while let Some(Ok(entry)) = dir.next().await { if entry.file_type().await?.is_dir() { let entry_opened = fs::read_dir(entry.path()).await?; + if entry.file_name().to_str().unwrap().chars().next().unwrap() == '_' { + continue; + } println!("Opened {}", entry.file_name().to_str().unwrap()); let mut config_path = entry.path(); config_path.push("config.toml"); @@ -113,11 +119,3 @@ impl AppState<'_> { Ok(()) } } - -// impl From for Value { -// fn from(value: Table) -> Self { -// Value { -// ..Default::default() -// } -// } -// } diff --git a/src/main.rs b/src/main.rs index 40d9de1..166cfe9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,13 @@ use smol::{fs, io, prelude::*, Async, Executor}; use smol_hyper::rt::{FuturesIo, SmolTimer}; use smol_macros::main; +mod assets; mod loader; +mod page; + +use crate::assets::AssetStore; use crate::loader::*; +use crate::page::*; struct AppState<'a> { pub pages: Vec, @@ -41,14 +46,21 @@ async fn serve(req: Request, state: Arc>) -> Result state .env .get_template(&x.header.id) .unwrap() .render(context! {}) - .unwrap(), - None => "?".to_string(), + .unwrap() + .into(), + None => match AssetStore::new("./templateServe/_assets/".into()) + .load_asset(path) + .await + { + Ok(asset) => asset, + Err(_) => "".into(), + }, }; Ok(Response::new(Full::new(reply.into()))) } diff --git a/src/page.rs b/src/page.rs new file mode 100644 index 0000000..5ec959d --- /dev/null +++ b/src/page.rs @@ -0,0 +1,39 @@ +use serde::Deserialize; + +pub struct Page { + pub header: Header, + // content: String, +} + +#[derive(Deserialize, Debug)] +pub enum PageType { + // If root, the dir contains all the pages, if there are no dirs within the root, we can assume + // its empty + Root, + // Contains a single html + Page, + // Is template + Base, +} + +#[derive(Deserialize, Debug, Clone)] +pub struct Header { + pub id: String, + // r#type: PageType, + r#type: String, + title: Option, + pub path: Option, + // TODO impl this later + // extras: Table, +} + +#[derive(Deserialize, Debug)] +pub struct Root { + id: String, + // r#type: PageType, + r#type: String, + pub title: Option, + pub path: Option, + // TODO impl this later + // extras: Table, +} diff --git a/template-serve/_assets/mroew.txt b/template-serve/_assets/mroew.txt new file mode 100644 index 0000000..79d8956 --- /dev/null +++ b/template-serve/_assets/mroew.txt @@ -0,0 +1 @@ +!!