Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve_dir doesn't seem to work and even panics in some cases #907

Open
darklajid opened this issue May 1, 2023 · 2 comments
Open

serve_dir doesn't seem to work and even panics in some cases #907

darklajid opened this issue May 1, 2023 · 2 comments

Comments

@darklajid
Copy link

A tiny sample project. I have

/src/main.rs
/static/index.html
/static/favicon.jpg

where main.rs is roughly this:

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::TRACE)
        .init();

    let mut app = tide::new();
    app.with(tide_tracing::TraceMiddleware::new());
    app.at("/*")
        .serve_dir("/home/me/foo/static/")?;
    app.at("/api").nest({
        let mut api = tide::new();
        api.at("/hello").get(|_| async { Ok("Hello, world") });
        api.at("/goodbye").get(|_| async { Ok("Goodbye, world") });
        api
    });
    app.listen("127.0.0.1:8080").await?;
    Ok(())
}

I tried serve_dir both with the absolute path (above) or (preferably) with a relative one. In both cases it doesn't work as expected (I expect / to serve /static/index.html and /favicon.jpg to resolve the same way).

I would instead get something like this:

2023-05-01T04:52:48.658475Z  INFO tide::log::middleware: <-- Request received    
2023-05-01T04:52:48.658572Z  INFO Request{http.method=GET http.target=/}: tide_tracing: received
2023-05-01T04:52:48.658649Z  WARN Request{http.method=GET http.target=/}:Response{http.status_code=404 http.duration=41.377µs}:Client error: tide_tracing: sent
2023-05-01T04:52:48.658699Z  WARN tide::log::middleware: Client error --> Response sent  

Whatever I try, tide just returns a 404 - which doesn't make sense to me at all. Worse though: While experimenting and trying to figure out what's going on, I stumbled upon this: Requesting /static/index.html causes a panic and a 500 handling the request, which is arguably worse than the confusing 404:

2023-05-01T04:53:02.376075Z  INFO tide::log::middleware: <-- Request received    
2023-05-01T04:53:02.376098Z  INFO Request{http.method=GET http.target=/static/index.html}: tide_tracing: received
thread 'async-std/runtime' panicked at 'called `Option::unwrap()` on a `None` value', /home/bam/.cargo/registry/src/github.com-1ecc6299db9ec823/tide-0.16.0/src/fs/serve_dir.rs:28:52
@AustinHellerRepo
Copy link

AustinHellerRepo commented Jun 21, 2023

I'm having the same issue. At first I was getting 404 errors, but after changing the serve directory from ./static/ to ./src/static/, now I'm getting the same panics as @darklajid.

After pulling down my own copy of tide and looking through the code, I think it may have to do with the usage of strip_prefix within the ServeDir's call function. It's an "unstable" feature atm and so I think it's returning None during "stable" usage.

A quick work-around for those that are having this issue:

  • Fork the repo
  • Replace your dependency to be tide = { git = "https://github.com/[your repo name]/tide" }

@Aierie
Copy link

Aierie commented Aug 15, 2023

strip_prefix for me seems to be trying to strip the wildcard (*) from the path as well. It was possible for me to circumvent this issue by doing app.at("/").serve_dir("my_dir") instead of app.at("/*").serve_dir("my_dir"). I think the examples in the default branch are not correct for tide 0.16.0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants