From b2c4e27a7a8166ace2ae302b6c1e022bfc8274bb Mon Sep 17 00:00:00 2001 From: Joris Date: Thu, 13 Feb 2025 22:53:35 +0100 Subject: Remove trailing slash in router --- src/routes.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/routes.rs') diff --git a/src/routes.rs b/src/routes.rs index 3b3fc9a..ae97b7a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -23,11 +23,12 @@ pub async fn routes( authorized_key: String, files_dir: String, ) -> Result>> { - let path = &request.uri().path().split('/').collect::>()[1..]; + let path = remove_trailing_slash(request.uri().path()); + let path_parts = &path.split('/').collect::>()[1..]; let files_dir = Path::new(&files_dir); - match (request.method(), path) { - (&Method::GET, [""]) => Ok(html(templates::INDEX)), + match (request.method(), path_parts) { + (&Method::GET, []) => Ok(html(templates::INDEX)), (&Method::GET, ["static", "main.js"]) => Ok(static_file( include_str!("static/main.js"), "application/javascript", @@ -48,6 +49,10 @@ pub async fn routes( } } +fn remove_trailing_slash(str: &str) -> String { + str.to_string().strip_suffix("/").unwrap_or(str).to_string() +} + async fn upload_file( request: Request, db_conn: Connection, -- cgit v1.2.3