xcaddy/README.md

133 lines
3.7 KiB
Markdown
Raw Normal View History

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-05-23 23:32:48 +01:00
⚠️ Still in development. Supports Caddy 2 only.
2020-04-08 01:56:30 +01:00
Stay updated, be aware of changes, and please submit feedback! Thanks!
2020-03-21 20:31:29 +00:00
## Requirements
- [Go installed](https://golang.org/doc/install)
2020-03-21 20:31:29 +00:00
2020-05-03 01:22:59 +01:00
## Install
You can [download binaries](https://github.com/caddyserver/xcaddy/releases) that are already compiled for your platform, or build `xcaddy` from source:
```bash
$ go get -u github.com/caddyserver/xcaddy/cmd/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
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-08 01:56:30 +01:00
### Custom builds
2020-03-21 20:31:29 +00:00
Syntax:
```
$ xcaddy build [<caddy_version>]
2020-04-08 01:56:30 +01:00
[--output <file>]
[--with <module[@version][=replacement]>...]
2020-03-21 20:31:29 +00:00
```
- `<caddy_version>` is the core Caddy version to build; defaults to `CADDY_VERSION` env variable or latest.
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.
Examples:
```bash
$ 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`.
2020-04-08 01:56:30 +01:00
The binary will be built and run from the current directory, then cleaned up.
2020-04-08 01:56:30 +01:00
The current working directory must be inside an initialized Go module.
Syntax:
```
2020-04-08 01:56:30 +01:00
$ xcaddy <args...>
```
- `<args...>` are passed through to the `caddy` command.
2020-03-21 20:31:29 +00:00
For example:
```bash
$ xcaddy list-modules
2020-04-08 01:56:30 +01:00
$ xcaddy run
$ xcaddy run --config caddy.json
2020-03-21 20:31:29 +00:00
```
2020-05-23 23:32:48 +01:00
The race detector can be enabled by setting `XCADDY_RACE_DETECTOR=1`.
2020-04-08 01:56:30 +01:00
## Library usage
```go
builder := xcaddy.Builder{
CaddyVersion: "v2.0.0",
2020-04-08 01:56:30 +01:00
Plugins: []xcaddy.Dependency{
{
ModulePath: "github.com/caddyserver/ntlm-transport",
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.
- `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting.
2020-04-08 01:56:30 +01:00
2020-03-21 20:31:29 +00:00
---
&copy; 2020 Matthew Holt