MCPcopy
hub / github.com/caddyserver/caddy / unsyncedDecodeAndRun

Function unsyncedDecodeAndRun

caddy.go:337–403  ·  view source on GitHub ↗

unsyncedDecodeAndRun removes any meta fields (like @id tags) from cfgJSON, decodes the result into a *Config, and runs it as the new config, replacing any other current config. It does NOT update the raw config state, as this is a lower-level function; most callers will want to use Load instead. A w

(cfgJSON []byte, allowPersist bool)

Source from the content-addressed store, hash-verified

335// allowPersist is false, it will not be persisted to disk,
336// even if it is configured to.
337func unsyncedDecodeAndRun(cfgJSON []byte, allowPersist bool) error {
338 // remove any @id fields from the JSON, which would cause
339 // loading to break since the field wouldn't be recognized
340 strippedCfgJSON := RemoveMetaFields(cfgJSON)
341
342 var newCfg *Config
343 err := StrictUnmarshalJSON(strippedCfgJSON, &newCfg)
344 if err != nil {
345 return err
346 }
347
348 // prevent recursive config loads; that is a user error, and
349 // although frequent config loads should be safe, we cannot
350 // guarantee that in the presence of third party plugins, nor
351 // do we want this error to go unnoticed (we assume it was a
352 // pulled config if we're not allowed to persist it)
353 if !allowPersist &&
354 newCfg != nil &&
355 newCfg.Admin != nil &&
356 newCfg.Admin.Config != nil &&
357 newCfg.Admin.Config.LoadRaw != nil &&
358 newCfg.Admin.Config.LoadDelay <= 0 {
359 return fmt.Errorf("recursive config loading detected: pulled configs cannot pull other configs without positive load_delay")
360 }
361
362 // run the new config and start all its apps
363 ctx, err := run(newCfg, true)
364 if err != nil {
365 return err
366 }
367
368 // swap old context (including its config) with the new one
369 currentCtxMu.Lock()
370 oldCtx := currentCtx
371 currentCtx = ctx
372 currentCtxMu.Unlock()
373
374 // Stop, Cleanup each old app
375 unsyncedStop(oldCtx)
376
377 // autosave a non-nil config, if not disabled
378 if allowPersist &&
379 newCfg != nil &&
380 (newCfg.Admin == nil ||
381 newCfg.Admin.Config == nil ||
382 newCfg.Admin.Config.Persist == nil ||
383 *newCfg.Admin.Config.Persist) {
384 dir := filepath.Dir(ConfigAutosavePath)
385 err := os.MkdirAll(dir, 0o700)
386 if err != nil {
387 Log().Error("unable to create folder for config autosave",
388 zap.String("dir", dir),
389 zap.Error(err))
390 } else {
391 err := os.WriteFile(ConfigAutosavePath, cfgJSON, 0o600)
392 if err == nil {
393 Log().Info("autosaved config (load with --resume flag)", zap.String("file", ConfigAutosavePath))
394 } else {

Callers 1

changeConfigFunction · 0.85

Calls 7

RemoveMetaFieldsFunction · 0.85
StrictUnmarshalJSONFunction · 0.85
runFunction · 0.85
unsyncedStopFunction · 0.85
LogFunction · 0.85
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected