summaryrefslogtreecommitdiff
path: root/caddyconfig/httpcaddyfile
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2023-01-27 14:56:39 -0500
committerGitHub <noreply@github.com>2023-01-27 14:56:39 -0500
commit7f2a93e6c36ceec9e007baeedeb594ab0dc43a71 (patch)
tree647a3d94cbd244d01de58d455d076309194fb6b2 /caddyconfig/httpcaddyfile
parente9d95ab29f6cbf24fd3f7f7fd439ab31e2797f93 (diff)
caddyfile: Allow overriding server names (#5323)
Diffstat (limited to 'caddyconfig/httpcaddyfile')
-rw-r--r--caddyconfig/httpcaddyfile/httptype.go2
-rw-r--r--caddyconfig/httpcaddyfile/serveroptions.go37
2 files changed, 37 insertions, 2 deletions
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index 00678ef..43d756f 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -706,7 +706,7 @@ func (st *ServerType) serversFromPairings(
err := applyServerOptions(servers, options, warnings)
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("applying global server options: %v", err)
}
return servers, nil
diff --git a/caddyconfig/httpcaddyfile/serveroptions.go b/caddyconfig/httpcaddyfile/serveroptions.go
index 7aa0a2a..dca4ede 100644
--- a/caddyconfig/httpcaddyfile/serveroptions.go
+++ b/caddyconfig/httpcaddyfile/serveroptions.go
@@ -33,6 +33,7 @@ type serverOptions struct {
ListenerAddress string
// These will all map 1:1 to the caddyhttp.Server struct
+ Name string
ListenerWrappersRaw []json.RawMessage
ReadTimeout caddy.Duration
ReadHeaderTimeout caddy.Duration
@@ -58,6 +59,15 @@ func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (any, error) {
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
+ case "name":
+ if serverOpts.ListenerAddress == "" {
+ return nil, d.Errf("cannot set a name for a server without a listener address")
+ }
+ if !d.NextArg() {
+ return nil, d.ArgErr()
+ }
+ serverOpts.Name = d.Val()
+
case "listener_wrappers":
for nesting := d.Nesting(); d.NextBlock(nesting); {
modID := "caddy.listeners." + d.Val()
@@ -248,7 +258,22 @@ func applyServerOptions(
return nil
}
- for _, server := range servers {
+ // check for duplicate names, which would clobber the config
+ existingNames := map[string]bool{}
+ for _, opts := range serverOpts {
+ if opts.Name == "" {
+ continue
+ }
+ if existingNames[opts.Name] {
+ return fmt.Errorf("cannot use duplicate server name '%s'", opts.Name)
+ }
+ existingNames[opts.Name] = true
+ }
+
+ // collect the server name overrides
+ nameReplacements := map[string]string{}
+
+ for key, server := range servers {
// find the options that apply to this server
opts := func() *serverOptions {
for _, entry := range serverOpts {
@@ -287,6 +312,16 @@ func applyServerOptions(
}
server.Logs.ShouldLogCredentials = opts.ShouldLogCredentials
}
+
+ if opts.Name != "" {
+ nameReplacements[key] = opts.Name
+ }
+ }
+
+ // rename the servers if marked to do so
+ for old, new := range nameReplacements {
+ servers[new] = servers[old]
+ delete(servers, old)
}
return nil