Caddy.ConfigManager (Caddy v2.3.1)

View Source

Unified configuration manager coordinating in-memory and runtime Caddy config.

Provides a single interface for:

  • Reading config from both sources (in-memory ConfigProvider and runtime Admin.Api)
  • Syncing between sources (manual by default)
  • Detecting drift between sources
  • Atomic updates with rollback on failure

Sources

  • :memory - The in-memory Caddyfile stored in Caddy.ConfigProvider
  • :runtime - The JSON config in the running Caddy process via Admin API
  • :both - Returns both sources for comparison

Sync Strategies

By default, sync is manual. Users explicitly call sync_to_caddy/0 or sync_from_caddy/0.

Examples

# Get config from preferred source
{:ok, config} = Caddy.ConfigManager.get_config(:memory)
{:ok, config} = Caddy.ConfigManager.get_config(:runtime)

# Update and sync
:ok = Caddy.ConfigManager.set_caddyfile(new_caddyfile, sync: true)

# Manual sync
:ok = Caddy.ConfigManager.sync_to_caddy()
:ok = Caddy.ConfigManager.sync_from_caddy()

# Check for drift
{:ok, :in_sync} = Caddy.ConfigManager.check_sync_status()
{:ok, {:drift_detected, diff}} = Caddy.ConfigManager.check_sync_status()

# Rollback to last known good config
:ok = Caddy.ConfigManager.rollback()

Summary

Functions

Apply JSON config directly to running Caddy (bypasses in-memory).

Check if in-memory and runtime configs are in sync.

Returns a specification to start this module under a supervisor.

Clear the current configuration, returning to unconfigured state.

Check if configuration is set (configured, synced, or degraded state).

Get configuration from specified source.

Get current internal state information (for debugging).

Get in-memory config in specified format.

Get JSON config from running Caddy.

Get JSON config from running Caddy at specific path.

Get the current application state.

Check if the system is ready to serve (synced state).

Report a health check result to update state.

Rollback to last known good config.

Set Caddyfile in memory with optional sync to Caddy.

Start the ConfigManager GenServer

sync_from_caddy() deprecated

Pull running Caddy config to memory.

Push in-memory config to running Caddy.

Validate config without applying.

Types

application_state()

@type application_state() :: Caddy.State.state()

source()

@type source() :: :memory | :runtime | :both

sync_status()

@type sync_status() :: :in_sync | {:drift_detected, map()}

Functions

apply_runtime_config(config)

@spec apply_runtime_config(map()) :: :ok | {:error, term()}

Apply JSON config directly to running Caddy (bypasses in-memory).

Use this for runtime-only changes that don't need to persist.

check_sync_status()

@spec check_sync_status() :: {:ok, sync_status()}

Check if in-memory and runtime configs are in sync.

Returns :in_sync if configs match, or {:drift_detected, diff} with information about the differences.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear_config()

@spec clear_config() :: :ok | {:error, term()}

Clear the current configuration, returning to unconfigured state.

configured?()

@spec configured?() :: boolean()

Check if configuration is set (configured, synced, or degraded state).

get_config(source \\ :runtime)

@spec get_config(source()) :: {:ok, map()} | {:error, term()}

Get configuration from specified source.

Sources

  • :memory - Returns the in-memory config as JSON (adapted from Caddyfile)
  • :runtime - Returns the running Caddy's JSON config
  • :both - Returns both configs in a map

Examples

{:ok, config} = Caddy.ConfigManager.get_config(:runtime)
{:ok, %{memory: mem_config, runtime: rt_config}} = Caddy.ConfigManager.get_config(:both)

get_internal_state()

@spec get_internal_state() :: map()

Get current internal state information (for debugging).

get_memory_config(atom)

@spec get_memory_config(:caddyfile | :json) ::
  {:ok, binary() | map()} | {:error, term()}

Get in-memory config in specified format.

Formats

  • :caddyfile - Returns raw Caddyfile text
  • :json - Returns JSON map (adapted from Caddyfile)

get_runtime_config()

@spec get_runtime_config() :: {:ok, map()} | {:error, term()}

Get JSON config from running Caddy.

get_runtime_config(path)

@spec get_runtime_config(String.t()) :: {:ok, map()} | {:error, term()}

Get JSON config from running Caddy at specific path.

get_state()

@spec get_state() :: application_state()

Get the current application state.

Returns one of: :initializing, :unconfigured, :configured, :synced, :degraded

ready?()

@spec ready?() :: boolean()

Check if the system is ready to serve (synced state).

report_health_status(status)

@spec report_health_status(:ok | :error) :: :ok

Report a health check result to update state.

Called by Server.External when health checks complete.

rollback()

@spec rollback() :: :ok | {:error, term()}

Rollback to last known good config.

Restores the last successfully synced configuration.

set_caddyfile(caddyfile, opts \\ [])

@spec set_caddyfile(
  binary(),
  keyword()
) :: :ok | {:error, term()}

Set Caddyfile in memory with optional sync to Caddy.

Options

  • :sync - If true, immediately sync to running Caddy (default: false)
  • :validate - If true, validate before setting (default: true)

start_link(args \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start the ConfigManager GenServer

sync_from_caddy()

This function is deprecated. Use get_runtime_config/0 instead. Will be removed in v3.0.0.
@spec sync_from_caddy() :: :ok | {:error, term()}

Pull running Caddy config to memory.

DEPRECATED: This function stores JSON in the Caddyfile field, which breaks the text-first design principle. It will be removed in v3.0.0.

The Caddy Admin API returns JSON configuration, but there is no reverse conversion from JSON back to Caddyfile format. Use get_runtime_config/0 to inspect the running configuration instead.

sync_to_caddy()

@spec sync_to_caddy() :: :ok | {:error, term()}

Push in-memory config to running Caddy.

Adapts the Caddyfile to JSON and loads it into the running Caddy instance.

Options

  • :backup - If true, backup current runtime config before sync (default: true)
  • :force - If true, skip validation (default: false)

validate_config(caddyfile)

@spec validate_config(binary()) :: :ok | {:error, term()}

Validate config without applying.