Skip to content

Commit a3c9ae3

Browse files
authored
Merge pull request #795 from u5surf/issue-777
bugfix : serve_dir doesn't precede to other route
2 parents 5383e17 + 47e0f25 commit a3c9ae3

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

examples/static_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ async fn main() -> Result<(), std::io::Error> {
33
tide::log::start();
44
let mut app = tide::new();
55
app.at("/").get(|_| async { Ok("visit /src/*") });
6-
app.at("/src").serve_dir("src/")?;
6+
app.at("/src/*").serve_dir("src/")?;
77
app.at("/example").serve_file("examples/static_file.html")?;
88
app.listen("127.0.0.1:8080").await?;
99
Ok(())

src/fs/serve_dir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ where
2525
{
2626
async fn call(&self, req: Request<State>) -> Result {
2727
let path = req.url().path();
28-
let path = path.strip_prefix(&self.prefix).unwrap();
28+
let path = path
29+
.strip_prefix(&self.prefix.trim_end_matches('*'))
30+
.unwrap();
2931
let path = path.trim_start_matches('/');
3032
let mut file_path = self.dir.clone();
3133
for p in Path::new(path) {

src/route.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State> {
127127
/// #[async_std::main]
128128
/// async fn main() -> Result<(), std::io::Error> {
129129
/// let mut app = tide::new();
130-
/// app.at("/images").serve_dir("public/images/")?;
130+
/// app.at("/images/*").serve_dir("public/images/")?;
131131
/// app.listen("127.0.0.1:8080").await?;
132132
/// Ok(())
133133
/// }
@@ -136,7 +136,7 @@ impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State> {
136136
// Verify path exists, return error if it doesn't.
137137
let dir = dir.as_ref().to_owned().canonicalize()?;
138138
let prefix = self.path().to_string();
139-
self.at("*").get(ServeDir::new(prefix, dir));
139+
self.get(ServeDir::new(prefix, dir));
140140
Ok(())
141141
}
142142

tests/serve_dir.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ use tide::{http, Result, Server};
33
use std::fs::{self, File};
44
use std::io::Write;
55

6+
fn api() -> Box<dyn tide::Endpoint<()>> {
7+
Box::new(|_| async { Ok("api") })
8+
}
9+
610
fn app(tempdir: &tempfile::TempDir) -> Result<Server<()>> {
711
let static_dir = tempdir.path().join("static");
812
fs::create_dir(&static_dir)?;
@@ -12,7 +16,9 @@ fn app(tempdir: &tempfile::TempDir) -> Result<Server<()>> {
1216
write!(file, "Foobar")?;
1317

1418
let mut app = Server::new();
15-
app.at("/static/").serve_dir(static_dir.to_str().unwrap())?;
19+
app.at("/static/api").get(api());
20+
app.at("/static/*")
21+
.serve_dir(static_dir.to_str().unwrap())?;
1622

1723
Ok(app)
1824
}
@@ -41,3 +47,13 @@ async fn not_found() {
4147

4248
assert_eq!(res.status(), 404);
4349
}
50+
51+
#[async_std::test]
52+
async fn static_endpoint_mixin() {
53+
let tempdir = tempfile::tempdir().unwrap();
54+
let app = app(&tempdir).unwrap();
55+
let mut res: http::Response = app.respond(request("api")).await.unwrap();
56+
57+
assert_eq!(res.status(), 200);
58+
assert_eq!(res.body_string().await.unwrap().as_str(), "api");
59+
}

0 commit comments

Comments
 (0)