diff --git a/.gitea/workflows/flyctl-deploy.yml b/.gitea/workflows/flyctl-deploy.yml index d3b2475..346a94c 100644 --- a/.gitea/workflows/flyctl-deploy.yml +++ b/.gitea/workflows/flyctl-deploy.yml @@ -6,9 +6,9 @@ on: jobs: deploy: name: Deploy app - runs-on: debian-latest + runs-on: debian-latest-arm steps: - - uses: actions/checkout@v3.5.3 + - uses: actions/checkout@v3.6.0 with: ref: trunk - uses: https://github.com/superfly/flyctl-actions/setup-flyctl@master diff --git a/src/main.rs b/src/main.rs index d053880..d0dae87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,6 @@ use orgize::Org; use serde::Deserialize; use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions}; use sqlx::{FromRow, Pool, Sqlite}; -use time::format_description::well_known::Rfc2822; use std::collections::HashMap; use std::fs::{self, File}; use std::io::prelude::*; @@ -29,8 +28,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use tokio::sync::Mutex; use tower_http::services::ServeDir; use rss::ChannelBuilder; -use time::{self, Date, format_description}; -use time::macros::time as t; +use time::{self, format_description, format_description::well_known::Rfc2822}; lazy_static! { static ref CACHE: Mutex> = Mutex::new(HashMap::new()); @@ -128,8 +126,8 @@ async fn main() -> Result<(), sqlx::Error> { Ok(()) } -async fn rss() -> Result { - let mut posts: Vec = Vec::new(); +fn get_posts() -> Vec { + let mut posts: Vec = Vec::new(); for entry in fs::read_dir("./posts").unwrap() { let entry = entry.unwrap(); let path = entry.path(); @@ -160,27 +158,39 @@ async fn rss() -> Result { .unwrap_or(&"") .split(":") .collect::>()[1].trim(); - let date = format!("{} 00:00:00 +00:00:00", date.trim()); - let format = format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]:[offset_second]").unwrap(); - let pub_date = match time::OffsetDateTime::parse(&date, &format).unwrap().format(&Rfc2822) { - Ok(r) => r, - Err(e) => { dbg!(e.to_string()); "".to_owned() }, - }; - let item = rss::ItemBuilder::default() - .title(Some(title.to_owned())) - .link(Some(format!("https://gabrielsimmer.com/blog/{}", fname))) - .pub_date(Some(pub_date)) - .build(); + let date = date.trim(); - posts.push(item); + posts.push(Post { + name: title.to_owned(), + route: fname, + date: date.to_owned(), + }); } } + posts.sort_by(|a, b| b.date.cmp(&a.date)); + posts +} +async fn rss() -> Result { + let posts = get_posts(); + let rss_posts: Vec = posts.into_iter().map(|p| { + let date = format!("{} 00:00:00 +00:00:00", p.date); + let format = format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory]:[offset_minute]:[offset_second]").unwrap(); + let pub_date = match time::OffsetDateTime::parse(&date, &format).unwrap().format(&Rfc2822) { + Ok(r) => r, + Err(e) => { dbg!(e.to_string()); "".to_owned() }, + }; + rss::ItemBuilder::default() + .title(Some(p.name)) + .link(Some(format!("https://gabrielsimmer.com/blog/{}", p.route))) + .pub_date(Some(pub_date)) + .build() + }).collect(); let channel = ChannelBuilder::default() .title("Gabriel Simmer's Blog".to_owned()) .link("https://gabrielsimmer.com/blog".to_owned()) .description("Gabriel Simmer's Blog Posts.".to_owned()) - .items(posts) + .items(rss_posts) .build(); return Ok(Response::builder() @@ -227,50 +237,8 @@ async fn homepage() -> Markup { } async fn list_blog_posts() -> Markup { - let mut posts: Vec = Vec::new(); - for entry in fs::read_dir("./posts").unwrap() { - let entry = entry.unwrap(); - let path = entry.path(); - let filename = path.file_name().unwrap().to_str().unwrap(); - let ext = path.extension().unwrap().to_str().unwrap(); - - // strip extension - let fname = filename.replace(&format!(".{}", ext), ""); - if ext == "md" || ext == "org" { - // We'll have the date at the beginning of the file - let mut content = File::open(&path).unwrap(); - let mut buffer = [0; 100]; - content.read(&mut buffer).unwrap(); - // Match date data of `date: YYYY-MM-DD` in the first 100 bytes - let metadata = String::from_utf8_lossy(&buffer); - let metadata_lines = metadata.split("\n").collect::>(); - // dbg!(&metadata); - // Split by --- and get the second element - let date = metadata_lines - .iter() - .find(|&x| x.contains("date:")) - .unwrap_or(&"") - .split(":") - .collect::>()[1]; - let title = metadata_lines - .iter() - .find(|&x| x.contains("title:")) - .unwrap_or(&"") - .split(":") - .collect::>()[1]; - let date = date.trim(); - dbg!(&date); - - posts.push(Post { - name: title.to_owned(), - route: fname, - date: date.to_owned(), - }); - } - } - + let posts = get_posts(); // Sort posts by date - posts.sort_by(|a, b| b.date.cmp(&a.date)); html! { (header("/blog"))