michalvankodev-site/axum_server/src/feed.rs
2024-01-30 22:19:36 +01:00

51 lines
1.9 KiB
Rust

use axum::http::{header, StatusCode};
use axum::response::IntoResponse;
use chrono::Utc;
use rss::{ChannelBuilder, GuidBuilder, Item, ItemBuilder};
use crate::{pages::post::PostMetadata, post_list::get_post_list};
pub async fn render_rss_feed() -> Result<impl IntoResponse, StatusCode> {
let mut post_list = get_post_list::<PostMetadata>().await.unwrap_or(vec![]);
post_list.sort_by_key(|post| post.metadata.date);
post_list.reverse();
let last_build_date = Utc::now().to_rfc2822();
let publish_date = post_list.last().map_or_else(
|| last_build_date.clone(),
|post| post.metadata.date.to_rfc2822(),
);
let post_items = post_list
.into_iter()
.map(|post| {
ItemBuilder::default()
.title(Some(post.metadata.title))
.link(Some(format!("https://michalvanko.dev/blog/{}", post.slug)))
// TODO Description should be just a preview
.description(None)
.guid(Some(
GuidBuilder::default()
.value(format!("https://michalvanko.dev/blog/{}", post.slug))
.build(),
))
.pub_date(Some(post.metadata.date.to_rfc2822()))
.build()
})
.collect::<Vec<Item>>();
let feed_builder = ChannelBuilder::default()
.title("michalvanko.dev latest posts".to_string())
.link("https://michalvanko.dev".to_string())
.description("Latest posts published on michalvanko.dev blog site".to_string())
.language(Some("en".to_string()))
.webmaster(Some("michalvankosk@gmail.com".to_string()))
.pub_date(Some(publish_date))
.last_build_date(Some(last_build_date))
.items(post_items)
.build();
let response = feed_builder.to_string();
return Ok(([(header::CONTENT_TYPE, "application/xml")], response));
}