From b583007c49975c5c368630af30bbe3b116935df0 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 20 Mar 2020 12:50:36 -0600 Subject: httpcaddyfile: Simplify 'root' directive parsing I must have written that one before the helper function `RegisterHandlerDirective`. --- caddyconfig/httpcaddyfile/builtins.go | 60 +++++++++++------------------------ 1 file changed, 19 insertions(+), 41 deletions(-) (limited to 'caddyconfig/httpcaddyfile/builtins.go') diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go index a88c27c..2c5a2d7 100644 --- a/caddyconfig/httpcaddyfile/builtins.go +++ b/caddyconfig/httpcaddyfile/builtins.go @@ -15,7 +15,6 @@ package httpcaddyfile import ( - "encoding/json" "fmt" "html" "net/http" @@ -32,8 +31,8 @@ import ( func init() { RegisterDirective("bind", parseBind) - RegisterDirective("root", parseRoot) // TODO: isn't this a handler directive? RegisterDirective("tls", parseTLS) + RegisterHandlerDirective("root", parseRoot) RegisterHandlerDirective("redir", parseRedir) RegisterHandlerDirective("respond", parseRespond) RegisterHandlerDirective("route", parseRoute) @@ -54,45 +53,6 @@ func parseBind(h Helper) ([]ConfigValue, error) { return h.NewBindAddresses(lnHosts), nil } -// parseRoot parses the root directive. Syntax: -// -// root [] -// -func parseRoot(h Helper) ([]ConfigValue, error) { - if !h.Next() { - return nil, h.ArgErr() - } - - matcherSet, ok, err := h.MatcherToken() - if err != nil { - return nil, err - } - if !ok { - // no matcher token; oops - h.Dispenser.Prev() - } - - if !h.NextArg() { - return nil, h.ArgErr() - } - root := h.Val() - if h.NextArg() { - return nil, h.ArgErr() - } - - varsHandler := caddyhttp.VarsMiddleware{"root": root} - route := caddyhttp.Route{ - HandlersRaw: []json.RawMessage{ - caddyconfig.JSONModuleObject(varsHandler, "handler", "vars", nil), - }, - } - if matcherSet != nil { - route.MatcherSetsRaw = []caddy.ModuleMap{matcherSet} - } - - return []ConfigValue{{Class: "route", Value: route}}, nil -} - // parseTLS parses the tls directive. Syntax: // // tls [|internal]|[ ] { @@ -350,6 +310,24 @@ func parseTLS(h Helper) ([]ConfigValue, error) { return configVals, nil } +// parseRoot parses the root directive. Syntax: +// +// root [] +// +func parseRoot(h Helper) (caddyhttp.MiddlewareHandler, error) { + var root string + for h.Next() { + if !h.Next() { + return nil, h.ArgErr() + } + root = h.Val() + if h.NextArg() { + return nil, h.ArgErr() + } + } + return caddyhttp.VarsMiddleware{"root": root}, nil +} + // parseRedir parses the redir directive. Syntax: // // redir [] [] -- cgit v1.2.3