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

fileserver: Support virtual file systems #4909

Merged
merged 4 commits into from
Jul 30, 2022
Merged

fileserver: Support virtual file systems #4909

merged 4 commits into from
Jul 30, 2022

Conversation

mholt
Copy link
Member

@mholt mholt commented Jul 25, 2022

This change replaces the hard-coded use of os.Open() and os.Stat() with
the use of the new (Go 1.16) io/fs APIs, enabling virtual file systems.
It introduces a new module namespace, caddy.fs, for such file systems.

Also improve documentation for the file server. I realized it was one of
the first modules written for Caddy 2, and the docs hadn't really been
updated since!

Anyway, this is super cool, since it enables Caddy's file server to serve not-so-static files, or files on remote resources like cloud storage or network devices. (S3? Database? No problem now.) It can serve files from any virtual file system, i.e. any Go type that has Open() and Stat() methods. So even if the content isn't strictly static, if the virtual file system can assemble the contents with a call to Open(), Caddy doesn't care whether it's static or not. Dynamic file server!

The first caddy.fs module I might implement (other than the unexported, implicitly-enabled, 2-liner osFS in this PR) is archiver.FileSystem() so that Caddy can serve files from zip/tar archives including transparently decompressing its contents!

I might still need to tweak a few things about this before finalizing.

Closes #3720

This change replaces the hard-coded use of os.Open() and os.Stat() with
the use of the new (Go 1.16) io/fs APIs, enabling virtual file systems.
It introduces a new module namespace, caddy.fs, for such file systems.

Also improve documentation for the file server. I realized it was one of
the first modules written for Caddy 2, and the docs hadn't really been
updated since!
@mholt mholt added the feature ⚙️ New feature or request label Jul 25, 2022
@mholt mholt added this to the v2.6.0 milestone Jul 25, 2022
@mholt mholt requested a review from francislavoie July 25, 2022 05:07
@mholt mholt merged commit 6668271 into master Jul 30, 2022
@mholt mholt deleted the fs branch July 30, 2022 19:07
@mholt
Copy link
Member Author

mholt commented Aug 1, 2022

Caddyfile support added here: ebd6abc

@robbymilo
Copy link

@mholt New to caddy and don't see this mentioned in the docs - do you have an example on how to serve content directly from an archive file (e.g. .zip or .tar.gz)?

@mholt
Copy link
Member Author

mholt commented Sep 26, 2022

@robbymilo Someone will need to write a Caddy plugin to do it. I'd recommend using https://github.com/mholt/archiver.

I think @WeidiDeng has been looking into this, or maybe even already done it!

@WeidiDeng
Copy link
Member

It's very easy to write one, basically replace embed.FS in caddy-embed with archive.FS, and create filesystem from archive file path.

ArchiveFS is too experimental to be used in production systems imo, afero is an alternative that I use in my custom file server.

@mholt
Copy link
Member Author

mholt commented Sep 27, 2022

@WeidiDeng ArchiveFS is new, but I don't want to discourage people from using it, otherwise it'll never be "production ready" 🙃

Thanks for helping me improve it -- I need to get back to those PRs soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Operating system interface for file server
4 participants