From 59ab02b472d0fa2ffec19b44688c290304f57fc6 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 25 May 2024 18:39:42 +0200 Subject: [PATCH] serve html files without needing to write the .html extension (GET /thing is will return /thing.html if it exists) --- src/main.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index fea9a6d..f60dce4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,9 +137,10 @@ fn get_file(request: &Request) -> Option<(Content, bool)> { let current_dir = env::current_dir().unwrap(); - let path = current_dir - .join(request.path.strip_prefix('/')?) - .canonicalize() + let request_path = request.path.strip_prefix('/')?; + let request_path_with_ext = format!("{request_path}.html"); + let path = fs::canonicalize(current_dir.join(request_path)) + .or_else(|_| fs::canonicalize(current_dir.join(request_path_with_ext))) .ok()?; if path