summaryrefslogtreecommitdiff
path: root/caddyconfig
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-08-22 14:26:33 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-08-22 14:26:33 -0600
commite34ff21a7183969afb4150f1f7a1f71b9a1dd0d6 (patch)
tree0da2e377a92a1f80468ef2dba0a81f4cfd939fae /caddyconfig
parentaf25f0254e94eb2e2898ba495bfc3de209a49e79 (diff)
caddyfile: Allow handler order to be customized
Diffstat (limited to 'caddyconfig')
-rw-r--r--caddyconfig/httpcaddyfile/httptype.go29
-rw-r--r--caddyconfig/httpcaddyfile/options.go (renamed from caddyconfig/httpcaddyfile/global.go)23
2 files changed, 42 insertions, 10 deletions
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index 047d559..ecf6e94 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -66,6 +66,8 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
val, err = parseHTTPPort(caddyfile.NewDispenser(segment))
case "https_port":
val, err = parseHTTPSPort(caddyfile.NewDispenser(segment))
+ case "handler_order":
+ val, err = parseHandlerOrder(caddyfile.NewDispenser(segment))
default:
return nil, warnings, fmt.Errorf("unrecognized parameter name: %s", dir)
}
@@ -148,7 +150,7 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
// each pairing of listener addresses to list of server
// blocks is basically a server definition
- servers, err := st.serversFromPairings(pairings, &warnings)
+ servers, err := st.serversFromPairings(pairings, options, &warnings)
if err != nil {
return nil, warnings, err
}
@@ -230,7 +232,11 @@ func (st *ServerType) hostsFromServerBlockKeys(sb caddyfile.ServerBlock) ([]stri
// serversFromPairings creates the servers for each pairing of addresses
// to server blocks. Each pairing is essentially a server definition.
-func (st *ServerType) serversFromPairings(pairings []sbAddrAssociation, warnings *[]caddyconfig.Warning) (map[string]*caddyhttp.Server, error) {
+func (st *ServerType) serversFromPairings(
+ pairings []sbAddrAssociation,
+ options map[string]interface{},
+ warnings *[]caddyconfig.Warning,
+) (map[string]*caddyhttp.Server, error) {
servers := make(map[string]*caddyhttp.Server)
for i, p := range pairings {
@@ -289,16 +295,19 @@ func (st *ServerType) serversFromPairings(pairings []sbAddrAssociation, warnings
siteVarSubroute.Routes = append(siteVarSubroute.Routes, cfgVal.Value.(caddyhttp.Route))
}
- // set up each handler directive
+ // set up each handler directive - the order of the handlers
+ // as they are added to the routes depends on user preference
dirRoutes := sblock.pile["route"]
- // TODO: The ordering here depends on... if there is a list of
- // directives to use, then sort by that, otherwise just use in
- // the order they appear in the slice (which is the order they
- // appeared in the Caddyfile)
- sortByList := true
- if sortByList {
+ handlerOrder, ok := options["handler_order"].([]string)
+ if !ok {
+ handlerOrder = defaultDirectiveOrder
+ }
+ if len(handlerOrder) == 1 && handlerOrder[0] == "appearance" {
+ handlerOrder = nil
+ }
+ if handlerOrder != nil {
dirPositions := make(map[string]int)
- for i, dir := range defaultDirectiveOrder {
+ for i, dir := range handlerOrder {
dirPositions[dir] = i
}
sort.SliceStable(dirRoutes, func(i, j int) bool {
diff --git a/caddyconfig/httpcaddyfile/global.go b/caddyconfig/httpcaddyfile/options.go
index 067d06e..f99ce51 100644
--- a/caddyconfig/httpcaddyfile/global.go
+++ b/caddyconfig/httpcaddyfile/options.go
@@ -51,3 +51,26 @@ func parseHTTPSPort(d *caddyfile.Dispenser) (int, error) {
}
return httpsPort, nil
}
+
+func parseHandlerOrder(d *caddyfile.Dispenser) ([]string, error) {
+ if !d.Next() {
+ return nil, d.ArgErr()
+ }
+ order := d.RemainingArgs()
+ if len(order) == 1 && order[0] == "appearance" {
+ return []string{"appearance"}, nil
+ }
+ if len(order) > 0 && d.NextBlock() {
+ return nil, d.Err("cannot open block if there are arguments")
+ }
+ for d.NextBlock() {
+ order = append(order, d.Val())
+ if d.NextArg() {
+ return nil, d.ArgErr()
+ }
+ }
+ if len(order) == 0 {
+ return nil, d.ArgErr()
+ }
+ return order, nil
+}