diff --git a/axum_server/src/components/mod.rs b/axum_server/src/components/mod.rs new file mode 100644 index 0000000..20389a4 --- /dev/null +++ b/axum_server/src/components/mod.rs @@ -0,0 +1 @@ +pub mod site_footer; diff --git a/axum_server/src/components/site_footer.rs b/axum_server/src/components/site_footer.rs new file mode 100644 index 0000000..0b2dd4b --- /dev/null +++ b/axum_server/src/components/site_footer.rs @@ -0,0 +1,21 @@ +use askama::Template; + +use crate::{pages::post::PostMetadata, post_list::get_post_list, post_parser::ParseResult}; + +#[derive(Template)] +#[template(path = "site_footer.html")] +pub struct SiteFooter { + pub latest_posts: Vec>, +} + +pub async fn render_site_footer() -> SiteFooter { + let mut post_list = get_post_list::().await.unwrap_or(vec![]); + post_list.sort_by_key(|post| post.metadata.date); + post_list.reverse(); + + let latest_posts = post_list + .into_iter() + .take(6) + .collect::>>(); + SiteFooter { latest_posts } +} diff --git a/axum_server/src/main.rs b/axum_server/src/main.rs index dcb3547..e1c8d5d 100644 --- a/axum_server/src/main.rs +++ b/axum_server/src/main.rs @@ -1,8 +1,9 @@ -use askama::filters::format; use axum; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +mod components; mod pages; +mod post_list; mod post_parser; mod router; // mod template; diff --git a/axum_server/src/pages/index.rs b/axum_server/src/pages/index.rs index fccf926..297730f 100644 --- a/axum_server/src/pages/index.rs +++ b/axum_server/src/pages/index.rs @@ -1,9 +1,14 @@ use askama::Template; +use crate::components::site_footer::{render_site_footer, SiteFooter}; + #[derive(Template)] #[template(path = "index.html")] -pub struct IndexTemplate {} +pub struct IndexTemplate { + site_footer: SiteFooter, +} pub async fn render_index() -> IndexTemplate { - IndexTemplate {} + let site_footer = render_site_footer().await; + IndexTemplate { site_footer } } diff --git a/axum_server/src/pages/post.rs b/axum_server/src/pages/post.rs index f4ecd43..58e4306 100644 --- a/axum_server/src/pages/post.rs +++ b/axum_server/src/pages/post.rs @@ -3,7 +3,10 @@ use axum::{extract::Path, http::StatusCode}; use chrono::{DateTime, Utc}; use serde::Deserialize; -use crate::post_parser::{deserialize_date, parse_post}; +use crate::{ + components::site_footer::{render_site_footer, SiteFooter}, + post_parser::{deserialize_date, parse_post}, +}; #[derive(Deserialize, Debug)] pub struct PostMetadata { @@ -22,13 +25,20 @@ pub struct PostMetadata { pub struct PostTemplate { pub title: String, pub body: String, + pub site_footer: SiteFooter, } pub async fn render_post(Path(post_id): Path) -> Result { + let site_footer = tokio::spawn(render_site_footer()); let path = format!("../_posts/blog/{}.md", post_id); let parsed = parse_post::(&path).await?; + + let site_footer = site_footer + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(PostTemplate { title: parsed.metadata.title, body: parsed.body, + site_footer, }) } diff --git a/axum_server/src/pages/post_list.rs b/axum_server/src/pages/post_list.rs index 8d27288..4a88d3e 100644 --- a/axum_server/src/pages/post_list.rs +++ b/axum_server/src/pages/post_list.rs @@ -1,9 +1,11 @@ use askama::Template; use axum::{extract::Path, http::StatusCode}; -use tokio::fs::read_dir; -use tracing::info; -use crate::post_parser::{parse_post, ParseResult}; +use crate::{ + components::site_footer::{render_site_footer, SiteFooter}, + post_list::get_post_list, + post_parser::ParseResult, +}; use super::post::PostMetadata; @@ -13,32 +15,20 @@ pub struct PostListTemplate { pub title: String, pub posts: Vec>, pub tag: Option, + pub site_footer: SiteFooter, } pub async fn render_post_list(tag: Option>) -> Result { // I will forget what happens here in a week. But essentially it's pattern matching and shadowing let tag = tag.map(|Path(tag)| tag); - let path = "../_posts/blog/"; - let mut dir = read_dir(path) - .await - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - let mut posts: Vec> = Vec::new(); + let site_footer = tokio::spawn(render_site_footer()); + let mut post_list = get_post_list::().await?; + post_list.sort_by_key(|post| post.metadata.date); + post_list.reverse(); - while let Some(file) = dir - .next_entry() - .await - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? - { - let file_path = file.path(); - let file_path_str = file_path.to_str().unwrap(); - info!(":{}", file_path_str); - let post = parse_post::(file_path_str).await?; - posts.push(post); - } - - let mut posts = match &tag { - Some(tag) => posts + let posts = match &tag { + Some(tag) => post_list .into_iter() .filter(|post| { post.metadata @@ -49,23 +39,18 @@ pub async fn render_post_list(tag: Option>) -> Result posts, + None => post_list, }; - if std::env::var("TARGET") - .unwrap_or_else(|_| "DEV".to_owned()) - .eq("PROD") - { - posts = posts - .into_iter() - .filter(|post| !post.slug.starts_with("dev")) - .collect() - } + let site_footer = site_footer + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(PostListTemplate { title: "Posts".to_owned(), posts, tag, + site_footer, }) } diff --git a/axum_server/src/post_list.rs b/axum_server/src/post_list.rs new file mode 100644 index 0000000..ed15a83 --- /dev/null +++ b/axum_server/src/post_list.rs @@ -0,0 +1,39 @@ +use axum::http::StatusCode; +use serde::de::DeserializeOwned; +use tokio::fs::read_dir; +use tracing::info; + +use crate::post_parser::{parse_post, ParseResult}; + +pub async fn get_post_list<'de, Metadata: DeserializeOwned>( +) -> Result>, StatusCode> { + let path = "../_posts/blog/"; + let mut dir = read_dir(path) + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let mut posts: Vec> = Vec::new(); + + while let Some(file) = dir + .next_entry() + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? + { + let file_path = file.path(); + let file_path_str = file_path.to_str().unwrap(); + info!(":{}", file_path_str); + let post = parse_post::(file_path_str).await?; + posts.push(post); + } + + if std::env::var("TARGET") + .unwrap_or_else(|_| "DEV".to_owned()) + .eq("PROD") + { + posts = posts + .into_iter() + .filter(|post| !post.slug.starts_with("dev")) + .collect() + } + + return Ok(posts); +} diff --git a/axum_server/templates/base.html b/axum_server/templates/base.html index bd1df63..c61b693 100644 --- a/axum_server/templates/base.html +++ b/axum_server/templates/base.html @@ -31,7 +31,8 @@ - {% block content %} Placeholder {% endblock %} - {# footer, should be not dependant on the each individual handler but it should have it's own handler #} + {% block content %} Placeholder {% endblock %} {# footer, should be not + dependant on the each individual handler but it should have it's own handler + #} {{ site_footer|safe }} diff --git a/axum_server/templates/site_footer.html b/axum_server/templates/site_footer.html new file mode 100644 index 0000000..60cd712 --- /dev/null +++ b/axum_server/templates/site_footer.html @@ -0,0 +1,7 @@ +