Skip to content

Commit d276032

Browse files
authored
feat(static): add custom Caddy modules support (#1210)
* feat: add custom Caddy modules to the static binary * cs * missing Docker ARG and docs * fix * improve
1 parent 20eaecf commit d276032

File tree

6 files changed

+104
-5
lines changed

6 files changed

+104
-5
lines changed

build-static.sh

+25-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ else
9696
if ! type "composer" >/dev/null; then
9797
packages="composer"
9898
fi
99-
if ! type "go" >/dev/null; then
99+
if ! type "go" >/dev/null 2>&1; then
100100
packages="${packages} go"
101101
fi
102102
if [ -n "${RELEASE}" ] && ! type "gh" >/dev/null 2>&1; then
@@ -125,6 +125,10 @@ else
125125
${spcCommand} build --debug --enable-zts --build-embed ${extraOpts} "${PHP_EXTENSIONS}" --with-libs="${PHP_EXTENSION_LIBS}"
126126
fi
127127

128+
if ! type "xcaddy" >/dev/null 2>&1; then
129+
go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
130+
fi
131+
128132
curlGitHubHeaders=(--header "X-GitHub-Api-Version: 2022-11-28")
129133
if [ "${GITHUB_TOKEN}" ]; then
130134
curlGitHubHeaders+=(--header "Authorization: Bearer ${GITHUB_TOKEN}")
@@ -265,10 +269,27 @@ if [ -n "${EMBED}" ] && [ -d "${EMBED}" ]; then
265269
${md5binary} app.tar | awk '{printf $1}' >app_checksum.txt
266270
fi
267271

268-
cd caddy/frankenphp/
272+
if [ -z "${XCADDY_ARGS}" ]; then
273+
XCADDY_ARGS="--with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy"
274+
fi
275+
276+
XCADDY_DEBUG=0
277+
if [ -n "${DEBUG_SYMBOLS}" ]; then
278+
XCADDY_DEBUG=1
279+
fi
280+
269281
go env
270-
go build -buildmode=pie -tags "cgo,netgo,osusergo,static_build,nobadger,nomysql,nopgx" -ldflags "-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'" -o "../../dist/${bin}"
271-
cd ../..
282+
cd caddy/
283+
# shellcheck disable=SC2086
284+
CGO_ENABLED=1 \
285+
XCADDY_GO_BUILD_FLAGS="-buildmode=pie -tags cgo,netgo,osusergo,static_build,nobadger,nomysql,nopgx -ldflags \"-linkmode=external -extldflags '-static-pie ${extraExtldflags}' ${extraLdflags} -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP ${FRANKENPHP_VERSION} PHP ${LIBPHP_VERSION} Caddy'\"" \
286+
XCADDY_DEBUG="${XCADDY_DEBUG}" \
287+
xcaddy build \
288+
--output "../dist/${bin}" \
289+
${XCADDY_ARGS} \
290+
--with github.com/dunglas/frankenphp=.. \
291+
--with github.com/dunglas/frankenphp/caddy=.
292+
cd ..
272293

273294
if [ -d "${EMBED}" ]; then
274295
truncate -s 0 app.tar

docs/cn/static.md

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ docker buildx bake \
4343
static-builder
4444
```
4545

46+
### 额外的 Caddy 模块
47+
48+
要向 [xcaddy](https://github.com/caddyserver/xcaddy) 添加额外的 Caddy 模块或传递其他参数,请使用 `XCADDY_ARGS` Docker 参数:
49+
50+
```console
51+
docker buildx bake \
52+
--load \
53+
--set static-builder.args.XCADDY_ARGS="--with github.com/darkweak/souin/plugins/caddy --with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy" \
54+
static-builder
55+
```
56+
57+
在本例中,我们为 Caddy 添加了 [Souin](https://souin.io) HTTP 缓存模块,以及 [Mercure](https://mercure.rocks)[Vulcain](https://vulcain.rocks) 模块。
58+
59+
> [!TIP]
60+
>
61+
> 如果 `XCADDY_ARGS` 为空或未设置,则默认包含 Mercure 和 Vulcain 模块。
62+
> 如果自定义了 `XCADDY_ARGS` 的值,则必须显式地包含它们。
63+
4664
参见:[自定义构建](#自定义构建)
4765

4866
### GitHub Token
@@ -75,6 +93,7 @@ cd frankenphp
7593
* `PHP_VERSION`: 要使用的 PHP 版本
7694
* `PHP_EXTENSIONS`: 要构建的 PHP 扩展([支持的扩展列表](https://static-php.dev/zh/guide/extensions.html)
7795
* `PHP_EXTENSION_LIBS`: 要构建的额外库,为扩展添加额外的功能
96+
* `XCADDY_ARGS`:传递给 [xcaddy](https://github.com/caddyserver/xcaddy) 的参数,例如用于添加额外的 Caddy 模块
7897
* `EMBED`: 要嵌入二进制文件的 PHP 应用程序的路径
7998
* `CLEAN`: 设置后,libphp 及其所有依赖项都是重新构建的(不使用缓存)
8099
* `DEBUG_SYMBOLS`: 设置后,调试符号将被保留在二进制文件内

docs/fr/static.md

+19
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ docker buildx bake \
4242
static-builder
4343
```
4444

45+
### Modules supplémentaires de Caddy
46+
47+
Pour ajouter des modules Caddy supplémentaires ou passer d'autres arguments à [xcaddy](https://github.com/caddyserver/xcaddy), utilisez l'argument Docker `XCADDY_ARGS` :
48+
49+
```console
50+
docker buildx bake \
51+
--load \
52+
--set static-builder.args.XCADDY_ARGS="--with github.com/darkweak/souin/plugins/caddy --with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy" \
53+
static-builder
54+
```
55+
56+
Dans cet exemple, nous ajoutons le module de cache HTTP [Souin](https://souin.io) pour Caddy ainsi que les modules [Mercure](https://mercure.rocks) et [Vulcain](https://vulcain.rocks).
57+
58+
> [!TIP]
59+
>
60+
> Les modules Mercure et Vulcain sont inclus par défaut si `XCADDY_ARGS` est vide ou n'est pas défini.
61+
> Si vous personnalisez la valeur de `XCADDY_ARGS`, vous devez les inclure explicitement si vous voulez qu'ils soient inclus.
62+
4563
Voir aussi comment [personnaliser la construction](#personnalisation-de-la-construction)
4664

4765
### Jeton GitHub
@@ -73,6 +91,7 @@ Les variables d'environnement suivantes peuvent être transmises à `docker buil
7391
* `PHP_VERSION` : la version de PHP à utiliser
7492
* `PHP_EXTENSIONS` : les extensions PHP à construire ([liste des extensions prises en charge](https://static-php.dev/en/guide/extensions.html))
7593
* `PHP_EXTENSION_LIBS` : bibliothèques supplémentaires à construire qui ajoutent des fonctionnalités aux extensions
94+
* `XCADDY_ARGS` : arguments à passer à [xcaddy](https://github.com/caddyserver/xcaddy), par exemple pour ajouter des modules Caddy supplémentaires
7695
* `EMBED` : chemin de l'application PHP à intégrer dans le binaire
7796
* `CLEAN` : lorsque défini, `libphp` et toutes ses dépendances sont construites à partir de zéro (pas de cache)
7897
* `DEBUG_SYMBOLS` : lorsque défini, les symboles de débogage ne seront pas supprimés et seront ajoutés dans le binaire

docs/static.md

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ docker buildx bake \
4343
static-builder
4444
```
4545

46+
### Extra Caddy Modules
47+
48+
To add extra Caddy modules or pass other arguments to [xcaddy](https://github.com/caddyserver/xcaddy), use the `XCADDY_ARGS` Docker ARG:
49+
50+
```console
51+
docker buildx bake \
52+
--load \
53+
--set static-builder.args.XCADDY_ARGS="--with github.com/darkweak/souin/plugins/caddy --with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy" \
54+
static-builder
55+
```
56+
57+
In this example, we add the [Souin](https://souin.io) HTTP cache module for Caddy as well as the [Mercure](https://mercure.rocks) and [Vulcain](https://vulcain.rocks) modules.
58+
59+
> [!TIP]
60+
>
61+
> The Mercure and Vulcain modules are included by default if `XCADDY_ARGS` is empty or not set.
62+
> If you customize the value of `XCADDY_ARGS`, you must include them explicitly if you want them to be included.
63+
4664
See also how to [customize the build](#customizing-the-build)
4765

4866
### GitHub Token
@@ -75,6 +93,7 @@ script to customize the static build:
7593
* `PHP_VERSION`: the version of PHP to use
7694
* `PHP_EXTENSIONS`: the PHP extensions to build ([list of supported extensions](https://static-php.dev/en/guide/extensions.html))
7795
* `PHP_EXTENSION_LIBS`: extra libraries to build that add features to the extensions
96+
* `XCADDY_ARGS`: arguments to pass to [xcaddy](https://github.com/caddyserver/xcaddy), for instance to add extra Caddy modules
7897
* `EMBED`: path of the PHP application to embed in the binary
7998
* `CLEAN`: when set, libphp and all its dependencies are built from scratch (no cache)
8099
* `NO_COMPRESS`: don't compress the resulting binary using UPX

docs/tr/static.md

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ docker buildx bake \
4343
static-builder
4444
```
4545

46+
### Ekstra Caddy Modülleri
47+
48+
Ekstra Caddy modülleri eklemek veya [xcaddy](https://github.com/caddyserver/xcaddy) adresine başka argümanlar iletmek için `XCADDY_ARGS` Docker ARG'sini kullanın:
49+
50+
```console
51+
docker buildx bake \
52+
--load \
53+
--set static-builder.args.XCADDY_ARGS="--with github.com/darkweak/souin/plugins/caddy --with github.com/dunglas/mercure/caddy --with github.com/dunglas/vulcain/caddy" \
54+
static-builder
55+
```
56+
57+
Bu örnekte, Caddy için [Souin](https://souin.io) HTTP önbellek modülünün yanı sıra [Mercure](https://mercure.rocks) ve [Vulcain](https://vulcain.rocks) modüllerini ekliyoruz.
58+
59+
> [!TIP]
60+
>
61+
> Mercure ve Vulcain modülleri, `XCADDY_ARGS` boşsa veya ayarlanmamışsa varsayılan olarak dahil edilir.
62+
> Eğer `XCADDY_ARGS` değerini özelleştirirseniz, dahil edilmelerini istiyorsanız bunları açıkça dahil etmelisiniz.
63+
4664
Derlemeyi nasıl [özelleştireceğinize](#yapıyı-özelleştirme) de bakın.
4765

4866
### GitHub Token
@@ -75,6 +93,7 @@ statik derlemeyi özelleştirmek için betik:
7593
* `PHP_VERSION`: kullanılacak PHP sürümü
7694
* `PHP_EXTENSIONS`: oluşturulacak PHP eklentileri ([desteklenen eklentiler listesi](https://static-php.dev/en/guide/extensions.html))
7795
* `PHP_EXTENSION_LIBS`: eklentilere özellikler ekleyen oluşturulacak ekstra kütüphaneler
96+
* `XCADDY_ARGS`: [xcaddy](https://github.com/caddyserver/xcaddy) adresine iletilecek argümanlar, örneğin ekstra Caddy modülleri eklemek için
7897
* `EMBED`: binary dosyaya gömülecek PHP uygulamasının yolu
7998
* `CLEAN`: ayarlandığında, libphp ve tüm bağımlılıkları sıfırdan oluşturulur (önbellek yok)
8099
* `DEBUG_SYMBOLS`: ayarlandığında, hata ayıklama sembolleri ayıklanmayacak ve binary dosyaya eklenecektir

static-builder.Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ENV PHP_VERSION=${PHP_VERSION}
1414

1515
ARG PHP_EXTENSIONS=''
1616
ARG PHP_EXTENSION_LIBS=''
17+
ARG XCADDY_ARGS=''
1718
ARG CLEAN=''
1819
ARG EMBED=''
1920
ARG DEBUG_SYMBOLS=''
@@ -86,7 +87,8 @@ WORKDIR /goroot/src
8687
ENV GOHOSTARCH="$TARGETARCH"
8788
RUN ./make.bash
8889
ENV PATH="/goroot/bin:$PATH"
89-
RUN go version
90+
RUN go version && \
91+
go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
9092

9193
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
9294
ENV COMPOSER_ALLOW_SUPERUSER=1

0 commit comments

Comments
 (0)