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:
+20
-2
@@ -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,7 +48,24 @@ fn main() {
|
|||||||
let mut path = sanitize(req.url());
|
let mut path = sanitize(req.url());
|
||||||
|
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
path.push("index.html");
|
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 {
|
if !path.exists() && spa {
|
||||||
|
|||||||
Reference in New Issue
Block a user