Skip to main content

Caddy JSON configuration manipulation (Pydantic v2 + fluent builder)

Project description

caddyconfig

Caddy JSON configuration manipulation for Python, built on Pydantic v2.

Provides typed models, validation, a fluent builder API, and bidirectional serialization to/from Caddy's native JSON format.

Installation

pip install caddyconfig

Requires Python 3.10+.

Quick Start

Fluent Builder API

from caddyconfig.builder import CaddyConfigBuilder

config = (
    CaddyConfigBuilder()
    .admin(listen="localhost:2019")
    .logging(writer="stderr", level="INFO")
    .server("main", listen=[":443", ":80"])
        .route()
            .match(host=["acme.com"])
            .handle_static(200, body="Welcome")
        .done()
        .route()
            .match(path=["/api"])
            .handle_reverse_proxy("10.0.0.1:8080")
        .done()
    .done()
    .build()
)

print(config.to_json(indent=2))

Direct Model API

from caddyconfig import (
    CaddyConfig, Admin, Logging, LogSink, LogEntry,
    Apps, HttpApp, Server, Route, MatchCriteria,
    StaticResponseHandler, ReverseProxyHandler,
)

config = CaddyConfig(
    admin=Admin(listen="localhost:2019"),
    logging=Logging(
        sink=LogSink(writer="stderr"),
        logs={"default": LogEntry(name="default", level="INFO")},
    ),
    apps=Apps(
        http=HttpApp(
            servers={
                "main": Server(
                    name="main",
                    listen=[":443", ":80"],
                    routes=[
                        Route(
                            match=[MatchCriteria(host=["acme.com"])],
                            handle=[StaticResponseHandler(status_code=200, body="Welcome")],
                        ),
                        Route(
                            match=[MatchCriteria(path=["/api"])],
                            handle=[ReverseProxyHandler(upstreams=["10.0.0.1:8080"])],
                        ),
                    ],
                )
            }
        ),
    )
)

JSON Roundtrip

json_str = config.to_json()
config2 = CaddyConfig.from_json(json_str)
assert config2.to_dict() == config.to_dict()

Route Lookup and Mutation

server = config.apps.http.get_server("main")

# Find a route by matcher criteria (partial match supported)
route = server.find_route(MatchCriteria(path=["/api"]))

# Upsert: replace if exists, insert otherwise
server.upsert_route(MatchCriteria(path=["/api"]), new_route)

# Remove a route
server.remove_route(MatchCriteria(path=["/api"]))

# Search recursively through subroutes
route = server.find_route(MatchCriteria(path=["/api"]), recursive=True)

Supported Handlers

Handler Model Builder Method
static_response StaticResponseHandler .handle_static(status, body=...)
reverse_proxy ReverseProxyHandler .handle_reverse_proxy("host:port")
file_server FileServerHandler .handle_file_server(root=...)
authentication AuthenticationHandler .handle_auth_basic(("u", "pw"))
subroute SubrouteHandler .handle_subroute()
unknown types RawHandler .handle(raw_handler)

Development

# Install dependencies
uv sync

# Run tests
uv run pytest

# Run a single test file
uv run pytest tests/test_builder.py

# With coverage
uv run pytest --cov=caddyconfig

License

Apache License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

caddyconfig-0.1.5.tar.gz (36.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

caddyconfig-0.1.5-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file caddyconfig-0.1.5.tar.gz.

File metadata

  • Download URL: caddyconfig-0.1.5.tar.gz
  • Upload date:
  • Size: 36.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for caddyconfig-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f0eafe22083a2029fb29341f7d6b532881add0e0329916c2e2ccc45494a8a9c1
MD5 79a082d6f5b5e2bf45eafd5cd1ad71ac
BLAKE2b-256 30b4081685d5d9e852d4175284f771e082f2fdfeb10ead2f30b99be9d257b209

See more details on using hashes here.

File details

Details for the file caddyconfig-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: caddyconfig-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 29.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for caddyconfig-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 5b89af0be8e5f762af46238bc5293f76027faef2945ee5c7aabd9bf4e3a436b1
MD5 4d422144bfdc18a85d63cd8ac2d386e7
BLAKE2b-256 41c43005ac1ccc0675e8591375a16082fc1a8e0f7ea6378e5cabbdf6b2b6f36d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page