refactor modules
This commit is contained in:
parent
6dc6a581e3
commit
5365cb5409
@ -20,7 +20,10 @@ decap_server:
|
||||
|
||||
# Run dev server in watch mode
|
||||
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
|
||||
test:
|
||||
|
18
axum_server/src/blog_posts/blog_post_model.rs
Normal file
18
axum_server/src/blog_posts/blog_post_model.rs
Normal 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>,
|
||||
}
|
16
axum_server/src/blog_posts/featured_blog_posts.rs
Normal file
16
axum_server/src/blog_posts/featured_blog_posts.rs
Normal 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)
|
||||
}
|
3
axum_server/src/blog_posts/mod.rs
Normal file
3
axum_server/src/blog_posts/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod blog_post_model;
|
||||
pub mod featured_blog_posts;
|
||||
pub mod tag_list;
|
@ -1,15 +1,16 @@
|
||||
use crate::{
|
||||
pages::post::{PostMetadata, BLOG_POST_PATH},
|
||||
post_list::get_post_list,
|
||||
};
|
||||
use axum::http::StatusCode;
|
||||
use std::collections::HashMap;
|
||||
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> {
|
||||
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
|
||||
.into_iter()
|
||||
.flat_map(|post| post.metadata.tags)
|
@ -2,26 +2,25 @@ use askama::Template;
|
||||
use axum::http::StatusCode;
|
||||
|
||||
use crate::{
|
||||
pages::post::{PostMetadata, BLOG_POST_PATH},
|
||||
post_list::get_post_list,
|
||||
post_parser::ParseResult,
|
||||
blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH},
|
||||
post_utils::{post_listing::get_post_list, post_parser::ParseResult},
|
||||
};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "site_footer.html")]
|
||||
pub struct SiteFooter {
|
||||
pub latest_posts: Vec<ParseResult<PostMetadata>>,
|
||||
pub latest_posts: Vec<ParseResult<BlogPostMetadata>>,
|
||||
}
|
||||
|
||||
// TODO remove whole site footer anyway
|
||||
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.reverse();
|
||||
|
||||
let latest_posts = post_list
|
||||
.into_iter()
|
||||
.take(6)
|
||||
.collect::<Vec<ParseResult<PostMetadata>>>();
|
||||
.collect::<Vec<ParseResult<BlogPostMetadata>>>();
|
||||
Ok(SiteFooter { latest_posts })
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
@ -3,11 +3,11 @@ 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};
|
||||
use crate::blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH};
|
||||
use crate::post_utils::post_listing::get_post_list;
|
||||
|
||||
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
|
||||
.unwrap_or(vec![]);
|
||||
post_list.sort_by_key(|post| post.metadata.date);
|
||||
|
@ -3,18 +3,14 @@ use tower_http::services::ServeDir;
|
||||
use tower_livereload::LiveReloadLayer;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod blog_posts;
|
||||
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 post_utils;
|
||||
mod projects;
|
||||
mod router;
|
||||
mod tag_list;
|
||||
// mod template;
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -2,33 +2,33 @@ use askama::Template;
|
||||
use axum::{extract::Path, http::StatusCode};
|
||||
|
||||
use crate::{
|
||||
blog_posts::blog_post_model::{BlogPostMetadata, BLOG_POST_PATH},
|
||||
components::{
|
||||
site_footer::{render_site_footer, SiteFooter},
|
||||
site_header::{HeaderProps, Link},
|
||||
},
|
||||
filters,
|
||||
post_list::get_post_list,
|
||||
post_parser::ParseResult,
|
||||
post_utils::{post_listing::get_post_list, post_parser::ParseResult},
|
||||
};
|
||||
|
||||
use super::post::{PostMetadata, BLOG_POST_PATH};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "post_list.html")]
|
||||
pub struct PostListTemplate {
|
||||
pub title: String,
|
||||
pub posts: Vec<ParseResult<PostMetadata>>,
|
||||
pub posts: Vec<ParseResult<BlogPostMetadata>>,
|
||||
pub tag: Option<String>,
|
||||
pub site_footer: SiteFooter,
|
||||
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
|
||||
let tag = tag.map(|Path(tag)| tag);
|
||||
|
||||
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.reverse();
|
||||
|
43
axum_server/src/pages/blog_post_page.rs
Normal file
43
axum_server/src/pages/blog_post_page.rs
Normal 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(),
|
||||
}),
|
||||
})
|
||||
}
|
@ -2,36 +2,35 @@ use askama::Template;
|
||||
use axum::http::StatusCode;
|
||||
use tokio::try_join;
|
||||
|
||||
use crate::filters;
|
||||
use crate::{
|
||||
blog_posts::{
|
||||
blog_post_model::BlogPostMetadata, featured_blog_posts::get_featured_blog_posts,
|
||||
tag_list::get_popular_blog_tags,
|
||||
},
|
||||
components::{
|
||||
site_footer::{render_site_footer, SiteFooter},
|
||||
site_header::HeaderProps,
|
||||
},
|
||||
featured_posts::get_featured_posts,
|
||||
featured_projects::get_featured_projects,
|
||||
post_parser::ParseResult,
|
||||
tag_list::get_popular_blog_tags,
|
||||
filters,
|
||||
post_utils::post_parser::ParseResult,
|
||||
projects::{featured_projects::get_featured_projects, project_model::ProjectMetadata},
|
||||
};
|
||||
|
||||
use super::post::PostMetadata;
|
||||
use super::project::ProjectMetadata;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
pub struct IndexTemplate {
|
||||
site_footer: SiteFooter,
|
||||
header_props: HeaderProps,
|
||||
blog_tags: Vec<String>,
|
||||
featured_posts: Vec<ParseResult<PostMetadata>>,
|
||||
featured_blog_posts: Vec<ParseResult<BlogPostMetadata>>,
|
||||
featured_projects: Vec<ParseResult<ProjectMetadata>>,
|
||||
}
|
||||
|
||||
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(),
|
||||
get_popular_blog_tags(),
|
||||
get_featured_posts(),
|
||||
get_featured_blog_posts(),
|
||||
get_featured_projects()
|
||||
)?;
|
||||
|
||||
@ -39,7 +38,7 @@ pub async fn render_index() -> Result<IndexTemplate, StatusCode> {
|
||||
site_footer,
|
||||
header_props: HeaderProps::default(),
|
||||
blog_tags,
|
||||
featured_posts,
|
||||
featured_blog_posts,
|
||||
featured_projects,
|
||||
})
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
pub mod admin;
|
||||
pub mod blog_post_list;
|
||||
pub mod blog_post_page;
|
||||
pub mod contact;
|
||||
pub mod index;
|
||||
pub mod post;
|
||||
pub mod post_list;
|
||||
pub mod project;
|
||||
|
@ -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(),
|
||||
}),
|
||||
})
|
||||
}
|
2
axum_server/src/post_utils/mod.rs
Normal file
2
axum_server/src/post_utils/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod post_listing;
|
||||
pub mod post_parser;
|
@ -3,7 +3,7 @@ use serde::de::DeserializeOwned;
|
||||
use tokio::fs::read_dir;
|
||||
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>(
|
||||
path: &str,
|
@ -1,9 +1,11 @@
|
||||
use crate::{
|
||||
pages::project::ProjectMetadata,
|
||||
post_list::get_post_list,
|
||||
use axum::http::StatusCode;
|
||||
|
||||
use crate::post_utils::{
|
||||
post_listing::get_post_list,
|
||||
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> {
|
||||
let project_list = get_post_list::<ProjectMetadata>("../_projects").await?;
|
2
axum_server/src/projects/mod.rs
Normal file
2
axum_server/src/projects/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod featured_projects;
|
||||
pub mod project_model;
|
@ -1,8 +1,8 @@
|
||||
use crate::{
|
||||
feed::render_rss_feed,
|
||||
pages::{
|
||||
admin::render_admin, contact::render_contact, index::render_index, post::render_post,
|
||||
post_list::render_post_list,
|
||||
admin::render_admin, blog_post_list::render_blog_post_list,
|
||||
blog_post_page::render_blog_post, contact::render_contact, index::render_index,
|
||||
},
|
||||
};
|
||||
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
|
||||
@ -12,9 +12,9 @@ use tracing::info_span;
|
||||
pub fn get_router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(render_index))
|
||||
.route("/blog", get(render_post_list))
|
||||
.route("/blog/tags/:tag", get(render_post_list))
|
||||
.route("/blog/:post_id", get(render_post))
|
||||
.route("/blog", get(render_blog_post_list))
|
||||
.route("/blog/tags/:tag", get(render_blog_post_list))
|
||||
.route("/blog/:post_id", get(render_blog_post))
|
||||
.route("/contact", get(render_contact))
|
||||
.route("/admin", get(render_admin))
|
||||
.route("/feed.xml", get(render_rss_feed))
|
||||
|
@ -35,7 +35,7 @@
|
||||
|
||||
<footer class="text-sm px-4">
|
||||
<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>
|
||||
<ul class="inline-block">
|
||||
{% for tag in project.metadata.tags %}
|
||||
|
@ -52,7 +52,7 @@
|
||||
<hr class="border-blue-950 m-5">
|
||||
|
||||
<ul class="mx-5">
|
||||
{% for post in featured_posts %}
|
||||
{% for post in featured_blog_posts %}
|
||||
<li>
|
||||
{% include "components/post_preview.html" %}
|
||||
<hr class="border-blue-950 my-5">
|
||||
|
Loading…
Reference in New Issue
Block a user