mylloon.fr/src/routes/blog.rs

109 lines
2.7 KiB
Rust
Raw Normal View History

2024-06-02 19:35:44 +02:00
use actix_web::{get, http::header::ContentType, routes, web, HttpResponse, Responder};
2023-04-14 11:30:58 +02:00
use cached::proc_macro::once;
use ramhorns::Content;
use crate::{
config::Config,
2024-11-09 16:54:38 +01:00
template::{InfosPage, NavBar},
2024-11-09 16:36:04 +01:00
utils::{
2024-11-09 16:54:38 +01:00
markdown::{File, TypeFileMetadata},
misc::{make_kw, read_file, Html},
routes::blog::{build_rss, get_post, get_posts, Post, BLOG_DIR, MIME_TYPE_RSS, POST_DIR},
},
};
2023-04-14 11:30:58 +02:00
#[get("/blog")]
2024-06-17 15:56:57 +02:00
pub async fn index(config: web::Data<Config>) -> impl Responder {
Html(build_index(config.get_ref().to_owned()))
2023-04-14 11:30:58 +02:00
}
2023-05-02 13:34:43 +02:00
#[derive(Content, Debug)]
2023-04-19 18:55:03 +02:00
struct BlogIndexTemplate {
navbar: NavBar,
2024-01-25 18:23:12 +01:00
about: Option<File>,
2023-04-24 18:01:38 +02:00
posts: Vec<Post>,
no_posts: bool,
2023-04-19 20:27:40 +02:00
}
2023-10-21 23:08:03 +02:00
#[once(time = 60)]
2023-10-24 11:50:30 +02:00
fn build_index(config: Config) -> String {
2024-01-25 18:23:12 +01:00
let blog_dir = format!("{}/{}", config.locations.data_dir, BLOG_DIR);
2024-05-28 20:26:58 +02:00
let mut posts = get_posts(&format!("{blog_dir}/{POST_DIR}"));
2024-01-25 18:23:12 +01:00
// Get about
let about: Option<File> =
2024-05-28 20:26:58 +02:00
read_file(&format!("{blog_dir}/about.md"), &TypeFileMetadata::Generic);
// Sort from newest to oldest
posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day));
posts.reverse();
config.tmpl.render(
"blog/index.html",
BlogIndexTemplate {
navbar: NavBar {
blog: true,
..NavBar::default()
},
2024-01-25 18:23:12 +01:00
about,
2023-04-24 18:01:38 +02:00
no_posts: posts.is_empty(),
posts,
},
2024-05-28 20:26:58 +02:00
InfosPage {
title: Some("Blog".into()),
desc: Some(format!(
2023-04-28 12:49:48 +02:00
"Liste des posts d'{}",
config.fc.name.unwrap_or_default()
)),
2024-05-28 20:26:58 +02:00
kw: Some(make_kw(&["blog", "blogging"])),
},
)
}
2023-05-02 13:34:43 +02:00
#[derive(Content, Debug)]
struct BlogPostTemplate {
navbar: NavBar,
post: Option<File>,
2023-05-02 23:10:36 +02:00
toc: String,
2023-04-14 11:30:58 +02:00
}
2023-04-26 10:41:49 +02:00
#[get("/blog/p/{id}")]
2024-06-17 15:56:57 +02:00
pub async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder {
2024-05-28 20:26:58 +02:00
Html(build_post(
&path.into_inner().0,
config.get_ref().to_owned(),
))
2023-04-14 11:30:58 +02:00
}
2024-05-28 20:26:58 +02:00
fn build_post(file: &str, config: Config) -> String {
2023-04-19 20:08:15 +02:00
let mut post = None;
2024-01-24 11:52:20 +01:00
let (infos, toc) = get_post(
&mut post,
file,
2024-05-28 20:26:58 +02:00
&config.fc.name.unwrap_or_default(),
&config.locations.data_dir,
2024-01-24 11:52:20 +01:00
);
2023-04-19 18:55:03 +02:00
config.tmpl.render(
"blog/post.html",
BlogPostTemplate {
navbar: NavBar {
blog: true,
..NavBar::default()
},
post,
toc,
},
infos,
)
}
2024-06-02 19:35:44 +02:00
#[routes]
#[get("/blog/blog.rss")]
2023-04-26 12:50:08 +02:00
#[get("/blog/rss")]
2024-06-17 15:56:57 +02:00
pub async fn rss(config: web::Data<Config>) -> impl Responder {
2023-04-26 15:16:57 +02:00
HttpResponse::Ok()
2024-01-25 18:23:12 +01:00
.content_type(ContentType(MIME_TYPE_RSS.parse().unwrap()))
2023-10-24 11:50:30 +02:00
.body(build_rss(config.get_ref().to_owned()))
2023-04-26 12:50:08 +02:00
}