Build Caddy with plugins
Go to file
Mohammed Al Sahaf e7b19b13a2
Run go mod tidy in the build environment (#47)
* Run `go mod tidy` in the build environment

As of go 1.16, the `go {build,test}` commands don't modify the go.mod and go.sum files (see: https://golang.org/issue/40728). We, therefore, have to run `go mod tidy` to ensure the go.mod and go.sum files are consistent with the module requirements.

Closes #46

* check the error of `go mod tidy`
2021-02-05 10:49:17 -07:00
.github ci: Fix echo message on Cloudsmith push (#43) 2020-12-30 11:56:40 -07:00
cmd/xcaddy Support setcap in dev mode (XCADDY_SETCAP) 2021-01-20 12:17:46 -07:00
.gitignore Update .gitignore (#31) 2020-08-19 07:09:48 -06:00
.golangci.yml chore: add CI/CD with GitHub Actions (#19) 2020-05-22 14:06:12 -06:00
.goreleaser.yml ci: Pull updates from Caddy, build .deb as well (#42) 2020-12-30 11:06:56 -07:00
builder.go Run go mod tidy in the build environment (#47) 2021-02-05 10:49:17 -07:00
builder_test.go Support go.mod replace version pinning (#27) 2020-08-10 12:27:38 -06:00
environment.go Support go.mod replace version pinning (#27) 2020-08-10 12:27:38 -06:00
go.mod Housekeeping 2020-04-23 14:36:17 -06:00
go.sum Housekeeping 2020-04-23 14:36:17 -06:00
LICENSE Initial commit 2020-03-21 14:31:29 -06:00
platforms.go Honor CGO_ENABLED environment variable (fix #17) 2020-05-22 10:31:34 -06:00
README.md Support setcap in dev mode (XCADDY_SETCAP) 2021-01-20 12:17:46 -07:00

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).

⚠️ Still in development. Supports Caddy 2 only.

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

Requirements

Install

You can download binaries that are already compiled for your platform, or build xcaddy from source:

$ go get -u github.com/caddyserver/xcaddy/cmd/xcaddy

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.

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.
  • --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/ntlm-transport@v0.1.1

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

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

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

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

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.

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_SKIP_CLEANUP=1 causes xcaddy to leave build artifacts on disk after exiting.
  • XCADDY_SETCAP=1 will run sudo setcap cap_net_bind_service=+ep on the temporary binary before running it when in dev mode.

© 2020 Matthew Holt