@marcel91200 / caddy Mirror · read-only / commits / ebd6abcb
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 15:30 UTC

fileserver: Support virtual file system in Caddyfile

Matthew Holt committed Jul 31, 2022 at 21:41 UTC ebd6abcbd5f6b31b7b4cadca0a546d9f0eab9ad3
1 file changed +22
modules/caddyhttp/fileserver/caddyfile.go
+22
@@ -15,11 +15,13 @@
15 package fileserver
16
17 import (
18 + "io/fs"
19 "path/filepath"
20 "strings"
21
22 "github.com/caddyserver/caddy/v2"
23 "github.com/caddyserver/caddy/v2/caddyconfig"
24 + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
25 "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
26 "github.com/caddyserver/caddy/v2/modules/caddyhttp"
27 "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
@@ -35,6 +37,7 @@ func init() {
37 // server and configures it with this syntax:
38 //
39 // file_server [<matcher>] [browse] {
40 +// fs <backend...>
41 // root <path>
42 // hide <files...>
43 // index <files...>
@@ -62,6 +65,25 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
65
66 for h.NextBlock(0) {
67 switch h.Val() {
68 + case "fs":
69 + if !h.NextArg() {
70 + return nil, h.ArgErr()
71 + }
72 + if fsrv.FileSystemRaw != nil {
73 + return nil, h.Err("file system module already specified")
74 + }
75 + name := h.Val()
76 + modID := "caddy.fs." + name
77 + unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
78 + if err != nil {
79 + return nil, err
80 + }
81 + statFS, ok := unm.(fs.StatFS)
82 + if !ok {
83 + return nil, h.Errf("module %s (%T) is not a supported file system implementation (requires fs.StatFS)", modID, unm)
84 + }
85 + fsrv.FileSystemRaw = caddyconfig.JSONModuleObject(statFS, "backend", name, nil)
86 +
87 case "hide":
88 fsrv.Hide = h.RemainingArgs()
89 if len(fsrv.Hide) == 0 {