directory listing

This commit is contained in:
2024-01-14 22:56:14 +01:00
parent 03fefda519
commit 834f998788
5 changed files with 72 additions and 2 deletions

View File

@ -1,2 +1,3 @@
pub mod index;
pub mod post;
pub mod post_list;

View File

@ -13,7 +13,7 @@ pub struct PostMetadata {
pub published: bool,
#[serde(deserialize_with = "deserialize_date")]
pub date: DateTime<Utc>,
pub thumbnail: String,
pub thumbnail: Option<String>,
pub tags: Vec<String>,
}

View File

@ -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<ParseResult<PostMetadata>>,
// TODO tags and pagination
}
pub async fn render_post_list() -> Result<PostListTemplate, StatusCode> {
let path = "../_posts/blog/";
let dir = read_dir(path).await;
let mut posts: Vec<ParseResult<PostMetadata>> = 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::<PostMetadata>(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`