• Stars
    star
    890
  • Rank 51,276 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Build Caddy with plugins

xcaddy - Custom Caddy Builder

This command line tool and associated Go package makes it easy to make custom builds of the Caddy Web Server.

It is used heavily by Caddy plugin developers as well as anyone who wishes to make custom caddy binaries (with or without plugins).

Stay updated, be aware of changes, and please submit feedback! Thanks!

Requirements

Install

You can download binaries that are already compiled for your platform from the Release tab.

You may also build xcaddy from source:

$ go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest

For Debian, Ubuntu, and Raspbian, an xcaddy package is available from our Cloudsmith repo:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/xcaddy/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-xcaddy-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/xcaddy/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-xcaddy.list
sudo apt update
sudo apt install xcaddy

โš ๏ธ Pro tip

If you find yourself fighting xcaddy in relation to your custom or proprietary build or development process, it might be easier to just build Caddy manually!

Caddy's main.go file, the main entry point to the application, has instructions in the comments explaining how to build Caddy essentially the same way xcaddy does it. But when you use the go command directly, you have more control over the whole thing and it may save you a lot of trouble.

The manual build procedure is very easy: just copy the main.go into a new folder, initialize a Go module, plug in your plugins (add an import for each one) and then run go build. Of course, you may wish to customize the go.mod file to your liking (specific dependency versions, replacements, etc).

Command usage

The xcaddy command has two primary uses:

  1. Compile custom caddy binaries
  2. A replacement for go run while developing Caddy plugins

The xcaddy command will use the latest version of Caddy by default. You can customize this for all invocations by setting the CADDY_VERSION environment variable.

As usual with go command, the xcaddy command will pass the GOOS, GOARCH, and GOARM environment variables through for cross-compilation.

Note that xcaddy will ignore the vendor/ folder with -mod=readonly.

Custom builds

Syntax:

$ xcaddy build [<caddy_version>]
    [--output <file>]
    [--with <module[@version][=replacement]>...]
  • <caddy_version> is the core Caddy version to build; defaults to CADDY_VERSION env variable or latest.
    This can be the keyword latest, which will use the latest stable tag, or any git ref such as:

    • A tag like v2.0.1
    • A branch like master
    • A commit like a58f240d3ecbb59285303746406cab50217f8d24
  • --output changes the output file.

  • --with can be used multiple times to add plugins by specifying the Go module name and optionally its version, similar to go get. Module name is required, but specific version and/or local replacement are optional.

Examples:

$ xcaddy build \
    --with github.com/caddyserver/ntlm-transport

$ xcaddy build v2.0.1 \
    --with github.com/caddyserver/[email protected]

$ xcaddy build master \
    --with github.com/caddyserver/ntlm-transport

$ xcaddy build a58f240d3ecbb59285303746406cab50217f8d24 \
    --with github.com/caddyserver/ntlm-transport

$ xcaddy build \
    --with github.com/caddyserver/ntlm-transport=../../my-fork

$ xcaddy build \
    --with github.com/caddyserver/[email protected]=../../my-fork

You can even replace Caddy core using the --with flag:

$ xcaddy build \
    --with github.com/caddyserver/caddy/v2=../../my-caddy-fork
    
$ xcaddy build \
    --with github.com/caddyserver/caddy/v2=github.com/my-user/caddy/v2@some-branch

This allows you to hack on Caddy core (and optionally plug in extra modules at the same time!) with relative ease.

For plugin development

If you run xcaddy from within the folder of the Caddy plugin you're working on without the build subcommand, it will build Caddy with your current module and run it, as if you manually plugged it in and invoked go run.

The binary will be built and run from the current directory, then cleaned up.

The current working directory must be inside an initialized Go module.

Syntax:

$ xcaddy <args...>
  • <args...> are passed through to the caddy command.

For example:

$ xcaddy list-modules
$ xcaddy run
$ xcaddy run --config caddy.json

The race detector can be enabled by setting XCADDY_RACE_DETECTOR=1. The DWARF debug info can be enabled by setting XCADDY_DEBUG=1.

Getting xcaddy's version

$ xcaddy version

Library usage

builder := xcaddy.Builder{
	CaddyVersion: "v2.0.0",
	Plugins: []xcaddy.Dependency{
		{
			ModulePath: "github.com/caddyserver/ntlm-transport",
			Version:    "v0.1.1",
		},
	},
}
err := builder.Build(context.Background(), "./caddy")

Versions can be anything compatible with go get.

Environment variables

Because the subcommands and flags are constrained to benefit rapid plugin prototyping, xcaddy does read some environment variables to take cues for its behavior and/or configuration when there is no room for flags.

  • CADDY_VERSION sets the version of Caddy to build.
  • XCADDY_RACE_DETECTOR=1 enables the Go race detector in the build.
  • XCADDY_DEBUG=1 enables the DWARF debug information in the build.
  • XCADDY_SETCAP=1 will run sudo setcap cap_net_bind_service=+ep on the resulting binary. By default, the sudo command will be used if it is found; set XCADDY_SUDO=0 to avoid using sudo if necessary.
  • XCADDY_SKIP_BUILD=1 causes xcaddy to not compile the program, it is used in conjunction with build tools such as GoReleaser. Implies XCADDY_SKIP_CLEANUP=1.
  • XCADDY_SKIP_CLEANUP=1 causes xcaddy to leave build artifacts on disk after exiting.
  • XCADDY_WHICH_GO sets the go command to use when for example more then 1 version of go is installed.
  • XCADDY_GO_BUILD_FLAGS overrides default build arguments. Supports Unix-style shell quoting, for example: XCADDY_GO_BUILD_FLAGS="-ldflags '-w s'". The provided flags are applied to go commands: build, clean, get, install, list, run, and test
  • XCADDY_GO_MOD_FLAGS overrides default go mod arguments. Supports Unix-style shell quoting.

ยฉ 2020 Matthew Holt

More Repositories

1

caddy

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Go
58,009
star
2

certmagic

Automatic HTTPS for any Go program: fully-managed TLS certificate issuance and renewal
Go
4,967
star
3

examples

OBSOLETE. This repo was for Caddy v1. For v2 and newer, see our forum's wiki category.
CSS
971
star
4

ingress

WIP Caddy 2 ingress controller for Kubernetes
Go
636
star
5

forwardproxy

Forward proxy plugin for the Caddy web server
Go
603
star
6

caddy-docker

Source for the official Caddy v2 Docker Image
Dockerfile
408
star
7

nginx-adapter

Run Caddy with your NGINX config
Go
315
star
8

cache-handler

Distributed HTTP caching module for Caddy
Go
260
star
9

website

The Caddy website
HTML
151
star
10

dist

Resources for packaging and distributing Caddy
Shell
115
star
11

dnsproviders

OBSOLETE: DNS providers adapted for use in Caddy to solve the ACME DNS challenge - for Caddy v1 only. See caddy-dns for v2.
Go
98
star
12

replace-response

Caddy module that performs replacements in response bodies
Go
95
star
13

vscode-caddyfile

Rich Caddyfile support for Visual Studio Code
TypeScript
78
star
14

transform-encoder

Log encoder module for custom log formats
Go
73
star
15

gateway

Kubernetes Gateway API implementation powered by Caddy
Go
60
star
16

caddy-lua

An experiment in executing Lua from Caddy. Ignore this for now. ;)
Go
33
star
17

sublimetext

Caddyfile syntax highlighting for Sublime Text 3
24
star
18

getcaddy.com

Automated installer script for Caddy with plugins. DECOMISSIONED in 2020.
Shell
22
star
19

ntlm-transport

NTLM reverse proxy transport module for Caddy
Go
14
star
20

cue-adapter

CUE config adapter for Caddy
Go
12
star
21

pkl-adapter

Configure Caddy with Pkl format
Go
10
star
22

zerossl

ZeroSSL REST API client implementation for Go
Go
8
star
23

moduledoc

Automated JSON docs generation for Caddy modules
Go
7
star
24

ingress-docker

4
star
25

circuitbreaker

A circuit-breaker module for the reverse proxy
Go
4
star
26

json5-adapter

JSON5 config adapter
Go
3
star
27

jsonc-adapter

JSONC config adapter
Go
2
star