mirror of
https://codeberg.org/gigirassy/teletraan.git
synced 2026-08-30 15:27:38 +00:00
add -i flag to serve any "index" file as index
This commit is contained in:
+19
-1
@@ -11,7 +11,7 @@ const ROOT: &str = "/data";
|
||||
|
||||
fn mime(path: &Path) -> &'static str {
|
||||
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",
|
||||
"js" => "application/javascript",
|
||||
"json" => "application/json",
|
||||
@@ -40,6 +40,7 @@ fn sanitize(url: &str) -> PathBuf {
|
||||
|
||||
fn main() {
|
||||
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();
|
||||
|
||||
@@ -47,8 +48,25 @@ fn main() {
|
||||
let mut path = sanitize(req.url());
|
||||
|
||||
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");
|
||||
}
|
||||
} else {
|
||||
path.push("index.html");
|
||||
}
|
||||
} else {
|
||||
path.push("index.html");
|
||||
}
|
||||
}
|
||||
|
||||
if !path.exists() && spa {
|
||||
path = PathBuf::from(ROOT).join("index.html");
|
||||
|
||||
Reference in New Issue
Block a user