From ba29f9d41d7951bde07dc2e0d6a1471c40bb7145 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 19 Sep 2019 12:42:36 -0600 Subject: httpcaddyfile: Global storage configuration (closes #2758) --- caddyconfig/httpcaddyfile/httptype.go | 14 +++++++++---- caddyconfig/httpcaddyfile/options.go | 38 +++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 8 deletions(-) (limited to 'caddyconfig') diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index 6b2ee6b..ff340de 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -57,15 +57,18 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock, dir := segment.Directive() var val interface{} var err error + disp := caddyfile.NewDispenser(segment) switch dir { case "http_port": - val, err = parseHTTPPort(caddyfile.NewDispenser(segment)) + val, err = parseOptHTTPPort(disp) case "https_port": - val, err = parseHTTPSPort(caddyfile.NewDispenser(segment)) + val, err = parseOptHTTPSPort(disp) case "handler_order": - val, err = parseHandlerOrder(caddyfile.NewDispenser(segment)) + val, err = parseOptHandlerOrder(disp) case "experimental_http3": - val, err = parseExperimentalHTTP3(caddyfile.NewDispenser(segment)) + val, err = parseOptExperimentalHTTP3(disp) + case "storage": + val, err = parseOptStorage(disp) default: return nil, warnings, fmt.Errorf("unrecognized parameter name: %s", dir) } @@ -207,6 +210,9 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock, if !reflect.DeepEqual(tlsApp, caddytls.TLS{}) { cfg.AppsRaw["tls"] = caddyconfig.JSON(tlsApp, &warnings) } + if storageCvtr, ok := options["storage"].(caddy.StorageConverter); ok { + cfg.StorageRaw = caddyconfig.JSON(storageCvtr, &warnings) + } return cfg, warnings, nil } diff --git a/caddyconfig/httpcaddyfile/options.go b/caddyconfig/httpcaddyfile/options.go index 1d7864c..dadde28 100644 --- a/caddyconfig/httpcaddyfile/options.go +++ b/caddyconfig/httpcaddyfile/options.go @@ -15,12 +15,14 @@ package httpcaddyfile import ( + "fmt" "strconv" + "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" ) -func parseHTTPPort(d *caddyfile.Dispenser) (int, error) { +func parseOptHTTPPort(d *caddyfile.Dispenser) (int, error) { var httpPort int for d.Next() { var httpPortStr string @@ -36,7 +38,7 @@ func parseHTTPPort(d *caddyfile.Dispenser) (int, error) { return httpPort, nil } -func parseHTTPSPort(d *caddyfile.Dispenser) (int, error) { +func parseOptHTTPSPort(d *caddyfile.Dispenser) (int, error) { var httpsPort int for d.Next() { var httpsPortStr string @@ -52,11 +54,11 @@ func parseHTTPSPort(d *caddyfile.Dispenser) (int, error) { return httpsPort, nil } -func parseExperimentalHTTP3(d *caddyfile.Dispenser) (bool, error) { +func parseOptExperimentalHTTP3(d *caddyfile.Dispenser) (bool, error) { return true, nil } -func parseHandlerOrder(d *caddyfile.Dispenser) ([]string, error) { +func parseOptHandlerOrder(d *caddyfile.Dispenser) ([]string, error) { if !d.Next() { return nil, d.ArgErr() } @@ -78,3 +80,31 @@ func parseHandlerOrder(d *caddyfile.Dispenser) ([]string, error) { } return order, nil } + +func parseOptStorage(d *caddyfile.Dispenser) (caddy.StorageConverter, error) { + if !d.Next() { + return nil, d.ArgErr() + } + args := d.RemainingArgs() + if len(args) != 1 { + return nil, d.ArgErr() + } + modName := args[0] + mod, err := caddy.GetModule("caddy.storage." + modName) + if err != nil { + return nil, fmt.Errorf("getting storage module '%s': %v", modName, err) + } + unm, ok := mod.New().(caddyfile.Unmarshaler) + if !ok { + return nil, fmt.Errorf("storage module '%s' is not a Caddyfile unmarshaler", mod.Name) + } + err = unm.UnmarshalCaddyfile(d.NewFromNextTokens()) + if err != nil { + return nil, err + } + storage, ok := unm.(caddy.StorageConverter) + if !ok { + return nil, fmt.Errorf("module %s is not a StorageConverter", mod.Name) + } + return storage, nil +} -- cgit v1.2.3