core: Major refactor of admin endpoint and config handling

Fixed several bugs and made other improvements. All config changes are
now mediated by the global config state manager. It used to be that
initial configs given at startup weren't tracked, so you could start
caddy with --config caddy.json and then do a GET /config/ and it would
return null. That is fixed, along with several other general flow/API
enhancements, with more to come.
This commit is contained in:
Matthew Holt 2019-11-04 12:05:20 -07:00
parent fb06c041c4
commit 35f70c98fa
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5
8 changed files with 962 additions and 683 deletions

View file

@ -160,23 +160,12 @@ func cmdRun(fl Flags) (int, error) {
cleanModVersion := strings.TrimPrefix(goModule.Version, "v")
certmagic.UserAgent = "Caddy/" + cleanModVersion
// start the admin endpoint along with any initial config
// a configuration without admin config is considered fine
// but does not enable the admin endpoint at all
err = caddy.StartAdmin(config)
if err == nil {
defer caddy.StopAdmin()
} else if err != caddy.ErrAdminInterfaceNotConfigured {
return caddy.ExitCodeFailedStartup,
fmt.Errorf("starting caddy administration endpoint: %v", err)
// run the initial config
err = caddy.Load(config, true)
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("loading initial config: %v", err)
}
// if a config has been supplied, load it as initial config
if len(config) > 0 {
err := caddy.Load(bytes.NewReader(config))
if err != nil {
return caddy.ExitCodeFailedStartup, fmt.Errorf("loading initial config: %v", err)
}
caddy.Log().Named("admin").Info("Caddy 2 serving initial configuration")
}