lpar / gzipped

Replacement for golang http.FileServer which supports precompressed static assets.
BSD 3-Clause "New" or "Revised" License
94 stars 15 forks source link

Use with `embed.FS` #19

Open muety opened 2 years ago

muety commented 2 years ago

Awesome project, exactly what I was looking for! Would be cool, though, if you could pass an embed.FS (or even better, just any fs.FS) instead of a directory name to serve files from. What do you think?

lpar commented 2 years ago

Stdlib now has an http.FS adaptor to convert an fs.FS into an http.FileSystem, but unfortunately fs.FS doesn't support the file existence check gzipped.Dir does. There's fs.StatFS which adds Stat to the FS interface, but unfortunately embed.FS doesn't support that.

For embed.FS it should be pretty easy to write an adaptor which implements Exists(filename). For fs.FS in general, it's trickier, because the interface only has Open. Supporting fs.StatFS probably makes sense though.

muety commented 2 years ago

I think it should be quite easy to come up with a wrapper around any fs.FS that uses fs.Stat() for the existence check. From my understanding, that static function takes any fs.FS as an input, calls Stat() on it if it actually implements fs.StatFS and otherwise falls back to the (slower) way of Open()ing the file to get stat. But I might have gotten something wrong.

lpar commented 2 years ago

Aha, I didn't know about fs.Stat(). That makes things much easier. Yes, in that case a general purpose wrapper should be fairly easy.

I'm thinking that rather than change the API again, it might be better to add an alternative to gzipped.FileServer that takes any fs.FS and uses fs.Stat() internally. Subtree selection would still be possible using fs.Sub.