serve html files without needing to write the .html extension (GET /thing is will return /thing.html if it exists)

This commit is contained in:
Crispy 2024-05-25 18:39:42 +02:00
parent 6f0484f26b
commit 59ab02b472

View file

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