summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-03-20 12:50:36 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-03-20 12:50:36 -0600
commitb583007c49975c5c368630af30bbe3b116935df0 (patch)
tree77facb97ecd0e48aa2596056f6c9c45d86da3c29
parent6b60a301c02222c483dc9e2a03077c03d2c6ef5b (diff)
httpcaddyfile: Simplify 'root' directive parsing
I must have written that one before the helper function `RegisterHandlerDirective`.
-rw-r--r--caddyconfig/httpcaddyfile/builtins.go60
1 files changed, 19 insertions, 41 deletions
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 [<matcher>] <path>
-//
-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 [<email>|internal]|[<cert_file> <key_file>] {
@@ -350,6 +310,24 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
return configVals, nil
}
+// parseRoot parses the root directive. Syntax:
+//
+// root [<matcher>] <path>
+//
+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 [<matcher>] <to> [<code>]