Skip to content

Commit

Permalink
fix(handlers): root path "/" without filename in serve
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 3, 2023
1 parent c90addf commit df8deec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Here you can find a lot of small crabs 🦀.

## Usage

### Run it
### Run it in `viz` directory

```console
$ cargo run --bin hello-world -- --nocapture
Expand Down
6 changes: 5 additions & 1 deletion examples/static-files/serve/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ async fn main() -> Result<()> {
let listener = TcpListener::bind(addr).await?;
println!("listening on http://{addr}");

// in `viz` directory
let dir = env::current_dir().unwrap();

let app = Router::new()
.get("/", index)
.get("/cargo.toml", serve::File::new(dir.join("Cargo.toml")))
.get("/examples/*", serve::Dir::new(dir).listing())
.get(
"/examples/*",
serve::Dir::new(dir.join("examples")).listing(),
)
.any("/*", |_| async { Ok(Response::text("Welcome!")) });
let tree = Arc::new(Tree::from(app));

Expand Down
10 changes: 7 additions & 3 deletions viz-handlers/src/serve/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
ffi::OsStr,
fmt::{Display, Formatter, Result},
fs::read_dir,
path::PathBuf,
path::{Path, PathBuf},
str::FromStr,
string::ToString,
};
Expand All @@ -24,7 +24,7 @@ impl Directory {
pub(crate) fn new(
base: &str,
prev: bool,
root: &std::path::Path,
root: &Path,
unlisted: &Option<Vec<&'static str>>,
) -> Option<Directory> {
let mut entries = read_dir(root).ok()?;
Expand Down Expand Up @@ -66,7 +66,11 @@ impl Directory {
0,
(
parent.join("").to_str()?.strip_prefix('/')?.to_string(),
parent.file_name().and_then(OsStr::to_str)?.to_string(),
parent
.file_name()
.and_then(OsStr::to_str)
.unwrap_or("")
.to_string(),
false,
None,
"..".to_string(),
Expand Down

0 comments on commit df8deec

Please sign in to comment.