i did some stuff

This commit is contained in:
2026-01-19 20:46:54 +00:00
parent 2f2d3073d8
commit d52c29d583
6 changed files with 107 additions and 57 deletions

View File

@@ -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

34
src/assets.rs Normal file
View File

@@ -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<Bytes> {
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<u8> = Vec::new();
asset.read(&mut vec).await.unwrap();
return Ok(Bytes::from(vec));
}
Err(e) => return Err(e.into()),
}
}
}

View File

@@ -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<Table> for Value {
// fn from(value: Table) -> Self {
// Value {
// ..Default::default()
// }
// }
// }

View File

@@ -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<Page>,
@@ -41,14 +46,21 @@ async fn serve(req: Request<Incoming>, state: Arc<AppState<'_>>) -> Result<Respo
false
}
});
let reply = match mreow {
let reply: Bytes = match mreow {
Some(x) => 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())))
}

39
src/page.rs Normal file
View File

@@ -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<String>,
pub path: Option<String>,
// TODO impl this later
// extras: Table,
}
#[derive(Deserialize, Debug)]
pub struct Root {
id: String,
// r#type: PageType,
r#type: String,
pub title: Option<String>,
pub path: Option<String>,
// TODO impl this later
// extras: Table,
}

View File

@@ -0,0 +1 @@
!!