Skip to content

Support configurable backend for ServeDir #309

@davidpdrsn

Description

@davidpdrsn

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.

Metadata

Metadata

Assignees

Labels

C-feature-requestCategory: A feature request, i.e: not implemented / a PR.E-hardCall for participation: Experience needed to fix: Hard / a lot

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions