Skip to content

Commit c839a98

Browse files
authored
filesystem: Globally declared filesystems, fs directive (#5833)
1 parent b359ca5 commit c839a98

30 files changed

+450
-219
lines changed

caddy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/google/uuid"
4040
"go.uber.org/zap"
4141

42+
"github.com/caddyserver/caddy/v2/internal/filesystems"
4243
"github.com/caddyserver/caddy/v2/notify"
4344
)
4445

@@ -84,6 +85,9 @@ type Config struct {
8485
storage certmagic.Storage
8586

8687
cancelFunc context.CancelFunc
88+
89+
// filesystems is a dict of filesystems that will later be loaded from and added to.
90+
filesystems FileSystems
8791
}
8892

8993
// App is a thing that Caddy runs.
@@ -447,6 +451,9 @@ func run(newCfg *Config, start bool) (Context, error) {
447451
}
448452
}
449453

454+
// create the new filesystem map
455+
newCfg.filesystems = &filesystems.FilesystemMap{}
456+
450457
// prepare the new config for use
451458
newCfg.apps = make(map[string]App)
452459

caddyconfig/caddyfile/dispenser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func TestDispenser_ArgErr_Err(t *testing.T) {
305305
t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err)
306306
}
307307

308-
var ErrBarIsFull = errors.New("bar is full")
308+
ErrBarIsFull := errors.New("bar is full")
309309
bookingError := d.Errf("unable to reserve: %w", ErrBarIsFull)
310310
if !errors.Is(bookingError, ErrBarIsFull) {
311311
t.Errorf("Errf(): should be able to unwrap the error chain")

caddyconfig/caddyfile/parse_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
func TestParseVariadic(t *testing.T) {
25-
var args = make([]string, 10)
25+
args := make([]string, 10)
2626
for i, tc := range []struct {
2727
input string
2828
result bool
@@ -111,7 +111,6 @@ func TestAllTokens(t *testing.T) {
111111
input := []byte("a b c\nd e")
112112
expected := []string{"a", "b", "c", "d", "e"}
113113
tokens, err := allTokens("TestAllTokens", input)
114-
115114
if err != nil {
116115
t.Fatalf("Expected no error, got %v", err)
117116
}
@@ -149,10 +148,11 @@ func TestParseOneAndImport(t *testing.T) {
149148
"localhost",
150149
}, []int{1}},
151150

152-
{`localhost:1234
151+
{
152+
`localhost:1234
153153
dir1 foo bar`, false, []string{
154-
"localhost:1234",
155-
}, []int{3},
154+
"localhost:1234",
155+
}, []int{3},
156156
},
157157

158158
{`localhost {
@@ -407,13 +407,13 @@ func TestRecursiveImport(t *testing.T) {
407407
err = os.WriteFile(recursiveFile1, []byte(
408408
`localhost
409409
dir1
410-
import recursive_import_test2`), 0644)
410+
import recursive_import_test2`), 0o644)
411411
if err != nil {
412412
t.Fatal(err)
413413
}
414414
defer os.Remove(recursiveFile1)
415415

416-
err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0644)
416+
err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0o644)
417417
if err != nil {
418418
t.Fatal(err)
419419
}
@@ -441,7 +441,7 @@ func TestRecursiveImport(t *testing.T) {
441441
err = os.WriteFile(recursiveFile1, []byte(
442442
`localhost
443443
dir1
444-
import `+recursiveFile2), 0644)
444+
import `+recursiveFile2), 0o644)
445445
if err != nil {
446446
t.Fatal(err)
447447
}
@@ -495,7 +495,7 @@ func TestDirectiveImport(t *testing.T) {
495495
}
496496

497497
err = os.WriteFile(directiveFile, []byte(`prop1 1
498-
prop2 2`), 0644)
498+
prop2 2`), 0o644)
499499
if err != nil {
500500
t.Fatal(err)
501501
}

caddyconfig/httpcaddyfile/builtins.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
func init() {
4141
RegisterDirective("bind", parseBind)
4242
RegisterDirective("tls", parseTLS)
43+
RegisterHandlerDirective("fs", parseFilesystem)
4344
RegisterHandlerDirective("root", parseRoot)
4445
RegisterHandlerDirective("vars", parseVars)
4546
RegisterHandlerDirective("redir", parseRedir)
@@ -658,6 +659,23 @@ func parseRoot(h Helper) (caddyhttp.MiddlewareHandler, error) {
658659
return caddyhttp.VarsMiddleware{"root": root}, nil
659660
}
660661

662+
// parseFilesystem parses the fs directive. Syntax:
663+
//
664+
// fs <filesystem>
665+
func parseFilesystem(h Helper) (caddyhttp.MiddlewareHandler, error) {
666+
var name string
667+
for h.Next() {
668+
if !h.NextArg() {
669+
return nil, h.ArgErr()
670+
}
671+
name = h.Val()
672+
if h.NextArg() {
673+
return nil, h.ArgErr()
674+
}
675+
}
676+
return caddyhttp.VarsMiddleware{"fs": name}, nil
677+
}
678+
661679
// parseVars parses the vars directive. See its UnmarshalCaddyfile method for syntax.
662680
func parseVars(h Helper) (caddyhttp.MiddlewareHandler, error) {
663681
v := new(caddyhttp.VarsMiddleware)

caddyconfig/httpcaddyfile/directives.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var directiveOrder = []string{
4141

4242
"map",
4343
"vars",
44+
"fs",
4445
"root",
4546
"skip_log",
4647

caddyconfig/httpcaddyfile/directives_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@ func TestHostsFromKeys(t *testing.T) {
3131
[]Address{
3232
{Original: ":2015", Port: "2015"},
3333
},
34-
[]string{}, []string{},
34+
[]string{},
35+
[]string{},
3536
},
3637
{
3738
[]Address{
3839
{Original: ":443", Port: "443"},
3940
},
40-
[]string{}, []string{},
41+
[]string{},
42+
[]string{},
4143
},
4244
{
4345
[]Address{
4446
{Original: "foo", Host: "foo"},
4547
{Original: ":2015", Port: "2015"},
4648
},
47-
[]string{}, []string{"foo"},
49+
[]string{},
50+
[]string{"foo"},
4851
},
4952
{
5053
[]Address{

caddyconfig/httpcaddyfile/httptype.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ func (st ServerType) Setup(
271271
if !reflect.DeepEqual(pkiApp, &caddypki.PKI{CAs: make(map[string]*caddypki.CA)}) {
272272
cfg.AppsRaw["pki"] = caddyconfig.JSON(pkiApp, &warnings)
273273
}
274+
if filesystems, ok := options["filesystem"].(caddy.Module); ok {
275+
cfg.AppsRaw["caddy.filesystems"] = caddyconfig.JSON(
276+
filesystems,
277+
&warnings)
278+
}
279+
274280
if storageCvtr, ok := options["storage"].(caddy.StorageConverter); ok {
275281
cfg.StorageRaw = caddyconfig.JSONModuleObject(storageCvtr,
276282
"module",
@@ -280,7 +286,6 @@ func (st ServerType) Setup(
280286
if adminConfig, ok := options["admin"].(*caddy.AdminConfig); ok && adminConfig != nil {
281287
cfg.Admin = adminConfig
282288
}
283-
284289
if pc, ok := options["persist_config"].(string); ok && pc == "off" {
285290
if cfg.Admin == nil {
286291
cfg.Admin = new(caddy.AdminConfig)

caddytest/integration/caddyfile_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
)
1010

1111
func TestRespond(t *testing.T) {
12-
1312
// arrange
1413
tester := caddytest.NewTester(t)
1514
tester.InitServer(`
@@ -32,7 +31,6 @@ func TestRespond(t *testing.T) {
3231
}
3332

3433
func TestRedirect(t *testing.T) {
35-
3634
// arrange
3735
tester := caddytest.NewTester(t)
3836
tester.InitServer(`
@@ -61,7 +59,6 @@ func TestRedirect(t *testing.T) {
6159
}
6260

6361
func TestDuplicateHosts(t *testing.T) {
64-
6562
// act and assert
6663
caddytest.AssertLoadError(t,
6764
`
@@ -76,7 +73,6 @@ func TestDuplicateHosts(t *testing.T) {
7673
}
7774

7875
func TestReadCookie(t *testing.T) {
79-
8076
localhost, _ := url.Parse("http://localhost")
8177
cookie := http.Cookie{
8278
Name: "clientname",
@@ -110,7 +106,6 @@ func TestReadCookie(t *testing.T) {
110106
}
111107

112108
func TestReplIndex(t *testing.T) {
113-
114109
tester := caddytest.NewTester(t)
115110
tester.InitServer(`
116111
{

caddytest/integration/reverseproxy_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func TestSRVReverseProxy(t *testing.T) {
5757
}
5858

5959
func TestDialWithPlaceholderUnix(t *testing.T) {
60-
6160
if runtime.GOOS == "windows" {
6261
t.SkipNow()
6362
}

caddytest/integration/sni_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
)
88

99
func TestDefaultSNI(t *testing.T) {
10-
1110
// arrange
1211
tester := caddytest.NewTester(t)
1312
tester.InitServer(`{
@@ -107,7 +106,6 @@ func TestDefaultSNI(t *testing.T) {
107106
}
108107

109108
func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) {
110-
111109
// arrange
112110
tester := caddytest.NewTester(t)
113111
tester.InitServer(`

0 commit comments

Comments
 (0)