mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 06:03:48 +03:00
caddyfile: Allow overriding server names (#5323)
This commit is contained in:
parent
e9d95ab29f
commit
7f2a93e6c3
3 changed files with 114 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
77
caddytest/integration/caddyfile_adapt/server_names.txt
Normal file
77
caddytest/integration/caddyfile_adapt/server_names.txt
Normal file
|
@ -0,0 +1,77 @@
|
|||
{
|
||||
servers :443 {
|
||||
name https
|
||||
}
|
||||
|
||||
servers :8000 {
|
||||
name app1
|
||||
}
|
||||
|
||||
servers :8001 {
|
||||
name app2
|
||||
}
|
||||
|
||||
servers 123.123.123.123:8002 {
|
||||
name bind-server
|
||||
}
|
||||
}
|
||||
|
||||
example.com {
|
||||
}
|
||||
|
||||
:8000 {
|
||||
}
|
||||
|
||||
:8001, :8002 {
|
||||
}
|
||||
|
||||
:8002 {
|
||||
bind 123.123.123.123 222.222.222.222
|
||||
}
|
||||
----------
|
||||
{
|
||||
"apps": {
|
||||
"http": {
|
||||
"servers": {
|
||||
"app1": {
|
||||
"listen": [
|
||||
":8000"
|
||||
]
|
||||
},
|
||||
"app2": {
|
||||
"listen": [
|
||||
":8001"
|
||||
]
|
||||
},
|
||||
"bind-server": {
|
||||
"listen": [
|
||||
"123.123.123.123:8002",
|
||||
"222.222.222.222:8002"
|
||||
]
|
||||
},
|
||||
"https": {
|
||||
"listen": [
|
||||
":443"
|
||||
],
|
||||
"routes": [
|
||||
{
|
||||
"match": [
|
||||
{
|
||||
"host": [
|
||||
"example.com"
|
||||
]
|
||||
}
|
||||
],
|
||||
"terminal": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"srv4": {
|
||||
"listen": [
|
||||
":8002"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue