🔥 使用 Rust 重构 #5

Closed
LittleSheep wants to merge 9 commits from refactor/rust into master
5 changed files with 44 additions and 7 deletions
Showing only changes of commit 905b70349b - Show all commits

View File

@ -6,4 +6,4 @@ hosts = ["localhost"]
paths = ["/"] paths = ["/"]
[[locations.destinations]] [[locations.destinations]]
id = "static" id = "static"
uri = "files://regions/index.html" uri = "files://regions?index=index.html"

View File

@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hello, World!</title>
</head>
<body>
<p>Hello, there!</p>
<p>
Here's the roadsign benchmarking test data! And you are in the subfolder
now!
</p>
</body>
</html>

View File

@ -98,20 +98,23 @@ impl Destination {
.get("utf8") .get("utf8")
.and_then(|val| val.as_bool()) .and_then(|val| val.as_bool())
.unwrap_or(false), .unwrap_or(false),
with_slash: queries
.get("slash")
.and_then(|val| val.as_bool())
.unwrap_or(false),
browse: queries browse: queries
.get("browse") .get("browse")
.and_then(|val| val.as_bool()) .and_then(|val| val.as_bool())
.unwrap_or(false), .unwrap_or(false),
with_slash: queries
.get("slash")
.and_then(|val| val.as_bool())
.unwrap_or(false),
index: queries index: queries
.get("index") .get("index")
.and_then(|val| val.as_str().map(str::to_string)), .and_then(|val| val.as_str().map(str::to_string)),
fallback: queries fallback: queries
.get("fallback") .get("fallback")
.and_then(|val| val.as_str().map(str::to_string)), .and_then(|val| val.as_str().map(str::to_string)),
suffix: queries
.get("suffix")
.and_then(|val| val.as_str().map(str::to_string)),
}) })
} }
_ => Err(()), _ => Err(()),

View File

@ -20,7 +20,9 @@ pub fn scan_regions(basepath: String) -> io::Result<(Vec<config::Region>, u32)>
} }
pub fn load_region(file: DirEntry) -> Result<config::Region, String> { pub fn load_region(file: DirEntry) -> Result<config::Region, String> {
if file.path().extension().and_then(OsStr::to_str).unwrap() != "toml" { if file.metadata().map(|val| val.is_dir()).unwrap()
|| file.path().extension().and_then(OsStr::to_str).unwrap() != "toml"
{
return Err("File entry wasn't toml file".to_string()); return Err("File entry wasn't toml file".to_string());
} }

View File

@ -97,10 +97,11 @@ pub async fn respond_hypertext(
pub struct StaticResponderConfig { pub struct StaticResponderConfig {
pub uri: String, pub uri: String,
pub utf8: bool, pub utf8: bool,
pub with_slash: bool,
pub browse: bool, pub browse: bool,
pub with_slash: bool,
pub index: Option<String>, pub index: Option<String>,
pub fallback: Option<String>, pub fallback: Option<String>,
pub suffix: Option<String>,
} }
pub async fn respond_static( pub async fn respond_static(
@ -142,6 +143,22 @@ pub async fn respond_static(
} }
if !file_path.exists() { if !file_path.exists() {
if let Some(suffix) = cfg.suffix {
let file_name = file_path
.file_name()
.and_then(OsStr::to_str)
.unwrap()
.to_string();
file_path.pop();
file_path.push((file_name + &suffix).as_str());
if file_path.is_file() {
return Ok(StaticFileRequest::from_request_without_body(req)
.await?
.create_response(&file_path, cfg.utf8)?
.into_response());
}
}
if let Some(file) = cfg.fallback { if let Some(file) = cfg.fallback {
let fallback_path = base_path.join(file); let fallback_path = base_path.join(file);
if fallback_path.is_file() { if fallback_path.is_file() {