Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
httpcaddyfile: Simplify 'root' directive parsing
I must have written that one before the helper function `RegisterHandlerDirective`.
Matthew Holt committed
Mar 20, 2020 at 12:50 UTC
b583007c49975c5c368630af30bbe3b116935df0
1 file changed
+19
-41
caddyconfig/httpcaddyfile/builtins.go
+19
-41
@@ -15,7 +15,6 @@
15
package httpcaddyfile
16
17
import (
18
- "encoding/json"
18
"fmt"
19
"html"
20
"net/http"
@@ -32,8 +31,8 @@ import (
31
32
func init() {
33
RegisterDirective("bind", parseBind)
35
- RegisterDirective("root", parseRoot) // TODO: isn't this a handler directive?
34
RegisterDirective("tls", parseTLS)
35
+ RegisterHandlerDirective("root", parseRoot)
36
RegisterHandlerDirective("redir", parseRedir)
37
RegisterHandlerDirective("respond", parseRespond)
38
RegisterHandlerDirective("route", parseRoute)
@@ -54,45 +53,6 @@ func parseBind(h Helper) ([]ConfigValue, error) {
53
return h.NewBindAddresses(lnHosts), nil
54
}
55
57
-// parseRoot parses the root directive. Syntax:
58
-//
59
-// root [<matcher>] <path>
60
-//
61
-func parseRoot(h Helper) ([]ConfigValue, error) {
62
- if !h.Next() {
63
- return nil, h.ArgErr()
64
- }
65
-
66
- matcherSet, ok, err := h.MatcherToken()
67
- if err != nil {
68
- return nil, err
69
- }
70
- if !ok {
71
- // no matcher token; oops
72
- h.Dispenser.Prev()
73
- }
74
-
75
- if !h.NextArg() {
76
- return nil, h.ArgErr()
77
- }
78
- root := h.Val()
79
- if h.NextArg() {
80
- return nil, h.ArgErr()
81
- }
82
-
83
- varsHandler := caddyhttp.VarsMiddleware{"root": root}
84
- route := caddyhttp.Route{
85
- HandlersRaw: []json.RawMessage{
86
- caddyconfig.JSONModuleObject(varsHandler, "handler", "vars", nil),
87
- },
88
- }
89
- if matcherSet != nil {
90
- route.MatcherSetsRaw = []caddy.ModuleMap{matcherSet}
91
- }
92
-
93
- return []ConfigValue{{Class: "route", Value: route}}, nil
94
-}
95
-
56
// parseTLS parses the tls directive. Syntax:
57
//
58
// tls [<email>|internal]|[<cert_file> <key_file>] {
@@ -350,6 +310,24 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
310
return configVals, nil
311
}
312
313
+// parseRoot parses the root directive. Syntax:
314
+//
315
+// root [<matcher>] <path>
316
+//
317
+func parseRoot(h Helper) (caddyhttp.MiddlewareHandler, error) {
318
+ var root string
319
+ for h.Next() {
320
+ if !h.Next() {
321
+ return nil, h.ArgErr()
322
+ }
323
+ root = h.Val()
324
+ if h.NextArg() {
325
+ return nil, h.ArgErr()
326
+ }
327
+ }
328
+ return caddyhttp.VarsMiddleware{"root": root}, nil
329
+}
330
+
331
// parseRedir parses the redir directive. Syntax:
332
//
333
// redir [<matcher>] <to> [<code>]