From 834f998788fd3659f4f4d859b1b95be71fa8f216 Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Sun, 14 Jan 2024 22:56:14 +0100 Subject: [PATCH] directory listing --- axum_server/src/pages/mod.rs | 1 + axum_server/src/pages/post.rs | 2 +- axum_server/src/pages/post_list.rs | 47 ++++++++++++++++++++++++++++ axum_server/src/router.rs | 3 +- axum_server/templates/post_list.html | 21 +++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 axum_server/src/pages/post_list.rs create mode 100644 axum_server/templates/post_list.html diff --git a/axum_server/src/pages/mod.rs b/axum_server/src/pages/mod.rs index dad6d73..6515127 100644 --- a/axum_server/src/pages/mod.rs +++ b/axum_server/src/pages/mod.rs @@ -1,2 +1,3 @@ pub mod index; pub mod post; +pub mod post_list; diff --git a/axum_server/src/pages/post.rs b/axum_server/src/pages/post.rs index 8a4e5a0..88acd97 100644 --- a/axum_server/src/pages/post.rs +++ b/axum_server/src/pages/post.rs @@ -13,7 +13,7 @@ pub struct PostMetadata { pub published: bool, #[serde(deserialize_with = "deserialize_date")] pub date: DateTime, - pub thumbnail: String, + pub thumbnail: Option, pub tags: Vec, } diff --git a/axum_server/src/pages/post_list.rs b/axum_server/src/pages/post_list.rs new file mode 100644 index 0000000..c8e5dd8 --- /dev/null +++ b/axum_server/src/pages/post_list.rs @@ -0,0 +1,47 @@ +use askama::Template; +use axum::http::StatusCode; +use tokio::fs::read_dir; +use tracing::info; + +use crate::post_parser::{parse_post, ParseResult}; + +use super::post::PostMetadata; + +#[derive(Template)] +#[template(path = "post_list.html")] +pub struct PostListTemplate { + pub posts: Vec>, + // TODO tags and pagination +} + +pub async fn render_post_list() -> Result { + let path = "../_posts/blog/"; + let dir = read_dir(path).await; + let mut posts: Vec> = Vec::new(); + + let mut files = match dir { + Err(_reason) => { + // TODO find the real reason + return Err(StatusCode::INTERNAL_SERVER_ERROR); + } + Ok(files) => files, + }; + + while let Some(file) = files + .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); + } + + Ok(PostListTemplate { posts }) +} + +// TODO Do we want pagination or not? Ask designer +// TODO How are we going to implement tags? The path extractor would have to make decision on wether we have a path or a blog post +// TODO Refactor `?` with `.map_err` diff --git a/axum_server/src/router.rs b/axum_server/src/router.rs index 844f205..1bc8bf9 100644 --- a/axum_server/src/router.rs +++ b/axum_server/src/router.rs @@ -1,4 +1,4 @@ -use crate::pages::{index::render_index, post::render_post}; +use crate::pages::{index::render_index, post::render_post, post_list::render_post_list}; use axum::{extract::MatchedPath, http::Request, routing::get, Router}; use tower_http::trace::TraceLayer; use tracing::info_span; @@ -6,6 +6,7 @@ use tracing::info_span; pub fn get_router() -> Router { Router::new() .route("/", get(render_index)) + .route("/blog", get(render_post_list)) .route("/blog/:post_id", get(render_post)) .layer( TraceLayer::new_for_http().make_span_with(|request: &Request<_>| { diff --git a/axum_server/templates/post_list.html b/axum_server/templates/post_list.html new file mode 100644 index 0000000..17ca242 --- /dev/null +++ b/axum_server/templates/post_list.html @@ -0,0 +1,21 @@ +{% if posts.len() == 0 %} +

You've found void in the space.

+{% else %} +

{# if filters.tags #} {# {filters.tags} #} Blog posts

+ +{#if filters.tags#} + +{% endif%} + +
    + {% for post in posts %} +
  • + {# #} {# + #} {{post.metadata.title}} +
  • + {% endfor %} +
+ +{# #}