From 754a11504331e1e79895d2f8290590b0eb6f87fd Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Tue, 23 Jul 2024 15:08:29 +0200 Subject: [PATCH] what a turn of events --- axum_server/src/components/site_footer.rs | 10 ++++---- axum_server/src/pages/index.rs | 29 +++++++---------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/axum_server/src/components/site_footer.rs b/axum_server/src/components/site_footer.rs index 49e8b5c..80a61d4 100644 --- a/axum_server/src/components/site_footer.rs +++ b/axum_server/src/components/site_footer.rs @@ -1,4 +1,5 @@ use askama::Template; +use axum::http::StatusCode; use crate::{ pages::post::{PostMetadata, BLOG_POST_PATH}, @@ -12,10 +13,9 @@ pub struct SiteFooter { pub latest_posts: Vec>, } -pub async fn render_site_footer() -> SiteFooter { - let mut post_list = get_post_list::(BLOG_POST_PATH) - .await - .unwrap_or(vec![]); +// TODO remove whole site footer anyway +pub async fn render_site_footer() -> Result { + let mut post_list = get_post_list::(BLOG_POST_PATH).await?; post_list.sort_by_key(|post| post.metadata.date); post_list.reverse(); @@ -23,5 +23,5 @@ pub async fn render_site_footer() -> SiteFooter { .into_iter() .take(6) .collect::>>(); - SiteFooter { latest_posts } + Ok(SiteFooter { latest_posts }) } diff --git a/axum_server/src/pages/index.rs b/axum_server/src/pages/index.rs index 43c5828..1148599 100644 --- a/axum_server/src/pages/index.rs +++ b/axum_server/src/pages/index.rs @@ -1,5 +1,6 @@ use askama::Template; use axum::http::StatusCode; +use tokio::try_join; use crate::filters; use crate::{ @@ -27,27 +28,13 @@ pub struct IndexTemplate { } 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 - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??; - - let site_footer = site_footer - .await - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - - let featured_posts = featured_posts - .await - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??; - - let featured_projects = featured_projects - .await - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)??; - // TODO convert projects into cms + let (site_footer, blog_tags, featured_posts, featured_projects) = try_join!( + render_site_footer(), + get_popular_blog_tags(), + get_featured_posts(), + get_featured_projects() + ) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(IndexTemplate { site_footer,