Caddy.Server (Caddy v2.3.1)

View Source

Caddy Server - Mode-based server selection.

This module delegates to the appropriate server implementation based on the configured mode:

Embedded Mode

In embedded mode, the Caddy binary is spawned and managed directly by this application. The server handles process lifecycle, output logging, and cleanup.

External Mode

In external mode, Caddy is managed by an external system (e.g., systemd). This server communicates via the Admin API and can execute system commands for lifecycle operations.

Configuration

# External mode (default)
config :caddy, mode: :external
config :caddy, admin_url: "http://localhost:2019"
config :caddy, commands: [
  start: "systemctl start caddy",
  stop: "systemctl stop caddy",
  restart: "systemctl restart caddy",
  status: "systemctl is-active caddy"
]

# Embedded mode
config :caddy, mode: :embedded
config :caddy, caddy_bin: "/usr/bin/caddy"

Summary

Functions

Check the status of the Caddy server.

Returns a child specification for the supervisor.

Execute a lifecycle command (external mode only).

Get Caddyfile content of the current running server.

Returns the current server implementation module.

Start the appropriate server based on the configured mode.

Functions

check_status()

@spec check_status() :: :running | :stopped | :unknown

Check the status of the Caddy server.

Returns:

  • :running - Caddy is running and responding
  • :stopped - Caddy is not running
  • :unknown - Status cannot be determined

child_spec(opts)

Returns a child specification for the supervisor.

This delegates to the appropriate implementation based on mode.

execute_command(command)

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

Execute a lifecycle command (external mode only).

Available commands: :start, :stop, :restart, :status

Returns {:error, :embedded_mode} if called in embedded mode.

get_caddyfile()

@spec get_caddyfile() :: binary()

Get Caddyfile content of the current running server.

In embedded mode, reads from the local etc directory. In external mode, fetches via Admin API.

impl_module()

@spec impl_module() :: module()

Returns the current server implementation module.

start_link(opts \\ [])

Start the appropriate server based on the configured mode.