diff --git a/Cargo.lock b/Cargo.lock index ab666d8..2baad09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -788,7 +788,7 @@ dependencies = [ [[package]] name = "surfer" -version = "0.3.1" +version = "0.3.2" dependencies = [ "async-std", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 15e0053..50b1ab2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "surfer" -version = "0.3.1" +version = "0.3.2" edition = "2021" description = "A small backend \"framework\" for Rust" authors = ["Patrand"] diff --git a/README.md b/README.md index ce91e72..48aa826 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Clone the repository and add the following to your `Cargo.toml`: ```toml [dependencies] -surfer = "0.3.1" +surfer = "0.3.2" ``` ## 📚 Example Usage diff --git a/src/server.rs b/src/server.rs index 8ddc059..4c774da 100644 --- a/src/server.rs +++ b/src/server.rs @@ -180,12 +180,33 @@ impl Server { dir_path: PathBuf, relative_path: String, ) -> Response { + // TODO: I should definitely refactor this method let file_path = dir_path.clone(); let mut file_path = file_path.join(relative_path.trim_start_matches('/')); + // If the requested path is a directory, try to serve an index.html file if file_path.is_dir().await || relative_path.is_empty() || relative_path.ends_with("/") { file_path.push("index.html"); } + // If requested path has no extension, try to serve an HTML file + if file_path.extension().is_none() { + // HTML File check + let html_file_path = file_path.clone(); + let html_file_path = + PathBuf::from(format!("{}.html", html_file_path.to_string_lossy())); + if html_file_path.exists().await { + file_path = html_file_path; + } + } + + if !file_path.exists().await { + return Response { + status_code: 404, + headers: headers!(("Content-Type", "text/plain")), + body: Some(b"404 Not Found".to_vec()), + }; + } + FileResponse { status_code: 200, headers: None,