Support configurable backend for ServeDir
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.
Wow, thanks! This is an excellent feature I have wanted for a long time.
Is this issue still alive? I want to use files from rust-embed file system. Without custom backend for ServeDir, it's impossible.
Nobody is actively working on this, if that's what you mean.