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

Support configurable backend for ServeDir #309

Open
davidpdrsn opened this issue Dec 2, 2022 · 1 comment · May be fixed by #313
Open

Support configurable backend for ServeDir #309

davidpdrsn opened this issue Dec 2, 2022 · 1 comment · May be fixed by #313
Assignees
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. E-hard Call for participation: Experience needed to fix: Hard / a lot

Comments

@davidpdrsn
Copy link
Member

davidpdrsn commented Dec 2, 2022

We've heard from several users who want to use ServeDir (or ServeFile) with files that are embedded in the binary, perhaps using rust-embed. By using ServeDir they'd get a lot for free such as finding the file from the path, range requests, pre-compressed files, etc.

That isn't possible today since ServeDir is hardcoded to always fetch files from the filesystem. I've been thinking that it could be solved by making a "backend" trait that tells ServeDir how to open files and get metadata. Something like:

#[async_trait]
pub trait Backend {
    type File: AsyncRead + AsyncSeek;
    type Metadata: FileMetadata;

    async fn open_file(&self, path: Path) -> io::Result<Self::File>;

    async fn metadata(&self, path: Path) -> io::Result<Self::Metadata>;
}

// we need a trait here because users cannot construct `std::fs::Metadata`
pub trait Metadata {
    // whatever methods we need
}

Then tower-http could provide a default backend that uses the file system but allow others to write their own.

This should be possible without breaking changes so not adding it to the 0.4 milestone.

@davidpdrsn davidpdrsn added C-feature-request Category: A feature request, i.e: not implemented / a PR. E-medium Call for participation: Experience needed to fix: Medium / intermediate labels Dec 2, 2022
@davidpdrsn davidpdrsn self-assigned this Dec 2, 2022
@davidpdrsn davidpdrsn added E-hard Call for participation: Experience needed to fix: Hard / a lot and removed E-medium Call for participation: Experience needed to fix: Medium / intermediate labels Dec 2, 2022
@davidpdrsn davidpdrsn linked a pull request Dec 2, 2022 that will close this issue
@Folyd
Copy link

Folyd commented Dec 8, 2022

Wow, thanks! This is an excellent feature I have wanted for a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. E-hard Call for participation: Experience needed to fix: Hard / a lot
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants