diff options
author | a <[email protected]> | 2024-01-13 14:12:43 -0600 |
---|---|---|
committer | GitHub <[email protected]> | 2024-01-13 20:12:43 +0000 |
commit | c839a98ff527932fd14460829142c486f4531a7b (patch) | |
tree | 3a4a9745d2bc54ff557b4439ea0fe2dbc58238e0 /caddyconfig | |
parent | b359ca565c624b8718eac79058bff0591b250d0e (diff) | |
download | caddy-c839a98ff527932fd14460829142c486f4531a7b.tar.gz caddy-c839a98ff527932fd14460829142c486f4531a7b.zip |
filesystem: Globally declared filesystems, `fs` directive (#5833)
Diffstat (limited to 'caddyconfig')
-rw-r--r-- | caddyconfig/caddyfile/dispenser_test.go | 2 | ||||
-rw-r--r-- | caddyconfig/caddyfile/parse_test.go | 18 | ||||
-rw-r--r-- | caddyconfig/httpcaddyfile/builtins.go | 18 | ||||
-rw-r--r-- | caddyconfig/httpcaddyfile/directives.go | 1 | ||||
-rw-r--r-- | caddyconfig/httpcaddyfile/directives_test.go | 9 | ||||
-rw-r--r-- | caddyconfig/httpcaddyfile/httptype.go | 7 |
6 files changed, 41 insertions, 14 deletions
diff --git a/caddyconfig/caddyfile/dispenser_test.go b/caddyconfig/caddyfile/dispenser_test.go index b64a97354..0f6ee5043 100644 --- a/caddyconfig/caddyfile/dispenser_test.go +++ b/caddyconfig/caddyfile/dispenser_test.go @@ -305,7 +305,7 @@ func TestDispenser_ArgErr_Err(t *testing.T) { t.Errorf("Expected error message with custom message in it ('foobar'); got '%v'", err) } - var ErrBarIsFull = errors.New("bar is full") + ErrBarIsFull := errors.New("bar is full") bookingError := d.Errf("unable to reserve: %w", ErrBarIsFull) if !errors.Is(bookingError, ErrBarIsFull) { t.Errorf("Errf(): should be able to unwrap the error chain") diff --git a/caddyconfig/caddyfile/parse_test.go b/caddyconfig/caddyfile/parse_test.go index b270f1613..90f5095d4 100644 --- a/caddyconfig/caddyfile/parse_test.go +++ b/caddyconfig/caddyfile/parse_test.go @@ -22,7 +22,7 @@ import ( ) func TestParseVariadic(t *testing.T) { - var args = make([]string, 10) + args := make([]string, 10) for i, tc := range []struct { input string result bool @@ -111,7 +111,6 @@ func TestAllTokens(t *testing.T) { input := []byte("a b c\nd e") expected := []string{"a", "b", "c", "d", "e"} tokens, err := allTokens("TestAllTokens", input) - if err != nil { t.Fatalf("Expected no error, got %v", err) } @@ -149,10 +148,11 @@ func TestParseOneAndImport(t *testing.T) { "localhost", }, []int{1}}, - {`localhost:1234 + { + `localhost:1234 dir1 foo bar`, false, []string{ - "localhost:1234", - }, []int{3}, + "localhost:1234", + }, []int{3}, }, {`localhost { @@ -407,13 +407,13 @@ func TestRecursiveImport(t *testing.T) { err = os.WriteFile(recursiveFile1, []byte( `localhost dir1 - import recursive_import_test2`), 0644) + import recursive_import_test2`), 0o644) if err != nil { t.Fatal(err) } defer os.Remove(recursiveFile1) - err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0644) + err = os.WriteFile(recursiveFile2, []byte("dir2 1"), 0o644) if err != nil { t.Fatal(err) } @@ -441,7 +441,7 @@ func TestRecursiveImport(t *testing.T) { err = os.WriteFile(recursiveFile1, []byte( `localhost dir1 - import `+recursiveFile2), 0644) + import `+recursiveFile2), 0o644) if err != nil { t.Fatal(err) } @@ -495,7 +495,7 @@ func TestDirectiveImport(t *testing.T) { } err = os.WriteFile(directiveFile, []byte(`prop1 1 - prop2 2`), 0644) + prop2 2`), 0o644) if err != nil { t.Fatal(err) } diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go index 701beff7d..5bfe434cb 100644 --- a/caddyconfig/httpcaddyfile/builtins.go +++ b/caddyconfig/httpcaddyfile/builtins.go @@ -40,6 +40,7 @@ import ( func init() { RegisterDirective("bind", parseBind) RegisterDirective("tls", parseTLS) + RegisterHandlerDirective("fs", parseFilesystem) RegisterHandlerDirective("root", parseRoot) RegisterHandlerDirective("vars", parseVars) RegisterHandlerDirective("redir", parseRedir) @@ -658,6 +659,23 @@ func parseRoot(h Helper) (caddyhttp.MiddlewareHandler, error) { return caddyhttp.VarsMiddleware{"root": root}, nil } +// parseFilesystem parses the fs directive. Syntax: +// +// fs <filesystem> +func parseFilesystem(h Helper) (caddyhttp.MiddlewareHandler, error) { + var name string + for h.Next() { + if !h.NextArg() { + return nil, h.ArgErr() + } + name = h.Val() + if h.NextArg() { + return nil, h.ArgErr() + } + } + return caddyhttp.VarsMiddleware{"fs": name}, nil +} + // parseVars parses the vars directive. See its UnmarshalCaddyfile method for syntax. func parseVars(h Helper) (caddyhttp.MiddlewareHandler, error) { v := new(caddyhttp.VarsMiddleware) diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go index 13229ed5c..58da2bd79 100644 --- a/caddyconfig/httpcaddyfile/directives.go +++ b/caddyconfig/httpcaddyfile/directives.go @@ -41,6 +41,7 @@ var directiveOrder = []string{ "map", "vars", + "fs", "root", "skip_log", diff --git a/caddyconfig/httpcaddyfile/directives_test.go b/caddyconfig/httpcaddyfile/directives_test.go index e46a6d2af..db0282299 100644 --- a/caddyconfig/httpcaddyfile/directives_test.go +++ b/caddyconfig/httpcaddyfile/directives_test.go @@ -31,20 +31,23 @@ func TestHostsFromKeys(t *testing.T) { []Address{ {Original: ":2015", Port: "2015"}, }, - []string{}, []string{}, + []string{}, + []string{}, }, { []Address{ {Original: ":443", Port: "443"}, }, - []string{}, []string{}, + []string{}, + []string{}, }, { []Address{ {Original: "foo", Host: "foo"}, {Original: ":2015", Port: "2015"}, }, - []string{}, []string{"foo"}, + []string{}, + []string{"foo"}, }, { []Address{ diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index cb3095a16..bc2b125ef 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -271,6 +271,12 @@ func (st ServerType) Setup( if !reflect.DeepEqual(pkiApp, &caddypki.PKI{CAs: make(map[string]*caddypki.CA)}) { cfg.AppsRaw["pki"] = caddyconfig.JSON(pkiApp, &warnings) } + if filesystems, ok := options["filesystem"].(caddy.Module); ok { + cfg.AppsRaw["caddy.filesystems"] = caddyconfig.JSON( + filesystems, + &warnings) + } + if storageCvtr, ok := options["storage"].(caddy.StorageConverter); ok { cfg.StorageRaw = caddyconfig.JSONModuleObject(storageCvtr, "module", @@ -280,7 +286,6 @@ func (st ServerType) Setup( if adminConfig, ok := options["admin"].(*caddy.AdminConfig); ok && adminConfig != nil { cfg.Admin = adminConfig } - if pc, ok := options["persist_config"].(string); ok && pc == "off" { if cfg.Admin == nil { cfg.Admin = new(caddy.AdminConfig) |