From 1861a85e76a6cda5e90be9d55292ef1b749e09bd Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Sun, 21 Jul 2024 22:42:54 +0200 Subject: [PATCH] showcase projects loading and displaying --- axum_server/src/components/site_footer.rs | 10 ++- axum_server/src/featured_posts.rs | 8 ++- axum_server/src/featured_projects.rs | 13 ++++ axum_server/src/feed.rs | 5 +- axum_server/src/main.rs | 3 + axum_server/src/pages/index.rs | 10 +++ axum_server/src/pages/mod.rs | 1 + axum_server/src/pages/post.rs | 2 + axum_server/src/pages/post_list.rs | 4 +- axum_server/src/pages/project.rs | 12 ++++ axum_server/src/post_list.rs | 3 +- axum_server/src/project_list.rs | 42 +++++++++++++ axum_server/src/tag_list.rs | 7 ++- axum_server/styles/output.css | 5 ++ .../templates/components/post_preview.html | 1 + .../components/project_preview_card.html | 37 +++++++++++ axum_server/templates/index.html | 37 +++++------ static/resources/config.yml | 63 ++++++++++++++----- 18 files changed, 215 insertions(+), 48 deletions(-) create mode 100644 axum_server/src/featured_projects.rs create mode 100644 axum_server/src/pages/project.rs create mode 100644 axum_server/src/project_list.rs create mode 100644 axum_server/templates/components/project_preview_card.html diff --git a/axum_server/src/components/site_footer.rs b/axum_server/src/components/site_footer.rs index 0b2dd4b..49e8b5c 100644 --- a/axum_server/src/components/site_footer.rs +++ b/axum_server/src/components/site_footer.rs @@ -1,6 +1,10 @@ use askama::Template; -use crate::{pages::post::PostMetadata, post_list::get_post_list, post_parser::ParseResult}; +use crate::{ + pages::post::{PostMetadata, BLOG_POST_PATH}, + post_list::get_post_list, + post_parser::ParseResult, +}; #[derive(Template)] #[template(path = "site_footer.html")] @@ -9,7 +13,9 @@ pub struct SiteFooter { } pub async fn render_site_footer() -> SiteFooter { - let mut post_list = get_post_list::().await.unwrap_or(vec![]); + let mut post_list = get_post_list::(BLOG_POST_PATH) + .await + .unwrap_or(vec![]); post_list.sort_by_key(|post| post.metadata.date); post_list.reverse(); diff --git a/axum_server/src/featured_posts.rs b/axum_server/src/featured_posts.rs index c4415c1..79067b0 100644 --- a/axum_server/src/featured_posts.rs +++ b/axum_server/src/featured_posts.rs @@ -1,8 +1,12 @@ -use crate::{pages::post::PostMetadata, post_list::get_post_list, post_parser::ParseResult}; +use crate::{ + pages::post::{PostMetadata, BLOG_POST_PATH}, + post_list::get_post_list, + post_parser::ParseResult, +}; use axum::http::StatusCode; pub async fn get_featured_posts() -> Result>, StatusCode> { - let post_list = get_post_list::().await?; + let post_list = get_post_list::(BLOG_POST_PATH).await?; let featured_posts = post_list .into_iter() diff --git a/axum_server/src/featured_projects.rs b/axum_server/src/featured_projects.rs new file mode 100644 index 0000000..99a1719 --- /dev/null +++ b/axum_server/src/featured_projects.rs @@ -0,0 +1,13 @@ +use crate::{pages::project::ProjectMetadata, post_list::get_post_list, post_parser::ParseResult}; +use axum::http::StatusCode; + +pub async fn get_featured_projects() -> Result>, StatusCode> { + let project_list = get_post_list::("../_projects").await?; + + let featured_projects = project_list + .into_iter() + .filter(|post| post.metadata.featured) + .collect(); + + Ok(featured_projects) +} diff --git a/axum_server/src/feed.rs b/axum_server/src/feed.rs index 9a81575..7d67498 100644 --- a/axum_server/src/feed.rs +++ b/axum_server/src/feed.rs @@ -3,10 +3,13 @@ use axum::response::IntoResponse; use chrono::Utc; use rss::{ChannelBuilder, GuidBuilder, Item, ItemBuilder}; +use crate::pages::post::BLOG_POST_PATH; use crate::{pages::post::PostMetadata, post_list::get_post_list}; pub async fn render_rss_feed() -> Result { - let mut post_list = get_post_list::().await.unwrap_or(vec![]); + let mut post_list = get_post_list::(BLOG_POST_PATH) + .await + .unwrap_or(vec![]); post_list.sort_by_key(|post| post.metadata.date); post_list.reverse(); diff --git a/axum_server/src/main.rs b/axum_server/src/main.rs index 761a315..debc465 100644 --- a/axum_server/src/main.rs +++ b/axum_server/src/main.rs @@ -5,10 +5,13 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; mod components; mod featured_posts; +mod featured_projects; mod feed; mod filters; mod pages; mod post_list; +// mod project_list; +// TODO make post and project modules mod post_parser; mod router; mod tag_list; diff --git a/axum_server/src/pages/index.rs b/axum_server/src/pages/index.rs index 220f6c1..43c5828 100644 --- a/axum_server/src/pages/index.rs +++ b/axum_server/src/pages/index.rs @@ -8,11 +8,13 @@ use crate::{ site_header::HeaderProps, }, featured_posts::get_featured_posts, + featured_projects::get_featured_projects, post_parser::ParseResult, tag_list::get_popular_blog_tags, }; use super::post::PostMetadata; +use super::project::ProjectMetadata; #[derive(Template)] #[template(path = "index.html")] @@ -21,12 +23,14 @@ pub struct IndexTemplate { header_props: HeaderProps, blog_tags: Vec, featured_posts: Vec>, + featured_projects: Vec>, } pub async fn render_index() -> Result { let site_footer = tokio::spawn(render_site_footer()); let blog_tags = tokio::spawn(get_popular_blog_tags()); let featured_posts = tokio::spawn(get_featured_posts()); + let featured_projects = tokio::spawn(get_featured_projects()); let blog_tags = blog_tags .await @@ -40,10 +44,16 @@ pub async fn render_index() -> Result { .await .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??; + let featured_projects = featured_projects + .await + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??; + // TODO convert projects into cms + Ok(IndexTemplate { site_footer, header_props: HeaderProps::default(), blog_tags, featured_posts, + featured_projects, }) } diff --git a/axum_server/src/pages/mod.rs b/axum_server/src/pages/mod.rs index dc99cba..7d686e4 100644 --- a/axum_server/src/pages/mod.rs +++ b/axum_server/src/pages/mod.rs @@ -3,3 +3,4 @@ pub mod contact; pub mod index; pub mod post; pub mod post_list; +pub mod project; diff --git a/axum_server/src/pages/post.rs b/axum_server/src/pages/post.rs index d6b1618..b98484e 100644 --- a/axum_server/src/pages/post.rs +++ b/axum_server/src/pages/post.rs @@ -12,6 +12,8 @@ use crate::{ post_parser::{deserialize_date, parse_post}, }; +pub const BLOG_POST_PATH: &str = "../_posts/blog"; + #[derive(Deserialize, Debug)] pub struct PostMetadata { pub layout: String, diff --git a/axum_server/src/pages/post_list.rs b/axum_server/src/pages/post_list.rs index f2599cc..9352367 100644 --- a/axum_server/src/pages/post_list.rs +++ b/axum_server/src/pages/post_list.rs @@ -11,7 +11,7 @@ use crate::{ post_parser::ParseResult, }; -use super::post::PostMetadata; +use super::post::{PostMetadata, BLOG_POST_PATH}; #[derive(Template)] #[template(path = "post_list.html")] @@ -28,7 +28,7 @@ pub async fn render_post_list(tag: Option>) -> Result().await?; + let mut post_list = get_post_list::(BLOG_POST_PATH).await?; post_list.sort_by_key(|post| post.metadata.date); post_list.reverse(); diff --git a/axum_server/src/pages/project.rs b/axum_server/src/pages/project.rs new file mode 100644 index 0000000..1db4a97 --- /dev/null +++ b/axum_server/src/pages/project.rs @@ -0,0 +1,12 @@ +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct ProjectMetadata { + pub title: String, + pub description: String, + pub classification: String, + pub displayed: bool, + pub cover_image: Option, + pub tags: Vec, + pub featured: bool, +} diff --git a/axum_server/src/post_list.rs b/axum_server/src/post_list.rs index ed15a83..6588c75 100644 --- a/axum_server/src/post_list.rs +++ b/axum_server/src/post_list.rs @@ -6,8 +6,9 @@ use tracing::info; use crate::post_parser::{parse_post, ParseResult}; pub async fn get_post_list<'de, Metadata: DeserializeOwned>( + path: &str, ) -> Result>, StatusCode> { - let path = "../_posts/blog/"; + // let path = "../_posts/blog/"; let mut dir = read_dir(path) .await .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; diff --git a/axum_server/src/project_list.rs b/axum_server/src/project_list.rs new file mode 100644 index 0000000..7a871d5 --- /dev/null +++ b/axum_server/src/project_list.rs @@ -0,0 +1,42 @@ +use std::path::Path; + +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>( + path: &Path, +) -> 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/src/tag_list.rs b/axum_server/src/tag_list.rs index 3a0f108..3cbbfca 100644 --- a/axum_server/src/tag_list.rs +++ b/axum_server/src/tag_list.rs @@ -1,4 +1,7 @@ -use crate::{pages::post::PostMetadata, post_list::get_post_list}; +use crate::{ + pages::post::{PostMetadata, BLOG_POST_PATH}, + post_list::get_post_list, +}; use axum::http::StatusCode; use std::collections::HashMap; use tracing::debug; @@ -6,7 +9,7 @@ use tracing::debug; pub async fn get_popular_blog_tags() -> Result, StatusCode> { const TAGS_LENGTH: usize = 7; - let post_list = get_post_list::().await?; + let post_list = get_post_list::(BLOG_POST_PATH).await?; let tags_sum = post_list .into_iter() .flat_map(|post| post.metadata.tags) diff --git a/axum_server/styles/output.css b/axum_server/styles/output.css index eb391a9..03151dc 100644 --- a/axum_server/styles/output.css +++ b/axum_server/styles/output.css @@ -619,6 +619,11 @@ video { margin-bottom: 1.5rem; } +.my-2 { + margin-top: 0.5rem; + margin-bottom: 0.5rem; +} + .mb-1 { margin-bottom: 0.25rem; } diff --git a/axum_server/templates/components/post_preview.html b/axum_server/templates/components/post_preview.html index 72cf6e0..ba4378b 100644 --- a/axum_server/templates/components/post_preview.html +++ b/axum_server/templates/components/post_preview.html @@ -1,5 +1,6 @@