From 7b455b6152c52059eec584b50d43522de4366a14 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Wed, 22 Dec 2021 11:48:59 -0500 Subject: code: Fix routing and fill out post templates --- src/blog/post.rs | 17 ++++++++++++++--- src/main.rs | 17 ++++++----------- 2 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/blog/post.rs b/src/blog/post.rs index 3c27690..3e4a0d6 100644 --- a/src/blog/post.rs +++ b/src/blog/post.rs @@ -12,6 +12,8 @@ pub struct Post { pub body_html: String, pub date: DateTime, pub link: String, + pub author: String, + pub draft: bool, } impl Ord for Post { @@ -43,12 +45,16 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result { let date: DateTime = DateTime::::from_utc(NaiveDateTime::new(date, NaiveTime::from_hms(0,0,0)), Utc) .with_timezone(&Utc) .into(); + let author = &front_matter.clone().author.unwrap_or("Cara Salter".to_string()); + let draft = &front_matter.clone().draft.unwrap_or(false); Ok(Post { front_matter, body_html, - link, - date + link, + date, + author: author.clone(), + draft: draft.clone(), } ) } @@ -61,8 +67,11 @@ pub async fn load(dir: &str) -> Result> { .await .into_iter() .map(Result::unwrap) + .filter(|p| !p.draft) .collect(); + info!("Loaded {:?} posts", result.len()); + if result.len() == 0 { Err(eyre!("No posts found")) } else { @@ -81,6 +90,8 @@ mod frontmatter { pub date: String, pub series: Option>, pub tags: Option>, + pub author: Option, + pub draft: Option, } enum ParseState { @@ -134,7 +145,7 @@ mod frontmatter { } } _ => { - panic!("Malformed frontmatter"); + panic!("Malformed frontmatter: {:?}", input); } }, ParseState::SkipNewLine { end } => match ch { diff --git a/src/main.rs b/src/main.rs index e247211..0726f87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,20 +27,15 @@ async fn main() -> Result<()> { let index = warp::get().and(path::end().and_then(misc::handlers::index)); - let blog_base = warp::path!("blog" / ..); - let blog_list = blog_base.and(give_site_state(state.clone())).and_then(blog::handlers::list); - let blog_post = blog_base.and( - warp::path!(String) - .and(give_site_state(state.clone())) - .and_then(blog::handlers::post), - ); + let blog_index = warp::path!("blog").and(give_site_state(state.clone())).and_then(blog::handlers::list); + + let blog_post = warp::path!("blog" / String).and(give_site_state(state.clone())).and_then(blog::handlers::post); + + let static_files = warp::path("static") .and(warp::fs::dir("./statics")); - let site = index - .or(blog_list.or(blog_post)) - .or(static_files).with(warp::log("site")); - + let site = index.or(blog_index.or(blog_post)).or(static_files).with(warp::log("site")); let server = warp::serve(site); -- cgit v1.2.3