refactor modules

This commit is contained in:
Michal Vanko 2024-08-07 13:18:13 +02:00
parent 6dc6a581e3
commit 5365cb5409
24 changed files with 139 additions and 130 deletions

View File

@ -20,7 +20,10 @@ decap_server:
# Run dev server in watch mode # Run dev server in watch mode
dev: dev:
(just server_dev; just tailwind) | parallel #!/usr/bin/env -S parallel --shebang --ungroup --jobs {{ num_cpus() }}
just server_dev
just tailwind
just decap_server
# Run dev server in watch mode # Run dev server in watch mode
test: test:

View File

@ -0,0 +1,18 @@
use chrono::{DateTime, Utc};
use serde::Deserialize;
use crate::post_utils::post_parser::deserialize_date;
pub const BLOG_POST_PATH: &str = "../_posts/blog";
#[derive(Deserialize, Debug)]
pub struct BlogPostMetadata {
pub layout: String,
pub title: String,
pub segments: Vec<String>,
pub published: bool,
#[serde(deserialize_with = "deserialize_date")]
pub date: DateTime<Utc>,
pub thumbnail: Option<String>,
pub tags: Vec<String>,
}

View File

@ -0,0 +1,16 @@
use axum::http::StatusCode;
use crate::post_utils::{post_listing::get_post_list, post_parser::ParseResult};
use super::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH};
pub async fn get_featured_blog_posts() -> Result<Vec<ParseResult<BlogPostMetadata>>, StatusCode> {
let post_list = get_post_list::<BlogPostMetadata>(BLOG_POST_PATH).await?;
let featured_posts = post_list
.into_iter()
.filter(|post| post.metadata.segments.contains(&"featured".to_string()))
.collect();
Ok(featured_posts)
}

View File

@ -0,0 +1,3 @@
pub mod blog_post_model;
pub mod featured_blog_posts;
pub mod tag_list;

View File

@ -1,15 +1,16 @@
use crate::{
pages::post::{PostMetadata, BLOG_POST_PATH},
post_list::get_post_list,
};
use axum::http::StatusCode; use axum::http::StatusCode;
use std::collections::HashMap; use std::collections::HashMap;
use tracing::debug; use tracing::debug;
use crate::{
blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH},
post_utils::post_listing::get_post_list,
};
pub async fn get_popular_blog_tags() -> Result<Vec<String>, StatusCode> { pub async fn get_popular_blog_tags() -> Result<Vec<String>, StatusCode> {
const TAGS_LENGTH: usize = 7; const TAGS_LENGTH: usize = 7;
let post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH).await?; let post_list = get_post_list::<BlogPostMetadata>(BLOG_POST_PATH).await?;
let tags_sum = post_list let tags_sum = post_list
.into_iter() .into_iter()
.flat_map(|post| post.metadata.tags) .flat_map(|post| post.metadata.tags)

View File

@ -2,26 +2,25 @@ use askama::Template;
use axum::http::StatusCode; use axum::http::StatusCode;
use crate::{ use crate::{
pages::post::{PostMetadata, BLOG_POST_PATH}, blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH},
post_list::get_post_list, post_utils::{post_listing::get_post_list, post_parser::ParseResult},
post_parser::ParseResult,
}; };
#[derive(Template)] #[derive(Template)]
#[template(path = "site_footer.html")] #[template(path = "site_footer.html")]
pub struct SiteFooter { pub struct SiteFooter {
pub latest_posts: Vec<ParseResult<PostMetadata>>, pub latest_posts: Vec<ParseResult<BlogPostMetadata>>,
} }
// TODO remove whole site footer anyway // TODO remove whole site footer anyway
pub async fn render_site_footer() -> Result<SiteFooter, StatusCode> { pub async fn render_site_footer() -> Result<SiteFooter, StatusCode> {
let mut post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH).await?; let mut post_list = get_post_list::<BlogPostMetadata>(BLOG_POST_PATH).await?;
post_list.sort_by_key(|post| post.metadata.date); post_list.sort_by_key(|post| post.metadata.date);
post_list.reverse(); post_list.reverse();
let latest_posts = post_list let latest_posts = post_list
.into_iter() .into_iter()
.take(6) .take(6)
.collect::<Vec<ParseResult<PostMetadata>>>(); .collect::<Vec<ParseResult<BlogPostMetadata>>>();
Ok(SiteFooter { latest_posts }) Ok(SiteFooter { latest_posts })
} }

View File

@ -1,17 +0,0 @@
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>(BLOG_POST_PATH).await?;
let featured_posts = post_list
.into_iter()
.filter(|post| post.metadata.segments.contains(&"featured".to_string()))
.collect();
Ok(featured_posts)
}

View File

@ -3,11 +3,11 @@ use axum::response::IntoResponse;
use chrono::Utc; use chrono::Utc;
use rss::{ChannelBuilder, GuidBuilder, Item, ItemBuilder}; use rss::{ChannelBuilder, GuidBuilder, Item, ItemBuilder};
use crate::pages::post::BLOG_POST_PATH; use crate::blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH};
use crate::{pages::post::PostMetadata, post_list::get_post_list}; use crate::post_utils::post_listing::get_post_list;
pub async fn render_rss_feed() -> Result<impl IntoResponse, StatusCode> { pub async fn render_rss_feed() -> Result<impl IntoResponse, StatusCode> {
let mut post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH) let mut post_list = get_post_list::<BlogPostMetadata>(BLOG_POST_PATH)
.await .await
.unwrap_or(vec![]); .unwrap_or(vec![]);
post_list.sort_by_key(|post| post.metadata.date); post_list.sort_by_key(|post| post.metadata.date);

View File

@ -3,18 +3,14 @@ use tower_http::services::ServeDir;
use tower_livereload::LiveReloadLayer; use tower_livereload::LiveReloadLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod blog_posts;
mod components; mod components;
mod featured_posts;
mod featured_projects;
mod feed; mod feed;
mod filters; mod filters;
mod pages; mod pages;
mod post_list; mod post_utils;
// mod project_list; mod projects;
// TODO make post and project modules
mod post_parser;
mod router; mod router;
mod tag_list;
// mod template; // mod template;
#[tokio::main] #[tokio::main]

View File

@ -2,33 +2,33 @@ use askama::Template;
use axum::{extract::Path, http::StatusCode}; use axum::{extract::Path, http::StatusCode};
use crate::{ use crate::{
blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH},
components::{ components::{
site_footer::{render_site_footer, SiteFooter}, site_footer::{render_site_footer, SiteFooter},
site_header::{HeaderProps, Link}, site_header::{HeaderProps, Link},
}, },
filters, filters,
post_list::get_post_list, post_utils::{post_listing::get_post_list, post_parser::ParseResult},
post_parser::ParseResult,
}; };
use super::post::{PostMetadata, BLOG_POST_PATH};
#[derive(Template)] #[derive(Template)]
#[template(path = "post_list.html")] #[template(path = "post_list.html")]
pub struct PostListTemplate { pub struct PostListTemplate {
pub title: String, pub title: String,
pub posts: Vec<ParseResult<PostMetadata>>, pub posts: Vec<ParseResult<BlogPostMetadata>>,
pub tag: Option<String>, pub tag: Option<String>,
pub site_footer: SiteFooter, pub site_footer: SiteFooter,
pub header_props: HeaderProps, pub header_props: HeaderProps,
} }
pub async fn render_post_list(tag: Option<Path<String>>) -> Result<PostListTemplate, StatusCode> { pub async fn render_blog_post_list(
tag: Option<Path<String>>,
) -> Result<PostListTemplate, StatusCode> {
// I will forget what happens here in a week. But essentially it's pattern matching and shadowing // 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 tag = tag.map(|Path(tag)| tag);
let site_footer = render_site_footer().await?; let site_footer = render_site_footer().await?;
let mut post_list = get_post_list::<PostMetadata>(BLOG_POST_PATH).await?; let mut post_list = get_post_list::<BlogPostMetadata>(BLOG_POST_PATH).await?;
post_list.sort_by_key(|post| post.metadata.date); post_list.sort_by_key(|post| post.metadata.date);
post_list.reverse(); post_list.reverse();

View File

@ -0,0 +1,43 @@
use askama::Template;
use axum::{extract::Path, http::StatusCode};
use chrono::{DateTime, Utc};
use tokio::try_join;
use crate::{
blog_posts::blog_post_model::BlogPostMetadata, components::site_header::Link, filters,
post_utils::post_parser::parse_post,
};
use crate::components::{
site_footer::{render_site_footer, SiteFooter},
site_header::HeaderProps,
};
#[derive(Template)]
#[template(path = "blog_post.html")]
pub struct BlogPostTemplate {
pub title: String,
pub body: String,
pub date: DateTime<Utc>,
pub tags: Vec<String>,
pub site_footer: SiteFooter,
pub header_props: HeaderProps,
}
pub async fn render_blog_post(Path(post_id): Path<String>) -> Result<BlogPostTemplate, StatusCode> {
let path = format!("../_posts/blog/{}.md", post_id);
let parse_post = parse_post::<BlogPostMetadata>(&path);
let (site_footer, parsed) = try_join!(render_site_footer(), parse_post)?;
Ok(BlogPostTemplate {
title: parsed.metadata.title,
date: parsed.metadata.date,
tags: parsed.metadata.tags,
body: parsed.body,
site_footer,
header_props: HeaderProps::with_back_link(Link {
href: "/blog".to_string(),
label: "All posts".to_string(),
}),
})
}

View File

@ -2,36 +2,35 @@ use askama::Template;
use axum::http::StatusCode; use axum::http::StatusCode;
use tokio::try_join; use tokio::try_join;
use crate::filters;
use crate::{ use crate::{
blog_posts::{
blog_post_model::BlogPostMetadata, featured_blog_posts::get_featured_blog_posts,
tag_list::get_popular_blog_tags,
},
components::{ components::{
site_footer::{render_site_footer, SiteFooter}, site_footer::{render_site_footer, SiteFooter},
site_header::HeaderProps, site_header::HeaderProps,
}, },
featured_posts::get_featured_posts, filters,
featured_projects::get_featured_projects, post_utils::post_parser::ParseResult,
post_parser::ParseResult, projects::{featured_projects::get_featured_projects, project_model::ProjectMetadata},
tag_list::get_popular_blog_tags,
}; };
use super::post::PostMetadata;
use super::project::ProjectMetadata;
#[derive(Template)] #[derive(Template)]
#[template(path = "index.html")] #[template(path = "index.html")]
pub struct IndexTemplate { pub struct IndexTemplate {
site_footer: SiteFooter, site_footer: SiteFooter,
header_props: HeaderProps, header_props: HeaderProps,
blog_tags: Vec<String>, blog_tags: Vec<String>,
featured_posts: Vec<ParseResult<PostMetadata>>, featured_blog_posts: Vec<ParseResult<BlogPostMetadata>>,
featured_projects: Vec<ParseResult<ProjectMetadata>>, featured_projects: Vec<ParseResult<ProjectMetadata>>,
} }
pub async fn render_index() -> Result<IndexTemplate, StatusCode> { pub async fn render_index() -> Result<IndexTemplate, StatusCode> {
let (site_footer, blog_tags, featured_posts, featured_projects) = try_join!( let (site_footer, blog_tags, featured_blog_posts, featured_projects) = try_join!(
render_site_footer(), render_site_footer(),
get_popular_blog_tags(), get_popular_blog_tags(),
get_featured_posts(), get_featured_blog_posts(),
get_featured_projects() get_featured_projects()
)?; )?;
@ -39,7 +38,7 @@ pub async fn render_index() -> Result<IndexTemplate, StatusCode> {
site_footer, site_footer,
header_props: HeaderProps::default(), header_props: HeaderProps::default(),
blog_tags, blog_tags,
featured_posts, featured_blog_posts,
featured_projects, featured_projects,
}) })
} }

View File

@ -1,6 +1,5 @@
pub mod admin; pub mod admin;
pub mod blog_post_list;
pub mod blog_post_page;
pub mod contact; pub mod contact;
pub mod index; pub mod index;
pub mod post;
pub mod post_list;
pub mod project;

View File

@ -1,57 +0,0 @@
use crate::{components::site_header::Link, filters};
use askama::Template;
use axum::{extract::Path, http::StatusCode};
use chrono::{DateTime, Utc};
use serde::Deserialize;
use tokio::try_join;
use crate::{
components::{
site_footer::{render_site_footer, SiteFooter},
site_header::HeaderProps,
},
post_parser::{deserialize_date, parse_post},
};
pub const BLOG_POST_PATH: &str = "../_posts/blog";
#[derive(Deserialize, Debug)]
pub struct PostMetadata {
pub layout: String,
pub title: String,
pub segments: Vec<String>,
pub published: bool,
#[serde(deserialize_with = "deserialize_date")]
pub date: DateTime<Utc>,
pub thumbnail: Option<String>,
pub tags: Vec<String>,
}
#[derive(Template)]
#[template(path = "post.html")]
pub struct PostTemplate {
pub title: String,
pub body: String,
pub date: DateTime<Utc>,
pub tags: Vec<String>,
pub site_footer: SiteFooter,
pub header_props: HeaderProps,
}
pub async fn render_post(Path(post_id): Path<String>) -> Result<PostTemplate, StatusCode> {
let path = format!("../_posts/blog/{}.md", post_id);
let parse_post = parse_post::<PostMetadata>(&path);
let (site_footer, parsed) = try_join!(render_site_footer(), parse_post)?;
Ok(PostTemplate {
title: parsed.metadata.title,
date: parsed.metadata.date,
tags: parsed.metadata.tags,
body: parsed.body,
site_footer,
header_props: HeaderProps::with_back_link(Link {
href: "/blog".to_string(),
label: "All posts".to_string(),
}),
})
}

View File

@ -0,0 +1,2 @@
pub mod post_listing;
pub mod post_parser;

View File

@ -3,7 +3,7 @@ use serde::de::DeserializeOwned;
use tokio::fs::read_dir; use tokio::fs::read_dir;
use tracing::info; use tracing::info;
use crate::post_parser::{parse_post, ParseResult}; use super::post_parser::{parse_post, ParseResult};
pub async fn get_post_list<'de, Metadata: DeserializeOwned>( pub async fn get_post_list<'de, Metadata: DeserializeOwned>(
path: &str, path: &str,

View File

@ -1,9 +1,11 @@
use crate::{ use axum::http::StatusCode;
pages::project::ProjectMetadata,
post_list::get_post_list, use crate::post_utils::{
post_listing::get_post_list,
post_parser::{parse_html, ParseResult}, post_parser::{parse_html, ParseResult},
}; };
use axum::http::StatusCode;
use super::project_model::ProjectMetadata;
pub async fn get_featured_projects() -> Result<Vec<ParseResult<ProjectMetadata>>, StatusCode> { pub async fn get_featured_projects() -> Result<Vec<ParseResult<ProjectMetadata>>, StatusCode> {
let project_list = get_post_list::<ProjectMetadata>("../_projects").await?; let project_list = get_post_list::<ProjectMetadata>("../_projects").await?;

View File

@ -0,0 +1,2 @@
pub mod featured_projects;
pub mod project_model;

View File

@ -1,8 +1,8 @@
use crate::{ use crate::{
feed::render_rss_feed, feed::render_rss_feed,
pages::{ pages::{
admin::render_admin, contact::render_contact, index::render_index, post::render_post, admin::render_admin, blog_post_list::render_blog_post_list,
post_list::render_post_list, blog_post_page::render_blog_post, contact::render_contact, index::render_index,
}, },
}; };
use axum::{extract::MatchedPath, http::Request, routing::get, Router}; use axum::{extract::MatchedPath, http::Request, routing::get, Router};
@ -12,9 +12,9 @@ use tracing::info_span;
pub fn get_router() -> Router { pub fn get_router() -> Router {
Router::new() Router::new()
.route("/", get(render_index)) .route("/", get(render_index))
.route("/blog", get(render_post_list)) .route("/blog", get(render_blog_post_list))
.route("/blog/tags/:tag", get(render_post_list)) .route("/blog/tags/:tag", get(render_blog_post_list))
.route("/blog/:post_id", get(render_post)) .route("/blog/:post_id", get(render_blog_post))
.route("/contact", get(render_contact)) .route("/contact", get(render_contact))
.route("/admin", get(render_admin)) .route("/admin", get(render_admin))
.route("/feed.xml", get(render_rss_feed)) .route("/feed.xml", get(render_rss_feed))

View File

@ -35,7 +35,7 @@
<footer class="text-sm px-4"> <footer class="text-sm px-4">
<h3 class="text-xl font-semibold text-blue-900 my-2"> <h3 class="text-xl font-semibold text-blue-900 my-2">
{{crate::pages::project::translate_classification(project.metadata.classification)}} {{crate::projects::project_model::translate_classification(project.metadata.classification)}}
</h3> </h3>
<ul class="inline-block"> <ul class="inline-block">
{% for tag in project.metadata.tags %} {% for tag in project.metadata.tags %}

View File

@ -52,7 +52,7 @@
<hr class="border-blue-950 m-5"> <hr class="border-blue-950 m-5">
<ul class="mx-5"> <ul class="mx-5">
{% for post in featured_posts %} {% for post in featured_blog_posts %}
<li> <li>
{% include "components/post_preview.html" %} {% include "components/post_preview.html" %}
<hr class="border-blue-950 my-5"> <hr class="border-blue-950 my-5">