diff --git a/src/config.rs b/src/config.rs index 6a5be68..16d52e2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,8 @@ use std::fs::File; use std::io::BufReader; use std::path::PathBuf; +use crate::util::drop_trailing_slash; + #[derive(Debug, Clone, serde::Deserialize)] pub struct Server { pub bind_addr: String, @@ -94,11 +96,22 @@ pub fn load_content( let mut pages: Pages = load_config(pages_path)?; for page in pages.pages.iter_mut() { page.file_path = [&server_config.pages_path, &page.file_path].iter().collect(); + drop_trailing_slash(&mut page.url); + if let Some(alternate_urls) = &mut page.alternate_urls { + for alternate_url in alternate_urls.iter_mut() { + drop_trailing_slash(alternate_url); + } + } } log::info!("Loading posts config from {:?}", posts_path); let mut posts: Posts = load_config(posts_path)?; for post in posts.posts.iter_mut() { post.file_path = [&server_config.posts_path, &post.file_path].iter().collect(); + if let Some(alternate_urls) = &mut post.alternate_urls { + for alternate_url in alternate_urls.iter_mut() { + drop_trailing_slash(alternate_url); + } + } } Ok((pages, posts)) } diff --git a/src/main.rs b/src/main.rs index 07283fe..819f8b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,22 +22,22 @@ async fn latest_posts(data: web::Data) -> impl Responder { data.serve_latest_post() } -#[actix_web::get("/tag/{tag}/")] +#[actix_web::get("/tag/{tag}")] async fn latest_posts_by_tag(path: web::Path<(String,)>, data: web::Data) -> impl Responder { let tag = path.into_inner().0; - log::debug!("GET /tag/{0}/ -> latest_posts_by_tag(), tag = {0}", tag); + log::debug!("GET /tag/{0} -> latest_posts_by_tag(), tag = {0}", tag); data.serve_posts_by_tag(&tag) } -#[actix_web::get("/archive/")] +#[actix_web::get("/archive")] async fn posts_archive(data: web::Data) -> impl Responder { - log::debug!("GET /archive/ -> posts_archive()"); + log::debug!("GET /archive -> posts_archive()"); data.serve_posts_archive() } -#[actix_web::get("/rss/")] +#[actix_web::get("/rss")] async fn rss_feed(data: web::Data) -> impl Responder { - log::debug!("GET /rss/ -> rss_feed()"); + log::debug!("GET /rss -> rss_feed()"); data.serve_rss_feed() } @@ -179,6 +179,7 @@ async fn main() -> anyhow::Result<()> { HttpServer::new(move || { App::new() // .app_data(data.clone()) + .wrap(actix_web::middleware::NormalizePath::trim()) .service(latest_posts) .service(latest_posts_by_tag) .service(posts_archive) diff --git a/src/site.rs b/src/site.rs index 0258bde..c435b99 100644 --- a/src/site.rs +++ b/src/site.rs @@ -143,7 +143,7 @@ pub struct Post { impl Post { pub fn try_from(value: config::Post, content_renderer: &ContentRenderer) -> Result { let url = format!( - "/{:04}/{:02}/{:02}/{}/", // + "/{:04}/{:02}/{:02}/{}", // value.date.year(), value.date.month(), value.date.day(), diff --git a/src/util.rs b/src/util.rs index 47d84f0..0a4adca 100644 --- a/src/util.rs +++ b/src/util.rs @@ -28,3 +28,9 @@ pub fn serialize_naivedatetime_to_i64( ) -> Result { serializer.serialize_i64(value.timestamp()) } + +pub fn drop_trailing_slash(s: &mut String) { + if s.ends_with("/") { + s.pop(); + } +}