showcase projects loading and displaying

This commit is contained in:
2024-07-21 22:42:54 +02:00
parent 65bb29f36b
commit 1861a85e76
18 changed files with 215 additions and 48 deletions

View File

@ -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::<PostMetadata>().await.unwrap_or(vec![]);
let mut post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH)
.await
.unwrap_or(vec![]);
post_list.sort_by_key(|post| post.metadata.date);
post_list.reverse();

View File

@ -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<Vec<ParseResult<PostMetadata>>, StatusCode> {
let post_list = get_post_list::<PostMetadata>().await?;
let post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH).await?;
let featured_posts = post_list
.into_iter()

View File

@ -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<Vec<ParseResult<ProjectMetadata>>, StatusCode> {
let project_list = get_post_list::<ProjectMetadata>("../_projects").await?;
let featured_projects = project_list
.into_iter()
.filter(|post| post.metadata.featured)
.collect();
Ok(featured_projects)
}

View File

@ -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<impl IntoResponse, StatusCode> {
let mut post_list = get_post_list::<PostMetadata>().await.unwrap_or(vec![]);
let mut post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH)
.await
.unwrap_or(vec![]);
post_list.sort_by_key(|post| post.metadata.date);
post_list.reverse();

View File

@ -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;

View File

@ -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<String>,
featured_posts: Vec<ParseResult<PostMetadata>>,
featured_projects: Vec<ParseResult<ProjectMetadata>>,
}
pub async fn render_index() -> Result<IndexTemplate, StatusCode> {
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<IndexTemplate, StatusCode> {
.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,
})
}

View File

@ -3,3 +3,4 @@ pub mod contact;
pub mod index;
pub mod post;
pub mod post_list;
pub mod project;

View File

@ -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,

View File

@ -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<Path<String>>) -> Result<PostListTempl
let tag = tag.map(|Path(tag)| tag);
let site_footer = tokio::spawn(render_site_footer());
let mut post_list = get_post_list::<PostMetadata>().await?;
let mut post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH).await?;
post_list.sort_by_key(|post| post.metadata.date);
post_list.reverse();

View File

@ -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<String>,
pub tags: Vec<String>,
pub featured: bool,
}

View File

@ -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<Vec<ParseResult<Metadata>>, StatusCode> {
let path = "../_posts/blog/";
// let path = "../_posts/blog/";
let mut dir = read_dir(path)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

View File

@ -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<Vec<ParseResult<Metadata>>, StatusCode> {
// let path = "../_posts/blog/";
let mut dir = read_dir(path)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let mut posts: Vec<ParseResult<Metadata>> = 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::<Metadata>(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);
}

View File

@ -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<Vec<String>, StatusCode> {
const TAGS_LENGTH: usize = 7;
let post_list = get_post_list::<PostMetadata>().await?;
let post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH).await?;
let tags_sum = post_list
.into_iter()
.flat_map(|post| post.metadata.tags)