Tide: Allow the routing methods to open a static file

Created on 25 Nov 2018  路  15Comments  路  Source: http-rs/tide

fn main() {
    let mut app = tide::App::new(());

   // Set static files context
    app.middleware(
          UrlPath::new()
            .prefix("/static/content/web/")
            .suffix(".html")
    );

  app.at("/").get(async || resource("index"));
  app.serve("127.0.0.1:7878");
}

P.S. Something similar to Rocket's NamedFile

design feature

Most helpful comment

I implemented something with an API in the same vein as what @DCJanus suggested. It is going to need a lot more configuration options, but it already works.

Here's the repo: https://github.com/tomhoule/tide-static-files

All 15 comments

Currently, middleware don't have any access to the Router, so this approach isn't possible. I think an approach that would work would be through configuration, as you would set up the static file serving up during server setup.

Looking at how it is done in rocket and actix, it seems that they are following something similar, both of them having internal configuration, rather than modifying shared configuration.

Maybe this feature could be implemented via Endpoint. Just like actix-web::fs::StaticFiles

    let mut app = tide::App::new(());
    app.at("/static/*").get(StaticFileEndpoint {
        root_path: PathBuf::from("./static/content/web"),
        suffix: ".html".to_string(), // maybe more configuration
    });

    let address = "127.0.0.1:8000".to_owned();
    println!("Server is listening on http://{}", address);
    app.serve(address)

I implemented something with an API in the same vein as what @DCJanus suggested. It is going to need a lot more configuration options, but it already works.

Here's the repo: https://github.com/tomhoule/tide-static-files

Great job for @tomhoule, but for prod, there is still a lot of other work to be done.

For example:

  • Range header(single range and multi range)
  • Content-Disposition header(Non-ASCII support?)
  • Better performance(less malloc or zero-copy by sendfile)

I was working on this too, but recently, I got a lot of job to be done, may not be able to complete this work.

For Range header parse, I wrote a simple libary via pest, might be help for you, @tomhoule

Thanks! Indeed, there's a lot of work ahead. I wanted to release early to get some feedback and not pour too much work into something that nobody will use. The three points you listed seem important, among others. Do you want to open issues for them? (otherwise I can do it of course).

@tomhoule My English is terrible, it's too hard to open such issue for me. :sob:

No worries, I can do it :) Feel free to open issues just to drop ideas without explaining at length, that helps a lot too.

edit: issues in other languages are ok too (I can read chinese)

I was going to develop such Endpoint in tide, so I forked tide and pushed some code to dcjanus/tide.

And then, inspired by tomhoule, separated repo might be better.

So I moved my code from dcjanus/tide to dcjanus/tide-static-file.

PS: new repo inspired a lot from @tomhoule

I think we should join efforts on one crate. @DCjanus has more features so we could use this crate as a starting point. I read the implementation and it looks like it's using blocking file operations without a thread pool, though, so that should probably be fixed.

I can contribute this part if we decide to standardize on this crate.

To be honest, for me, this repo is just for fun.
For me, it is very difficult to participate in this project steadily. I am willing to transfer this project to someone or contribute my code to @tomhoule's repo or add @tomhoule as collaborator.Maybe, participate in the development if I have time.
That's my bad to talk about my personal project in this issue, I am so sorry.

With the latest changes in the revamp PR it became possible to make tide-static-files work again with the new abstractions.

from what I understand, this issue is about serving a single file from within an endpoint, correct? Tide now supports serving static files under a directory...

app.at("/public/images").serve_dir("images/")?;

but maybe there's potentially a case where a user would actually want to handle this within an endpoint.

Isn't this covered with the serve_file method that was added recently?

Yeah I think this is probably covered by https://github.com/http-rs/tide/pull/725

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aturon picture aturon  路  6Comments

yoshuawuyts picture yoshuawuyts  路  6Comments

jbr picture jbr  路  4Comments

alexreg picture alexreg  路  5Comments

cquintana-verbio picture cquintana-verbio  路  6Comments