switch from requiring trailing slashes to them being optional

This commit is contained in:
Gered 2023-07-10 14:15:13 -04:00
parent 4cce2d4ad7
commit 0b8423a64f
4 changed files with 27 additions and 7 deletions

View file

@ -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))
}

View file

@ -22,22 +22,22 @@ async fn latest_posts(data: web::Data<site::SiteService>) -> 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<site::SiteService>) -> 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<site::SiteService>) -> 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<site::SiteService>) -> 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)

View file

@ -143,7 +143,7 @@ pub struct Post {
impl Post {
pub fn try_from(value: config::Post, content_renderer: &ContentRenderer) -> Result<Self, SiteError> {
let url = format!(
"/{:04}/{:02}/{:02}/{}/", //
"/{:04}/{:02}/{:02}/{}", //
value.date.year(),
value.date.month(),
value.date.day(),

View file

@ -28,3 +28,9 @@ pub fn serialize_naivedatetime_to_i64<S: serde::Serializer>(
) -> Result<S::Ok, S::Error> {
serializer.serialize_i64(value.timestamp())
}
pub fn drop_trailing_slash(s: &mut String) {
if s.ends_with("/") {
s.pop();
}
}