Skip to content

Commit

Permalink
fileserver: Support virtual file system in Caddyfile
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Aug 1, 2022
1 parent 6668271 commit ebd6abc
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions modules/caddyhttp/fileserver/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package fileserver

import (
"io/fs"
"path/filepath"
"strings"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
Expand All @@ -35,6 +37,7 @@ func init() {
// server and configures it with this syntax:
//
// file_server [<matcher>] [browse] {
// fs <backend...>
// root <path>
// hide <files...>
// index <files...>
Expand Down Expand Up @@ -62,6 +65,25 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)

for h.NextBlock(0) {
switch h.Val() {
case "fs":
if !h.NextArg() {
return nil, h.ArgErr()
}
if fsrv.FileSystemRaw != nil {
return nil, h.Err("file system module already specified")
}
name := h.Val()
modID := "caddy.fs." + name
unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
if err != nil {
return nil, err
}
statFS, ok := unm.(fs.StatFS)
if !ok {
return nil, h.Errf("module %s (%T) is not a supported file system implementation (requires fs.StatFS)", modID, unm)
}
fsrv.FileSystemRaw = caddyconfig.JSONModuleObject(statFS, "backend", name, nil)

case "hide":
fsrv.Hide = h.RemainingArgs()
if len(fsrv.Hide) == 0 {
Expand Down

0 comments on commit ebd6abc

Please sign in to comment.