blog genericity

This commit is contained in:
Mylloon 2024-01-25 18:23:12 +01:00
parent c787171d6b
commit 9558202a46
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 19 additions and 7 deletions

View file

@ -7,7 +7,7 @@ use ::rss::{
extension::atom::{AtomExtension, Link},
Category, Channel, Guid, Image, Item,
};
use actix_web::{get, web, HttpResponse, Responder};
use actix_web::{get, http::header::ContentType, web, HttpResponse, Responder};
use cached::proc_macro::once;
use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc};
use chrono_tz::Europe;
@ -27,6 +27,8 @@ use crate::{
};
const MIME_TYPE_RSS: &str = "application/rss+xml";
const BLOG_DIR: &str = "blog";
const POST_DIR: &str = "posts";
#[get("/blog")]
async fn index(config: web::Data<Config>) -> impl Responder {
@ -36,13 +38,19 @@ async fn index(config: web::Data<Config>) -> impl Responder {
#[derive(Content, Debug)]
struct BlogIndexTemplate {
navbar: NavBar,
about: Option<File>,
posts: Vec<Post>,
no_posts: bool,
}
#[once(time = 60)]
fn build_index(config: Config) -> String {
let mut posts = get_posts(format!("{}/blog", config.locations.data_dir));
let blog_dir = format!("{}/{}", config.locations.data_dir, BLOG_DIR);
let mut posts = get_posts(format!("{}/{}", blog_dir, POST_DIR));
// Get about
let about: Option<File> =
read_file(&format!("{}/about.md", blog_dir), TypeFileMetadata::Generic);
// Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));
@ -55,6 +63,7 @@ fn build_index(config: Config) -> String {
blog: true,
..NavBar::default()
},
about,
no_posts: posts.is_empty(),
posts,
},
@ -82,7 +91,7 @@ struct Post {
impl Post {
// Fetch the file content
fn fetch_content(&mut self, data_dir: &str) {
let blog_dir = format!("{}/blog", data_dir);
let blog_dir = format!("{}/{}/{}", data_dir, BLOG_DIR, POST_DIR);
let ext = ".md";
if let Some(file) = read_file(
@ -217,7 +226,7 @@ fn get_post(
name: String,
data_dir: String,
) -> (Infos, String) {
let blog_dir = format!("{}/blog", data_dir);
let blog_dir = format!("{}/{}/{}", data_dir, BLOG_DIR, POST_DIR);
let ext = ".md";
*post = read_file(
@ -272,13 +281,16 @@ fn get_post(
#[get("/blog/rss")]
async fn rss(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok()
.append_header(("content-type", MIME_TYPE_RSS))
.content_type(ContentType(MIME_TYPE_RSS.parse().unwrap()))
.body(build_rss(config.get_ref().to_owned()))
}
#[once(time = 10800)] // 3h
fn build_rss(config: Config) -> String {
let mut posts = get_posts(format!("{}/blog", config.locations.data_dir));
let mut posts = get_posts(format!(
"{}/{}/{}",
config.locations.data_dir, BLOG_DIR, POST_DIR
));
// Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));

View file

@ -16,7 +16,7 @@
{{#data}}
<h1>Blog</h1>
<p>Blog perso, je dis peut-être n'importe quoi 🫶</p>
{{#about}} {{&content}} {{/about}}
<a id="rss" href="/blog/rss">Lien vers le flux RSS</a>
{{#no_posts}}