diff options
| author | Matthew Holt <mholt@users.noreply.github.com> | 2022-07-31 21:41:26 -0600 | 
|---|---|---|
| committer | Matthew Holt <mholt@users.noreply.github.com> | 2022-07-31 21:41:26 -0600 | 
| commit | ebd6abcbd5f6b31b7b4cadca0a546d9f0eab9ad3 (patch) | |
| tree | 58646171cd0d6311f3b8292418e098349c87c1e0 /modules/caddyhttp/fileserver | |
| parent | 6668271661857b2cc43143ff65edf1071013e67e (diff) | |
fileserver: Support virtual file system in Caddyfile
Diffstat (limited to 'modules/caddyhttp/fileserver')
| -rw-r--r-- | modules/caddyhttp/fileserver/caddyfile.go | 22 | 
1 files changed, 22 insertions, 0 deletions
diff --git a/modules/caddyhttp/fileserver/caddyfile.go b/modules/caddyhttp/fileserver/caddyfile.go index 2641535..a3cf9e4 100644 --- a/modules/caddyhttp/fileserver/caddyfile.go +++ b/modules/caddyhttp/fileserver/caddyfile.go @@ -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" @@ -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...> @@ -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 {  | 
