add -i flag to serve any "index" file as index

This commit is contained in:
gigirassy
2026-07-18 02:00:16 +02:00
parent a047a1737d
commit 938532dbd3
+19 -1
View File
@@ -11,7 +11,7 @@ const ROOT: &str = "/data";
fn mime(path: &Path) -> &'static str { fn mime(path: &Path) -> &'static str {
match path.extension().and_then(|s| s.to_str()).unwrap_or("") { match path.extension().and_then(|s| s.to_str()).unwrap_or("") {
"html" => "text/html; charset=utf-8", "html" => "text/html; charset=utf8",
"css" => "text/css", "css" => "text/css",
"js" => "application/javascript", "js" => "application/javascript",
"json" => "application/json", "json" => "application/json",
@@ -40,6 +40,7 @@ fn sanitize(url: &str) -> PathBuf {
fn main() { fn main() {
let spa = env::args().any(|a| a == "-s"); let spa = env::args().any(|a| a == "-s");
let any_index = env::args().any(|a| a == "-i");
let server = Server::http("0.0.0.0:3000").unwrap(); let server = Server::http("0.0.0.0:3000").unwrap();
@@ -47,8 +48,25 @@ fn main() {
let mut path = sanitize(req.url()); let mut path = sanitize(req.url());
if path.is_dir() { if path.is_dir() {
if any_index {
if let Ok(entries) = fs::read_dir(&path) {
if let Some(entry) = entries.flatten().find(|e| {
e.path()
.file_stem()
.and_then(|s| s.to_str())
== Some("index")
}) {
path = entry.path();
} else {
path.push("index.html"); path.push("index.html");
} }
} else {
path.push("index.html");
}
} else {
path.push("index.html");
}
}
if !path.exists() && spa { if !path.exists() && spa {
path = PathBuf::from(ROOT).join("index.html"); path = PathBuf::from(ROOT).join("index.html");