mirror of https://github.com/Tim-Paik/srv.git
0.1.6-beta
This commit is contained in:
parent
2806bf5119
commit
e68a1b7dd8
|
@ -2174,7 +2174,7 @@ checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "web"
|
name = "web"
|
||||||
version = "0.1.0-alpha"
|
version = "0.1.6-beta"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
|
|
@ -3,7 +3,7 @@ authors = ["Tim_Paik <timpaikc@outlook.com>"]
|
||||||
description = "simple http server written in rust"
|
description = "simple http server written in rust"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
name = "web"
|
name = "web"
|
||||||
version = "0.1.0-alpha"
|
version = "0.1.6-beta"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
|
|
122
src/main.rs
122
src/main.rs
|
@ -59,73 +59,93 @@ enum Resp {
|
||||||
|
|
||||||
#[catch(404)]
|
#[catch(404)]
|
||||||
async fn not_found(request: &rocket::Request<'_>) -> Resp {
|
async fn not_found(request: &rocket::Request<'_>) -> Resp {
|
||||||
|
let path = request.uri().path();
|
||||||
let root = std::env::var("ROOT").unwrap_or(".".to_string());
|
let root = std::env::var("ROOT").unwrap_or(".".to_string());
|
||||||
let root = Path::new(&root);
|
let root = Path::new(&root);
|
||||||
let localpath = request.uri().path().to_string();
|
let localpath = path.to_string();
|
||||||
let localpath = localpath[1..localpath.len()].to_string();
|
let localpath = localpath[1..localpath.len()].to_string();
|
||||||
// Remove the / in front of the path, if the path with / is spliced, the previous path will be ignored
|
// Remove the / in front of the path, if the path with / is spliced, the previous path will be ignored
|
||||||
let localpath = &root.join(localpath);
|
let localpath = &root.join(localpath);
|
||||||
|
// Show dotfiles, std::path::PathBuf does not match the url beginning with the dot
|
||||||
|
let show_dot_files = std::env::var("DOTFILES").unwrap_or("false".to_string()) == "true";
|
||||||
|
if localpath.is_file() && show_dot_files {
|
||||||
|
return Resp::File(NamedFile::open(localpath).await.ok());
|
||||||
|
}
|
||||||
// Single-Page Application support
|
// Single-Page Application support
|
||||||
if root.join("index.html").is_file()
|
if root.join("index.html").is_file()
|
||||||
&& std::env::var("SPA").unwrap_or("false".to_string()) == "true"
|
&& std::env::var("SPA").unwrap_or("false".to_string()) == "true"
|
||||||
{
|
{
|
||||||
return Resp::File(NamedFile::open(&root.join("index.html")).await.ok());
|
return Resp::File(NamedFile::open(&root.join("index.html")).await.ok());
|
||||||
}
|
}
|
||||||
// Show dotfiles, std::path::PathBuf does not match the url beginning with the dot
|
|
||||||
if localpath.is_file() && std::env::var("DOTFILES").unwrap_or("false".to_string()) == "true" {
|
|
||||||
return Resp::File(NamedFile::open(localpath).await.ok());
|
|
||||||
}
|
|
||||||
if !localpath.is_dir() {
|
if !localpath.is_dir() {
|
||||||
return Resp::NotFound("");
|
return Resp::NotFound("");
|
||||||
}
|
}
|
||||||
if !request.uri().path().ends_with("/") {
|
if !path.ends_with("/") {
|
||||||
println!("ok");
|
return Resp::Redirect(Redirect::to(path.to_string() + "/"));
|
||||||
return Resp::Redirect(Redirect::to(request.uri().path().to_string() + "/"));
|
|
||||||
}
|
}
|
||||||
let context = &IndexContext {
|
let mut context = IndexContext {
|
||||||
title: "title?",
|
title: "",
|
||||||
paths: vec!["target", "debug"],
|
paths: vec![],
|
||||||
dirs: vec![
|
dirs: vec![],
|
||||||
Dir {
|
files: vec![],
|
||||||
name: ".fingerprint".to_string(),
|
|
||||||
modified: chrono::Local::now().to_string(),
|
|
||||||
},
|
|
||||||
Dir {
|
|
||||||
name: "build".to_string(),
|
|
||||||
modified: chrono::Local::now().to_string(),
|
|
||||||
},
|
|
||||||
Dir {
|
|
||||||
name: "deps".to_string(),
|
|
||||||
modified: chrono::Local::now().to_string(),
|
|
||||||
},
|
|
||||||
Dir {
|
|
||||||
name: "examples".to_string(),
|
|
||||||
modified: chrono::Local::now().to_string(),
|
|
||||||
},
|
|
||||||
Dir {
|
|
||||||
name: "incremental".to_string(),
|
|
||||||
modified: chrono::Local::now().to_string(),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
files: vec![
|
|
||||||
File {
|
|
||||||
name: ".cargo-lock".to_string(),
|
|
||||||
size: 0,
|
|
||||||
modified: chrono::Local::now().to_string(),
|
|
||||||
},
|
|
||||||
File {
|
|
||||||
name: "web".to_string(),
|
|
||||||
size: 142606336,
|
|
||||||
modified: chrono::Local::now().to_string(),
|
|
||||||
},
|
|
||||||
File {
|
|
||||||
name: "web.d".to_string(),
|
|
||||||
size: 88,
|
|
||||||
modified: chrono::Local::now().naive_local().to_string(),
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
};
|
||||||
Resp::Index(Template::render("index", context))
|
for path in path.split('/') {
|
||||||
|
if path == "" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
context.paths.push(path.as_str());
|
||||||
|
}
|
||||||
|
match std::fs::read_dir(localpath) {
|
||||||
|
Err(e) => println!("{} {}", "Error".bright_red(), e.to_string()),
|
||||||
|
Ok(paths) => {
|
||||||
|
for path in paths {
|
||||||
|
let path = match path {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(e) => {
|
||||||
|
println!("{} {}", "Error".bright_red(), e.to_string());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let filename = match path.file_name().to_str() {
|
||||||
|
Some(str) => str.to_string(),
|
||||||
|
None => {
|
||||||
|
println!("{} {}", "Error".bright_red(), "Read filename error");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if !show_dot_files && filename.starts_with(".") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let metadata = match path.metadata() {
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(e) => {
|
||||||
|
println!("{} {}", "Error".bright_red(), e.to_string());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let modified = match metadata.modified() {
|
||||||
|
Ok(time) => chrono::DateTime::<chrono::Local>::from(time).to_string(),
|
||||||
|
Err(e) => {
|
||||||
|
println!("{} {}", "Error".bright_red(), e.to_string());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if metadata.is_dir() {
|
||||||
|
context.dirs.push(Dir {
|
||||||
|
name: filename,
|
||||||
|
modified: modified,
|
||||||
|
})
|
||||||
|
} else if metadata.is_file() {
|
||||||
|
context.files.push(File {
|
||||||
|
name: filename,
|
||||||
|
size: metadata.len(),
|
||||||
|
modified: modified,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Resp::Index(Template::render("index", &context))
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Logger {}
|
struct Logger {}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
th,
|
th,
|
||||||
td {
|
td {
|
||||||
|
height: 1.6rem;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
padding: 0.4rem 0;
|
padding: 0.4rem 0;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
|
|
Loading…
Reference in New Issue