diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index c429e8a..29716e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,18 @@ mod error; mod fs; +mod markup; mod settings; mod templated; use std::{os::unix::fs::MetadataExt, str::FromStr}; use axum::{ - body::Body, extract::Path, http::header, response::Response, routing::get, Extension, Router, + body::Body, + extract::Path, + http::{header, StatusCode}, + response::Response, + routing::get, + Extension, Router, }; use bempline::{Document, Options}; use camino::Utf8PathBuf; @@ -15,7 +21,10 @@ use fs::Filesystem; use settings::Settings; use tokio_util::io::ReaderStream; -use crate::templated::Templated; +use crate::{ + fs::{PathResolution, Webpath}, + templated::Templated, +}; #[tokio::main] async fn main() { @@ -59,11 +68,19 @@ async fn falible_handler( ) -> Result<Response, RuntimeError> { println!("raw = {path}"); - let path = path.parse()?; + let webpath: Webpath = path.parse()?; println!("path = {path}"); - let filepath = fs.resolve(&path)?; + let PathResolution { + filepath, + is_dirfile, + } = fs.resolve(&webpath)?; + + if !webpath.is_dir() && is_dirfile { + println!("as_dir = {}", webpath.as_dir()); + return Ok(redirect(webpath.as_dir())); + } let ext = filepath.extension().unwrap_or_default(); @@ -81,6 +98,16 @@ async fn falible_handler( } } +fn redirect<S: Into<String>>(redirection: S) -> Response { + let location = redirection.into(); + println!("redirecting to {location}"); + Response::builder() + .status(StatusCode::TEMPORARY_REDIRECT) + .header(header::LOCATION, &location) + .body(Body::new(format!("redirecting to {location}"))) + .unwrap() +} + // 20 megabytes const STREAM_AFTER: u64 = 20 * 1024 * 1024; @@ -142,6 +169,13 @@ async fn send_template( templated.frontmatter.get("title").unwrap_or(filename), ); + let style_pattern = template.get_pattern("styles").unwrap(); + for style in templated.frontmatter.get_many("style") { + let mut pat = style_pattern.clone(); + pat.set("style", style); + template.set_pattern("styles", pat); + } + template.set("main", templated.content); Ok(Response::builder() |