Skip to content

Commit 0109e61

Browse files
committed
feat: add xcaddy
1 parent 7020cf4 commit 0109e61

File tree

4 files changed

+347
-0
lines changed

4 files changed

+347
-0
lines changed

xcaddy/README.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
title: Custom Caddy Builder
3+
homepage: https://github.com/caddyserver/xcaddy
4+
tagline: |
5+
xcaddy makes it easy to make custom builds of the Caddy Web Server
6+
---
7+
8+
To update or switch versions, run `webi xcaddy@stable` (or `@v0.3`, `@beta`,
9+
etc).
10+
11+
### Files
12+
13+
These are the files / directories that are created and/or modified with this
14+
install:
15+
16+
```text
17+
~/.config/envman/PATH.env
18+
~/.local/bin/xcaddy
19+
~/.local/opt/go/
20+
~/go/bin/
21+
```
22+
23+
## Cheat Sheet
24+
25+
> `xcaddy` makes it easy to build caddy with DNS providers, and to test builds
26+
> of feature branches and PRs.
27+
28+
Build with `xcaddy`:
29+
30+
```sh
31+
CGO_ENABLED=0 xcaddy build 'v2.7.5' \
32+
--with github.com/caddy-dns/duckdns \
33+
--with github.com/caddy-dns/lego-deprecated \
34+
--output ./caddy-v2.7.5-extras
35+
```
36+
37+
See that it worked:
38+
39+
```sh
40+
./caddy-v2.7.5-extras list-modules
41+
# v2.7.5 h1:HoysvZkLcN2xJExEepaFHK92Qgs7xAiCFydN5x5Hs6Q=
42+
```
43+
44+
Other helpful tips:
45+
46+
- Caddy Versions
47+
- DNS Providers
48+
- Other Modules
49+
- Cross-Compiling
50+
- Build a Feature Branch
51+
- Build a Fork
52+
- List Built-In Modules
53+
- Common ENVs
54+
55+
### How to List Available Caddy Versions
56+
57+
<https://github.com/caddyserver/caddy/releases>
58+
59+
See also:
60+
61+
- <https://github.com/caddyserver/caddy/tags>
62+
- <https://webinstall.dev/api/releases/caddy.json?pretty=true&channel=stable&os=linux&arch=amd64&limit=10>
63+
- <https://webinstall.dev/api/releases/caddy.tab?channel=stable&os=linux&arch=amd64&limit=100>
64+
65+
### How to Find DNS Providers
66+
67+
There are two types of DNS Providers:
68+
69+
**1.`libdns`**
70+
71+
These providers each have their own module, most of which can be searched here:
72+
73+
- <https://github.com/caddy-dns>
74+
- <https://caddyserver.com/docs/modules/> (search `dns.providers`)
75+
76+
**2. `lego` (legacy)**
77+
78+
This module bundles _many_ providers together, _all_ of which are listed here:
79+
80+
- `github.com/caddy-dns/lego-deprecated`
81+
- <https://github.com/go-acme/lego#dns-providers>
82+
- <https://caddyserver.com/docs/modules/dns.providers.lego_deprecated>
83+
84+
### How to Find Other Modules
85+
86+
There are a variety of other modules, including things like `ngrok`, `s3`,
87+
`layer4`, and others.
88+
89+
Most module authors register them in the caddy modules docs:
90+
91+
- <https://caddyserver.com/docs/modules/>
92+
93+
However, a module can be loaded from any Git URL (not just GitHub).
94+
95+
Note: Modules are named internally by their namespace (e.g. `dns.providers.x`),
96+
but referenced externally by their Git URL .
97+
98+
### How to Cross-Compile
99+
100+
Use `CGO_ENABLED`, `GOOS`, `GOARCH`, and `GOARM` (for 32-bit ARM):
101+
102+
```sh
103+
GOOS=linux GOARCH=arm64 GOARM= \
104+
CGO_ENABLED=0 \
105+
xcaddy build 'v2.7.5' \
106+
--with github.com/caddy-dns/duckdns \
107+
--with github.com/caddy-dns/lego-deprecated \
108+
--output ./caddy-v2.7.5-extras
109+
```
110+
111+
You can _inline_ (as seen above) or `export` the ENVs:
112+
113+
```sh
114+
export GOOS=linux
115+
export GOARCH=arm64
116+
export GOARM=
117+
export CGO_ENABLED=0
118+
119+
xcaddy build 'v2.7.5' \
120+
--with github.com/caddy-dns/duckdns \
121+
--with github.com/caddy-dns/lego-deprecated \
122+
--output ./caddy-v2.7.5-extras
123+
```
124+
125+
Using `CGO_ENABLED=0` can only build pure Go programs, which means that
126+
cross-compiling is guaranteed to succeed (assuming the modules don't contain C),
127+
however, file sizes may be slightly larger for bundling Go modules for DNS
128+
resolution, etc, which your OS would otherwise provide.
129+
130+
Common OSes and Arches are:
131+
132+
- `linux`, `darwin`, `windows`, `freebsd`
133+
- `amd64`, `arm64`, `mips`, `ppc64le`
134+
135+
See also:
136+
137+
- OSes: `go tool dist list | cut -d/ -f1 | sort -u`
138+
- Arches: `go tool dist list | cut -d/ -f2 | sort -u`
139+
140+
### How to Build From a Feature Branch
141+
142+
The `build` is actually a [commit-ish][commit-ish] (a.k.a. a "git ref"), which
143+
can apply to any branch of the official repository:
144+
145+
```sh
146+
xcaddy build "file-placeholder"
147+
```
148+
149+
See also:
150+
151+
- <https://github.com/caddyserver/caddy/tags>
152+
- <https://github.com/caddyserver/caddy/branches>
153+
- <https://github.com/caddyserver/caddy/commits/master>
154+
155+
[commit-ish]:
156+
https://mirrors.edge.kernel.org/pub/software/scm/git/docs/#_identifier_terminology
157+
158+
### How to List Built-In Modules
159+
160+
When it's time to rebuild `caddy` and you've forgotten which modules you used to
161+
build it, fear not!
162+
163+
`list-modules` to the rescue!
164+
165+
```sh
166+
caddy list-modules
167+
```
168+
169+
```text
170+
admin.api.load
171+
# ... 104 other modules
172+
tls.stek.standard
173+
174+
Standard modules: 106
175+
176+
dns.providers.duckdns
177+
178+
Non-standard modules: 1
179+
```
180+
181+
### Common Options
182+
183+
Here are some of the common environment variables that change xcaddy's behavior:
184+
185+
| Option | Description |
186+
| ---------------------- | ------------------------------------------------------------- |
187+
| `CGO_ENABLED` | Set to `0` to build pure Go (no C or OS libraries) |
188+
| `GOOS` | For cross-compiling: `linux`, `darwin`, `windows`, etc |
189+
| `GOARCH` | For cross-compiling: `amd64`, `arm64`, `arm`, etc |
190+
| `GOARM` | For cross-compiling arm (32-bit): `7` or `6` (8 is arm64) |
191+
| `CADDY_VERSION` | Equivalent to `xcaddy build "$CADDY_VERSION" ...` |
192+
| `XCADDY_SETCAP=1` | Linux: runs [`sudo setcap cap_net_bind_service=+ep`][netbind] |
193+
| `XCADDY_SUDO=0` | Don't use `sudo` for `XCADDY_SETCAP` (i.e. on Alpine) |
194+
| `XCADDY_RACE_DETECTOR` | For finding race conditions in feature builds |
195+
| `XCADDY_DEBUG=1` | Disables `-ldflags '-w -s'`, for debug builds |
196+
| `XCADDY_SKIP_CLEANUP` | Leaves build files (for debugging failure) |
197+
| `XCADDY_SKIP_BUILD` | For use with building & releasing custom builds w/ GoReleaser |
198+
199+
[netbind]: ../setcap-netbind/
200+
201+
See also:
202+
203+
- `go tool dist list`
204+
- <https://github.com/caddyserver/xcaddy#environment-variables>

xcaddy/install.ps1

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env pwsh
2+
3+
##################
4+
# Install xcaddy #
5+
##################
6+
7+
# Every package should define these variables
8+
$pkg_cmd_name = "xcaddy"
9+
10+
$pkg_dst_cmd = "$Env:USERPROFILE\.local\bin\xcaddy.exe"
11+
$pkg_dst_bin = "$Env:USERPROFILE\.local\bin"
12+
$pkg_dst = "$pkg_dst_cmd"
13+
14+
$pkg_src_cmd = "$Env:USERPROFILE\.local\opt\xcaddy-v$Env:WEBI_VERSION\bin\xcaddy.exe"
15+
$pkg_src_bin = "$Env:USERPROFILE\.local\opt\xcaddy-v$Env:WEBI_VERSION\bin"
16+
$pkg_src_dir = "$Env:USERPROFILE\.local\opt\xcaddy-v$Env:WEBI_VERSION"
17+
$pkg_src = "$pkg_src_cmd"
18+
19+
New-Item "$Env:USERPROFILE\Downloads\webi" -ItemType Directory -Force | Out-Null
20+
$pkg_download = "$Env:USERPROFILE\Downloads\webi\$Env:WEBI_PKG_FILE"
21+
22+
# Fetch Go compiler
23+
Write-Output "Checking for Go compiler..."
24+
IF (-not (Test-Path "$Env:USERPROFILE\.local\opt\go")) {
25+
& "$Env:USERPROFILE\.local\bin\webi-pwsh.ps1" go
26+
}
27+
28+
# Fetch archive
29+
IF (!(Test-Path -Path "$pkg_download")) {
30+
Write-Output "Downloading xcaddy from $Env:WEBI_PKG_URL to $pkg_download"
31+
& curl.exe -A "$Env:WEBI_UA" -fsSL "$Env:WEBI_PKG_URL" -o "$pkg_download.part"
32+
& Move-Item "$pkg_download.part" "$pkg_download"
33+
}
34+
35+
IF (!(Test-Path -Path "$pkg_src_cmd")) {
36+
Write-Output "Installing xcaddy"
37+
38+
# TODO: create package-specific temp directory
39+
# Enter tmp
40+
Push-Location .local\tmp
41+
42+
# Remove any leftover tmp cruft
43+
Remove-Item -Path ".\xcaddy-v*" -Recurse -ErrorAction Ignore
44+
Remove-Item -Path ".\xcaddy.exe" -Recurse -ErrorAction Ignore
45+
46+
# Unpack archive file into this temporary directory
47+
# Windows BSD-tar handles zip. Imagine that.
48+
Write-Output "Unpacking $pkg_download"
49+
& tar xf "$pkg_download"
50+
51+
# Settle unpacked archive into place
52+
Write-Output "Install Location: $pkg_src_cmd"
53+
New-Item "$pkg_src_bin" -ItemType Directory -Force | Out-Null
54+
Move-Item -Path ".\xcaddy.exe" -Destination "$pkg_src_bin"
55+
56+
# Exit tmp
57+
Pop-Location
58+
}
59+
60+
Write-Output "Copying into '$pkg_dst_cmd' from '$pkg_src_cmd'"
61+
Remove-Item -Path "$pkg_dst_cmd" -Recurse -ErrorAction Ignore | Out-Null
62+
New-Item "$pkg_dst_bin" -ItemType Directory -Force | Out-Null
63+
Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse

xcaddy/install.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/sh
2+
3+
# The generic functions - version checks, download, extract, etc - are here:
4+
# - https://github.com/webinstall/packages/branches/master/_webi/template.sh
5+
6+
set -e
7+
set -u
8+
9+
pkg_cmd_name="xcaddy"
10+
11+
# IMPORTANT: this let's other functions know to expect this to be a single file
12+
WEBI_SINGLE=true
13+
14+
# Every package should define these 6 variables
15+
pkg_cmd_name="xcaddy"
16+
17+
pkg_dst_cmd="$HOME/.local/bin/xcaddy"
18+
#pkg_dst="$pkg_dst_cmd"
19+
20+
pkg_src_cmd="$HOME/.local/opt/xcaddy-v$WEBI_VERSION/bin/xcaddy"
21+
#pkg_src_dir="$HOME/.local/opt/xcaddy-v$WEBI_VERSION/bin"
22+
#pkg_src="$pkg_src_cmd"
23+
24+
pkg_get_current_version() {
25+
# 'xcaddy version' has output in this format:
26+
# v0.3.5 h1:XyC3clncb2Q3gTQC6hOJerRt3FS9+vAljW1f8jlryZA=
27+
# This trims it down to just the version number:
28+
# 0.3.5
29+
xcaddy version 2> /dev/null |
30+
head -n 1 |
31+
cut -d' ' -f1 |
32+
cut -c 2-
33+
}
34+
35+
pkg_install() {
36+
echo "Checking for Go compiler..."
37+
if ! command -v go 2> /dev/null; then
38+
"$HOME/.local/bin/webi" go
39+
export PATH="$HOME/.local/opt/go/bin:$PATH"
40+
fi
41+
42+
# $HOME/.local/opt/xcaddy-v0.3.5/bin
43+
mkdir -p "${pkg_src_bin}"
44+
45+
# mv ./xcaddy* "$HOME/.local/opt/xcaddy-v0.3.5/bin/xcaddy"
46+
mv ./"${pkg_cmd_name}"* "${pkg_src_cmd}"
47+
48+
# chmod a+x "$HOME/.local/opt/xcaddy-v0.3.5/bin/xcaddy"
49+
chmod a+x "${pkg_src_cmd}"
50+
}
51+
52+
pkg_link() {
53+
# rm -f "$HOME/.local/bin/xcaddy"
54+
rm -f "${pkg_dst_cmd}"
55+
56+
# ln -s "$HOME/.local/opt/xcaddy-v0.3.5/bin/xcaddy" "$HOME/.local/bin/xcaddy"
57+
ln -s "${pkg_src_cmd}" "${pkg_dst_cmd}"
58+
}

xcaddy/releases.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var github = require('../_common/github.js');
4+
var owner = 'caddyserver';
5+
var repo = 'xcaddy';
6+
7+
module.exports = function (request) {
8+
return github(request, owner, repo).then(function (all) {
9+
// remove checksums and .deb
10+
all.releases = all.releases.filter(function (rel) {
11+
return !/(\.txt)|(\.deb)$/i.test(rel.name);
12+
});
13+
return all;
14+
});
15+
};
16+
17+
if (module === require.main) {
18+
module.exports(require('@root/request')).then(function (all) {
19+
all = require('../_webi/normalize.js')(all);
20+
console.info(JSON.stringify(all));
21+
});
22+
}

0 commit comments

Comments
 (0)