Caddy (Caddy v2.3.1)

View Source

Caddy

Start Caddy HTTP Server in supervisor tree.

If caddy bin is set, caddy server will automatically start when application starts.

Starting Caddy

Add to your supervision tree:

def application do
  [
    extra_applications: [Caddy.Application]
  ]
end

Configuration

Configuration is stored as raw Caddyfile text. Write native Caddyfile syntax directly:

Caddy.set_caddyfile("""
{
  debug
  admin unix//tmp/caddy.sock
}

example.com {
  reverse_proxy localhost:3000
}
""")

If caddy_bin is not specified, Caddy.Server will not start. Set caddy_bin to the path of Caddy binary file and start Caddy.Server:

Caddy.set_bin("/usr/bin/caddy")
Caddy.restart_server()

This will restart server automatically:

Caddy.set_bin!("/usr/bin/caddy")

Application Config

import Config

# dump caddy server log to stdout
config :caddy, dump_log: false

# caddy server will not start, this is useful for testing
config :caddy, start: false

Summary

Functions

Adapt Caddyfile text to JSON (validates syntax)

Add an additional (snippet, matcher, etc.)

Add a site configuration

Append content to the Caddyfile

Apply JSON config directly to running Caddy.

See Caddy.ConfigManager.apply_runtime_config/2.

Backup current configuration to file

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

Clear the current configuration, returning to :unconfigured state.

Check if a Caddyfile configuration is set.

Get an additional by name

Get all additionals

Get the assembled Caddyfile configuration

Get current configuration struct

Get global options content

Get JSON config from running Caddy.

Get JSON config from running Caddy at specific path.

Get a site by address

Get all sites

Get the current application state.

Check if the system is ready to serve requests.

Remove an additional by name

Remove a site by address

Restart Caddy Server

Restore configuration from backup

Rollback to last known good config.

Save current configuration

Set Caddy binary path

Set Caddy binary path and restart server

Set the Caddyfile configuration (parses into 3 parts)

Set configuration struct

Set global options content (without braces)

Manually Start Caddy Server.

Start the Caddy supervisor as part of a supervision tree.

Stop Caddy Server

sync_from_caddy() deprecated

Pull runtime config from Caddy to memory.

Sync in-memory config to running Caddy.

See Caddy.ConfigManager.sync_to_caddy/1.

Update an existing additional by name, or add if not found

Update an existing site by address, or add if not found

Validate Caddyfile without applying.

Functions

adapt(caddyfile)

Adapt Caddyfile text to JSON (validates syntax)

add_additional(name, content)

Add an additional (snippet, matcher, etc.)

add_site(address, config)

Add a site configuration

append_caddyfile(content)

Append content to the Caddyfile

apply_runtime_config(config)

Apply JSON config directly to running Caddy.

Bypasses in-memory config - use for runtime-only changes.

apply_runtime_config(path, config)

See Caddy.ConfigManager.apply_runtime_config/2.

backup_config()

Backup current configuration to file

check_sync_status()

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

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

clear_config()

Clear the current configuration, returning to :unconfigured state.

Can only be called from :configured state. Returns an error if called from other states.

Examples

iex> Caddy.clear_config()
:ok
iex> Caddy.get_state()
:unconfigured

configured?()

Check if a Caddyfile configuration is set.

Returns true when in :configured, :synced, or :degraded state.

Examples

iex> Caddy.configured?()
false

iex> Caddy.set_caddyfile("localhost { respond 200 }")
:ok
iex> Caddy.configured?()
true

get_additional(name)

Get an additional by name

get_additionals()

Get all additionals

get_caddyfile()

Get the assembled Caddyfile configuration

get_config()

Get current configuration struct

get_global()

Get global options content

get_runtime_config()

Get JSON config from running Caddy.

Returns the current configuration from the running Caddy process via Admin API.

get_runtime_config(path)

Get JSON config from running Caddy at specific path.

Examples

{:ok, servers} = Caddy.get_runtime_config("apps/http/servers")

get_site(address)

Get a site by address

get_sites()

Get all sites

get_state()

Get the current application state.

Returns one of:

  • :initializing - Library starting up
  • :unconfigured - No Caddyfile set, waiting for configuration
  • :configured - Caddyfile set, pending sync to Caddy
  • :synced - Configuration synced to Caddy, operational
  • :degraded - Configuration synced but Caddy not responding

Examples

iex> Caddy.get_state()
:unconfigured

iex> Caddy.set_caddyfile("localhost { respond 200 }")
:ok
iex> Caddy.get_state()
:configured

ready?()

Check if the system is ready to serve requests.

Returns true only when in :synced state (configuration has been successfully pushed to Caddy).

Examples

iex> Caddy.ready?()
false

iex> Caddy.sync_to_caddy()
:ok
iex> Caddy.ready?()
true

remove_additional(name)

Remove an additional by name

remove_site(address)

Remove a site by address

restart_server()

@spec restart_server() :: {:ok, pid()} | {:ok, :undefined} | {:error, term()}

Restart Caddy Server

restore_config()

Restore configuration from backup

rollback()

Rollback to last known good config.

save_config()

Save current configuration

set_bin(bin_path)

Set Caddy binary path

set_bin!(bin_path)

Set Caddy binary path and restart server

set_caddyfile(caddyfile)

Set the Caddyfile configuration (parses into 3 parts)

set_config(config)

Set configuration struct

set_global(content)

Set global options content (without braces)

start()

@spec start() :: :ignore | {:error, any()} | {:ok, pid()}

start(caddy_bin)

@spec start(binary()) :: :ignore | {:error, any()} | {:ok, pid()}

Manually Start Caddy Server.

This is useful when you want to start Caddy Server in iex console.

start_link(args)

@spec start_link(Keyword.t()) :: :ignore | {:error, any()} | {:ok, pid()}

Start the Caddy supervisor as part of a supervision tree.

stop(reason \\ :normal)

@spec stop(term()) :: :ok

Stop Caddy Server

sync_from_caddy()

This function is deprecated. Use get_runtime_config/0 instead. Will be removed in v3.0.0.

Pull runtime config from Caddy 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()

Sync in-memory config to running Caddy.

Adapts the Caddyfile 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)

Examples

:ok = Caddy.sync_to_caddy()
:ok = Caddy.sync_to_caddy(backup: false)

sync_to_caddy(opts)

See Caddy.ConfigManager.sync_to_caddy/1.

update_additional(name, content)

Update an existing additional by name, or add if not found

update_site(address, config)

Update an existing site by address, or add if not found

validate_caddyfile(caddyfile)

Validate Caddyfile without applying.