Implement some shorthand placeholders for Caddyfile

This commit is contained in:
Matthew Holt 2019-08-21 11:03:50 -06:00
parent d73b650c26
commit fa334c4bdf
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -17,7 +17,6 @@ package httpcaddyfile
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
@ -39,8 +38,6 @@ func init() {
type ServerType struct { type ServerType struct {
} }
// TODO: error on unrecognized directives
// Setup makes a config from the tokens. // Setup makes a config from the tokens.
func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock, func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
options map[string]string) (*caddy.Config, []caddyconfig.Warning, error) { options map[string]string) (*caddy.Config, []caddyconfig.Warning, error) {
@ -55,6 +52,27 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
} }
for _, sb := range serverBlocks { for _, sb := range serverBlocks {
// replace shorthand placeholders (which are
// convenient when writing a Caddyfile) with
// their actual placeholder identifiers or
// variable names
replacer := strings.NewReplacer(
"{uri}", "{http.request.uri}",
"{path}", "{http.request.uri.path}",
"{host}", "{http.request.host}",
"{hostport}", "{http.request.hostport}",
"{method}", "{http.request.method}",
"{scheme}", "{http.request.scheme}",
"{file}", "{http.request.uri.path.file}",
"{dir}", "{http.request.uri.path.dir}",
"{query}", "{http.request.uri.query}",
)
for _, segment := range sb.block.Segments {
for i := 0; i < len(segment); i++ {
segment[i].Text = replacer.Replace(segment[i].Text)
}
}
// extract matcher definitions // extract matcher definitions
d := sb.block.DispenseDirective("matcher") d := sb.block.DispenseDirective("matcher")
matcherDefs, err := st.parseMatcherDefinitions(d) matcherDefs, err := st.parseMatcherDefinitions(d)
@ -82,8 +100,8 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
sb.pile[result.Class] = append(sb.pile[result.Class], result) sb.pile[result.Class] = append(sb.pile[result.Class], result)
} }
} else { } else {
// TODO: this should be an error tkn := segment[0]
log.Printf("%s not registered", dir) return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
} }
} }
} }
@ -97,22 +115,6 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
// reduce // reduce
pairings := st.consolidateAddrMappings(sbmap) pairings := st.consolidateAddrMappings(sbmap)
// TODO: shorthand placeholders
// for _, p := range pairings {
// for _, sblock := range p.serverBlocks {
// for _, tokens := range sblock.Tokens {
// for i := 0; i < len(tokens); i++ {
// switch tokens[i].Text {
// case "{uri}":
// tokens[i].Text = "{http.request.uri}"
// case "{path}":
// tokens[i].Text = "{http.request.uri.path}"
// }
// }
// }
// }
// }
// each pairing of listener addresses to list of server // each pairing of listener addresses to list of server
// blocks is basically a server definition // blocks is basically a server definition
servers, err := st.serversFromPairings(pairings, &warnings) servers, err := st.serversFromPairings(pairings, &warnings)