diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/blog/mod.rs | 25 | ||||
-rw-r--r-- | src/blog/post.rs | 18 | ||||
-rw-r--r-- | src/main.rs | 18 | ||||
-rw-r--r-- | src/misc/mod.rs | 10 |
4 files changed, 62 insertions, 9 deletions
diff --git a/src/blog/mod.rs b/src/blog/mod.rs index 3640bcd..c227f22 100644 --- a/src/blog/mod.rs +++ b/src/blog/mod.rs @@ -2,6 +2,7 @@ pub mod post; pub mod handlers { use std::sync::Arc; +use color_eyre::eyre::eyre; use warp::{Reply, Rejection}; use warp::http::Response; @@ -9,13 +10,31 @@ use crate::templates::{self, Html, RenderRucte}; use crate::internal::SiteState; +use super::post::Post; + pub async fn list(state: Arc<SiteState>) -> Result<impl Reply, Rejection> { let state = state.clone(); Response::builder() - .html(|o| templates::index_html(o)) + .html(|o| templates::bloglist_html(o, state.blog.clone())) } - pub async fn post(name: String) -> Result<impl Reply, Rejection> { - Ok("Post test") + pub async fn post(name: String, state: Arc<SiteState>) -> Result<impl Reply, Rejection> { + let mut want: Option<Post> = None; + + for post in &state.blog { + if post.link == format!("blog/{}", name) { + want = Some(post.clone()); + break; + } + } + + match want { + Some(post) => { + let body = Html(post.body_html.clone()); + Response::builder() + .html(|o| templates::post_html(o, post, body)) + }, + None => panic!("No post found"), + } } } diff --git a/src/blog/post.rs b/src/blog/post.rs index abe57aa..3c27690 100644 --- a/src/blog/post.rs +++ b/src/blog/post.rs @@ -1,7 +1,7 @@ use std::{cmp::Ordering, path::PathBuf}; use glob::glob; use color_eyre::eyre::{Result, Context, eyre}; -use tokio::fs; +use tokio::{fs}; use chrono::prelude::*; @@ -11,6 +11,7 @@ pub struct Post { pub front_matter: frontmatter::Data, pub body_html: String, pub date: DateTime<FixedOffset>, + pub link: String, } impl Ord for Post { @@ -46,6 +47,7 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result<Post> { Ok(Post { front_matter, body_html, + link, date } ) } @@ -55,7 +57,19 @@ pub async fn load(dir: &str) -> Result<Vec<Post>> { .filter_map(Result::ok) .map(|fname| read_post(dir, fname)); - Ok(Vec::new()) + let mut result: Vec<Post> = futures::future::join_all(futs) + .await + .into_iter() + .map(Result::unwrap) + .collect(); + + if result.len() == 0 { + Err(eyre!("No posts found")) + } else { + result.sort(); + result.reverse(); + Ok(result) + } } mod frontmatter { diff --git a/src/main.rs b/src/main.rs index 04ab18e..e247211 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,12 @@ extern crate tracing; use color_eyre::eyre::Result; use std::{net::IpAddr, sync::Arc}; -use warp::Filter; +use warp::{Filter, path}; use std::str::FromStr; pub mod blog; +pub mod misc; mod internal; use internal::SiteState; @@ -24,15 +25,22 @@ async fn main() -> Result<()> { let state = Arc::new(internal::init().await?); + 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(warp::get()).and_then(blog::handlers::post)); + .and(give_site_state(state.clone())) + .and_then(blog::handlers::post), + ); - let blog = blog_list.or(blog_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 = blog.with(warp::log("site")); let server = warp::serve(site); @@ -51,3 +59,5 @@ async fn main() -> Result<()> { fn give_site_state(sitestate: Arc<SiteState>) -> impl Filter<Extract = (Arc<SiteState>,), Error=std::convert::Infallible> + Clone { warp::any().map(move || sitestate.clone()) } + +include!(concat!(env!("OUT_DIR"), "/templates.rs")); diff --git a/src/misc/mod.rs b/src/misc/mod.rs new file mode 100644 index 0000000..5942b8c --- /dev/null +++ b/src/misc/mod.rs @@ -0,0 +1,10 @@ +pub mod handlers { + use color_eyre::Result; + use warp::{Reply, Rejection, http::Response}; + use crate::templates::{self, Html, RenderRucte}; + + pub async fn index() -> Result<impl Reply, Rejection> { + Response::builder() + .html(|o| templates::index_html(o)) + } +} |