2020-04-08 01:56:30 +01:00
`xcaddy` - Custom Caddy Builder
===============================
2020-03-21 20:31:29 +00:00
2020-04-08 01:56:30 +01:00
This command line tool and associated Go package makes it easy to make custom builds of the [Caddy Web Server ](https://github.com/caddyserver/caddy ).
It is used heavily by Caddy plugin developers as well as anyone who wishes to make custom `caddy` binaries (with or without plugins).
2020-03-21 20:31:29 +00:00
2020-04-08 01:56:30 +01:00
Stay updated, be aware of changes, and please submit feedback! Thanks!
2020-03-25 06:01:44 +00:00
2020-03-21 20:31:29 +00:00
## Requirements
2020-04-22 23:29:57 +01:00
- [Go installed ](https://golang.org/doc/install )
2020-03-21 20:31:29 +00:00
2020-05-03 01:22:59 +01:00
## Install
2021-02-22 20:14:46 +00:00
You can [download binaries ](https://github.com/caddyserver/xcaddy/releases ) that are already compiled for your platform from the Release tab.
You may also build `xcaddy` from source:
2020-05-03 01:22:59 +01:00
```bash
2021-02-22 20:16:27 +00:00
$ go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
2020-05-03 01:22:59 +01:00
```
2021-02-22 20:16:27 +00:00
For Debian, Ubuntu, and Raspbian, an `xcaddy` package is available from our [Cloudsmith repo ](https://cloudsmith.io/~caddy/repos/xcaddy/packages/ ):
2021-02-22 20:14:46 +00:00
```bash
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
2022-05-17 16:07:12 +01:00
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/xcaddy/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-xcaddy-archive-keyring.gpg
2021-02-22 20:14:46 +00:00
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
```
2020-03-21 20:31:29 +00:00
2020-04-08 01:56:30 +01:00
## Command usage
2020-03-21 20:31:29 +00:00
2020-04-08 01:56:30 +01:00
The `xcaddy` command has two primary uses:
2020-03-21 20:31:29 +00:00
2020-05-23 23:32:48 +01:00
1. Compile custom `caddy` binaries
2. A replacement for `go run` while developing Caddy plugins
2020-03-25 06:01:44 +00:00
2020-04-22 23:29:57 +01:00
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.
2020-05-03 01:22:59 +01:00
As usual with `go` command, the `xcaddy` command will pass the `GOOS` , `GOARCH` , and `GOARM` environment variables through for cross-compilation.
2020-04-22 23:29:57 +01:00
2022-09-30 18:26:05 +01:00
Note that `xcaddy` will ignore the `vendor/` folder with `-mod=readonly` .
2020-04-08 01:56:30 +01:00
### Custom builds
2020-03-25 06:01:44 +00:00
2020-03-21 20:31:29 +00:00
Syntax:
```
2020-04-22 23:29:57 +01:00
$ xcaddy build [< caddy_version > ]
2020-04-08 01:56:30 +01:00
[--output < file > ]
2020-04-22 23:29:57 +01:00
[--with < module [ @ version ] [ = replacement ] > ...]
2020-03-21 20:31:29 +00:00
```
2022-03-25 18:24:30 +00:00
- `<caddy_version>` is the core Caddy version to build; defaults to `CADDY_VERSION` env variable or latest.< br >
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`
2020-03-21 20:31:29 +00:00
- `--output` changes the output file.
2020-04-22 23:40:25 +01:00
- `--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.
2020-03-25 06:01:44 +00:00
2020-04-22 23:29:57 +01:00
Examples:
2020-03-25 06:01:44 +00:00
```bash
2020-04-22 23:29:57 +01:00
$ xcaddy build \
--with github.com/caddyserver/ntlm-transport
$ xcaddy build v2.0.1 \
2020-05-10 01:59:18 +01:00
--with github.com/caddyserver/ntlm-transport@v0.1.1
2020-04-22 23:29:57 +01:00
2022-03-25 18:24:30 +00:00
$ xcaddy build master \
--with github.com/caddyserver/ntlm-transport
$ xcaddy build a58f240d3ecbb59285303746406cab50217f8d24 \
--with github.com/caddyserver/ntlm-transport
2020-04-22 23:29:57 +01:00
$ xcaddy build \
--with github.com/caddyserver/ntlm-transport=../../my-fork
$ xcaddy build \
2020-05-10 01:59:18 +01:00
--with github.com/caddyserver/ntlm-transport@v0.1.1=../../my-fork
2020-03-25 06:01:44 +00:00
```
2020-12-28 15:49:32 +00:00
You can even replace Caddy core using the `--with` flag:
```
$ xcaddy build \
--with github.com/caddyserver/caddy/v2=../../my-caddy-fork
2022-09-02 00:43:36 +01:00
$ xcaddy build \
--with github.com/caddyserver/caddy/v2=github.com/my-user/caddy/v2@some-branch
2020-12-28 15:49:32 +00:00
```
This allows you to hack on Caddy core (and optionally plug in extra modules at the same time!) with relative ease.
2021-09-04 02:36:13 +01:00
2020-03-25 06:01:44 +00:00
### For plugin development
2020-04-22 23:29:57 +01:00
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` .
2020-04-08 01:56:30 +01:00
The binary will be built and run from the current directory, then cleaned up.
2020-03-25 06:01:44 +00:00
2020-04-08 01:56:30 +01:00
The current working directory must be inside an initialized Go module.
2020-03-25 06:01:44 +00:00
Syntax:
```
2020-04-08 01:56:30 +01:00
$ xcaddy < args... >
2020-03-25 06:01:44 +00:00
```
- `<args...>` are passed through to the `caddy` command.
2020-03-21 20:31:29 +00:00
For example:
```bash
2020-03-25 06:01:44 +00:00
$ xcaddy list-modules
2020-04-08 01:56:30 +01:00
$ xcaddy run
2020-03-25 06:01:44 +00:00
$ xcaddy run --config caddy.json
2020-03-21 20:31:29 +00:00
```
2021-07-04 00:57:17 +01:00
The race detector can be enabled by setting `XCADDY_RACE_DETECTOR=1` . The DWARF debug info can be enabled by setting `XCADDY_DEBUG=1` .
2020-04-08 01:56:30 +01:00
2021-09-04 02:36:13 +01:00
### Getting `xcaddy`'s version
```
$ xcaddy version
```
2020-04-08 01:56:30 +01:00
## Library usage
```go
builder := xcaddy.Builder{
2020-05-10 01:59:18 +01:00
CaddyVersion: "v2.0.0",
2020-04-08 01:56:30 +01:00
Plugins: []xcaddy.Dependency{
{
ModulePath: "github.com/caddyserver/ntlm-transport",
2020-05-10 01:59:18 +01:00
Version: "v0.1.1",
2020-04-08 01:56:30 +01:00
},
},
}
2020-04-28 18:07:59 +01:00
err := builder.Build(context.Background(), "./caddy")
2020-04-08 01:56:30 +01:00
```
Versions can be anything compatible with `go get` .
2020-07-16 23:07:50 +01:00
## 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.
2021-07-04 00:57:17 +01:00
- `XCADDY_DEBUG=1` enables the DWARF debug information in the build.
2021-01-20 19:17:46 +00:00
- `XCADDY_SETCAP=1` will run `sudo setcap cap_net_bind_service=+ep` on the temporary binary before running it when in dev mode.
2021-02-05 23:34:51 +00:00
- `XCADDY_SKIP_BUILD=1` causes xcaddy to not compile the program, it is used in conjunction with build tools such as [GoReleaser ](https://goreleaser.com ). Implies `XCADDY_SKIP_CLEANUP=1` .
- `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting.
2022-04-25 17:13:12 +01:00
- `XCADDY_WHICH_GO` sets the go command to use when for example more then 1 version of go is installed.
2022-08-16 22:27:48 +01:00
- `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-03-21 20:31:29 +00:00
---
© 2020 Matthew Holt