i did some stuff
This commit is contained in:
56
bleh.txt
56
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
|
- 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
34
src/assets.rs
Normal 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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
use std::future::IntoFuture;
|
use std::future::IntoFuture;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::page::*;
|
||||||
use crate::AppState;
|
use crate::AppState;
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use minijinja::Value;
|
use minijinja::Value;
|
||||||
@@ -51,8 +52,10 @@ pub struct Root {
|
|||||||
|
|
||||||
impl AppState<'_> {
|
impl AppState<'_> {
|
||||||
pub async fn load_from_fs(&mut self) -> anyhow::Result<()> {
|
pub async fn load_from_fs(&mut self) -> anyhow::Result<()> {
|
||||||
|
println!("mreow");
|
||||||
let mut dir = fs::read_dir("./template-serve").await?;
|
let mut dir = fs::read_dir("./template-serve").await?;
|
||||||
|
|
||||||
|
println!("mreow");
|
||||||
// Find config.toml from within dir
|
// Find config.toml from within dir
|
||||||
let config = dir
|
let config = dir
|
||||||
.find(|x| x.as_ref().unwrap().file_name().eq("config.toml"))
|
.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 {
|
while let Some(Ok(entry)) = dir.next().await {
|
||||||
if entry.file_type().await?.is_dir() {
|
if entry.file_type().await?.is_dir() {
|
||||||
let entry_opened = fs::read_dir(entry.path()).await?;
|
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());
|
println!("Opened {}", entry.file_name().to_str().unwrap());
|
||||||
let mut config_path = entry.path();
|
let mut config_path = entry.path();
|
||||||
config_path.push("config.toml");
|
config_path.push("config.toml");
|
||||||
@@ -113,11 +119,3 @@ impl AppState<'_> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl From<Table> for Value {
|
|
||||||
// fn from(value: Table) -> Self {
|
|
||||||
// Value {
|
|
||||||
// ..Default::default()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
18
src/main.rs
18
src/main.rs
@@ -16,8 +16,13 @@ use smol::{fs, io, prelude::*, Async, Executor};
|
|||||||
use smol_hyper::rt::{FuturesIo, SmolTimer};
|
use smol_hyper::rt::{FuturesIo, SmolTimer};
|
||||||
use smol_macros::main;
|
use smol_macros::main;
|
||||||
|
|
||||||
|
mod assets;
|
||||||
mod loader;
|
mod loader;
|
||||||
|
mod page;
|
||||||
|
|
||||||
|
use crate::assets::AssetStore;
|
||||||
use crate::loader::*;
|
use crate::loader::*;
|
||||||
|
use crate::page::*;
|
||||||
|
|
||||||
struct AppState<'a> {
|
struct AppState<'a> {
|
||||||
pub pages: Vec<Page>,
|
pub pages: Vec<Page>,
|
||||||
@@ -41,14 +46,21 @@ async fn serve(req: Request<Incoming>, state: Arc<AppState<'_>>) -> Result<Respo
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let reply = match mreow {
|
let reply: Bytes = match mreow {
|
||||||
Some(x) => state
|
Some(x) => state
|
||||||
.env
|
.env
|
||||||
.get_template(&x.header.id)
|
.get_template(&x.header.id)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.render(context! {})
|
.render(context! {})
|
||||||
.unwrap(),
|
.unwrap()
|
||||||
None => "?".to_string(),
|
.into(),
|
||||||
|
None => match AssetStore::new("./templateServe/_assets/".into())
|
||||||
|
.load_asset(path)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(asset) => asset,
|
||||||
|
Err(_) => "".into(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Ok(Response::new(Full::new(reply.into())))
|
Ok(Response::new(Full::new(reply.into())))
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/page.rs
Normal file
39
src/page.rs
Normal 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,
|
||||||
|
}
|
||||||
1
template-serve/_assets/mroew.txt
Normal file
1
template-serve/_assets/mroew.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
!!
|
||||||
Reference in New Issue
Block a user