mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-22 16:46:53 +01:00
httpcaddyfile: Simplify 'root' directive parsing
I must have written that one before the helper function `RegisterHandlerDirective`.
This commit is contained in:
parent
6b60a301c0
commit
b583007c49
1 changed files with 19 additions and 41 deletions
|
@ -15,7 +15,6 @@
|
||||||
package httpcaddyfile
|
package httpcaddyfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -32,8 +31,8 @@ import (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterDirective("bind", parseBind)
|
RegisterDirective("bind", parseBind)
|
||||||
RegisterDirective("root", parseRoot) // TODO: isn't this a handler directive?
|
|
||||||
RegisterDirective("tls", parseTLS)
|
RegisterDirective("tls", parseTLS)
|
||||||
|
RegisterHandlerDirective("root", parseRoot)
|
||||||
RegisterHandlerDirective("redir", parseRedir)
|
RegisterHandlerDirective("redir", parseRedir)
|
||||||
RegisterHandlerDirective("respond", parseRespond)
|
RegisterHandlerDirective("respond", parseRespond)
|
||||||
RegisterHandlerDirective("route", parseRoute)
|
RegisterHandlerDirective("route", parseRoute)
|
||||||
|
@ -54,45 +53,6 @@ func parseBind(h Helper) ([]ConfigValue, error) {
|
||||||
return h.NewBindAddresses(lnHosts), nil
|
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:
|
// parseTLS parses the tls directive. Syntax:
|
||||||
//
|
//
|
||||||
// tls [<email>|internal]|[<cert_file> <key_file>] {
|
// tls [<email>|internal]|[<cert_file> <key_file>] {
|
||||||
|
@ -350,6 +310,24 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
|
||||||
return configVals, nil
|
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:
|
// parseRedir parses the redir directive. Syntax:
|
||||||
//
|
//
|
||||||
// redir [<matcher>] <to> [<code>]
|
// redir [<matcher>] <to> [<code>]
|
||||||
|
|
Loading…
Reference in a new issue