From 95c381ec78c4c957df542c7a6f5f5300e928cab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 8 Oct 2024 18:57:34 +0200 Subject: [PATCH] feat: add build tag to skip Brotli support (#1070) * feat: add build tag to skip Brotli support * update docs --- Dockerfile | 2 +- alpine.Dockerfile | 2 +- build-static.sh | 2 +- caddy/br-skip.go | 5 +++++ caddy/br.go | 5 +++++ caddy/php-server.go | 27 +++++++++++++++++++++------ dev.Dockerfile | 2 +- docs/compile.md | 22 +++++++++++++++++++++- 8 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 caddy/br-skip.go create mode 100644 caddy/br.go diff --git a/Dockerfile b/Dockerfile index a92104d8b..3c39264f7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -101,7 +101,7 @@ ENV CGO_CPPFLAGS=$PHP_CPPFLAGS ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" WORKDIR /go/src/app/caddy/frankenphp -RUN GOBIN=/usr/local/bin go install -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ +RUN GOBIN=/usr/local/bin go install -tags 'brotli' -ldflags "-w -s -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \ cp Caddyfile /etc/caddy/Caddyfile && \ frankenphp version diff --git a/alpine.Dockerfile b/alpine.Dockerfile index 81f5bf7da..c87dd5ff5 100644 --- a/alpine.Dockerfile +++ b/alpine.Dockerfile @@ -122,7 +122,7 @@ ENV CGO_CPPFLAGS=$PHP_CPPFLAGS ENV CGO_LDFLAGS="-lssl -lcrypto -lreadline -largon2 -lcurl -lonig -lz $PHP_LDFLAGS" WORKDIR /go/src/app/caddy/frankenphp -RUN GOBIN=/usr/local/bin go install -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ +RUN GOBIN=/usr/local/bin go install -tags 'brotli' -ldflags "-w -s -extldflags '-Wl,-z,stack-size=0x80000' -X 'github.com/caddyserver/caddy/v2.CustomVersion=FrankenPHP $FRANKENPHP_VERSION PHP $PHP_VERSION Caddy'" && \ setcap cap_net_bind_service=+ep /usr/local/bin/frankenphp && \ upx --best /usr/local/bin/frankenphp && \ frankenphp version diff --git a/build-static.sh b/build-static.sh index ae5882d22..2e656ce57 100755 --- a/build-static.sh +++ b/build-static.sh @@ -249,7 +249,7 @@ fi cd caddy/frankenphp/ go env -go build -buildmode=pie -tags "cgo netgo osusergo static_build" -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}" +go build -buildmode=pie -tags "cgo netgo osusergo static_build brotli" -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}" cd ../.. if [ -d "${EMBED}" ]; then diff --git a/caddy/br-skip.go b/caddy/br-skip.go new file mode 100644 index 000000000..7ff7f96be --- /dev/null +++ b/caddy/br-skip.go @@ -0,0 +1,5 @@ +//go:build !brotli + +package caddy + +var brotli = false diff --git a/caddy/br.go b/caddy/br.go new file mode 100644 index 000000000..8b303bbc8 --- /dev/null +++ b/caddy/br.go @@ -0,0 +1,5 @@ +//go:build brotli + +package caddy + +var brotli = true diff --git a/caddy/php-server.go b/caddy/php-server.go index bc9b3c1fa..d4ad872df 100644 --- a/caddy/php-server.go +++ b/caddy/php-server.go @@ -219,15 +219,30 @@ func cmdPHPServer(fs caddycmd.Flags) (int, error) { return caddy.ExitCodeFailedStartup, err } + var ( + encodings caddy.ModuleMap + prefer []string + ) + if brotli { + encodings = caddy.ModuleMap{ + "zstd": caddyconfig.JSON(zstd.New(), nil), + "br": caddyconfig.JSON(br.New(), nil), + "gzip": caddyconfig.JSON(gzip.New(), nil), + } + prefer = []string{"zstd", "br", "gzip"} + } else { + encodings = caddy.ModuleMap{ + "zstd": caddyconfig.JSON(zstd.New(), nil), + "gzip": caddyconfig.JSON(gzip.New(), nil), + } + prefer = []string{"zstd", "gzip"} + } + encodeRoute := caddyhttp.Route{ MatcherSetsRaw: []caddy.ModuleMap{}, HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(encode.Encode{ - EncodingsRaw: caddy.ModuleMap{ - "zstd": caddyconfig.JSON(zstd.New(), nil), - "br": caddyconfig.JSON(br.New(), nil), - "gzip": caddyconfig.JSON(gzip.New(), nil), - }, - Prefer: []string{"zstd", "br", "gzip"}, + EncodingsRaw: encodings, + Prefer: prefer, }, "handler", "encode", nil)}, } diff --git a/dev.Dockerfile b/dev.Dockerfile index 194e31091..4b95f42fd 100644 --- a/dev.Dockerfile +++ b/dev.Dockerfile @@ -78,7 +78,7 @@ WORKDIR /go/src/app COPY . . WORKDIR /go/src/app/caddy/frankenphp -RUN go build -buildvcs=false +RUN go build -buildvcs=false -tags 'brotli' WORKDIR /go/src/app CMD [ "zsh" ] diff --git a/docs/compile.md b/docs/compile.md index 307acfca5..0f6248977 100644 --- a/docs/compile.md +++ b/docs/compile.md @@ -81,7 +81,6 @@ XCADDY_GO_BUILD_FLAGS="-ldflags '-w -s'" \ xcaddy build \ --output frankenphp \ --with github.com/dunglas/frankenphp/caddy \ - --with github.com/dunglas/caddy-cbrotli \ --with github.com/dunglas/mercure/caddy \ --with github.com/dunglas/vulcain/caddy # Add extra Caddy modules here @@ -96,3 +95,24 @@ xcaddy build \ > To do so, change the `XCADDY_GO_BUILD_FLAGS` environment variable to something like > `XCADDY_GO_BUILD_FLAGS=$'-ldflags "-w -s -extldflags \'-Wl,-z,stack-size=0x80000\'"'` > (change the value of the stack size according to your app needs). + +## Build Tags + +Additional features can be enabled if the required C libraries are installed by +passing additional build tags to the Go compiler: + +| Tag | Dependencies | Description | +|--------|--------------------------------------------|--------------------------------------------------| +| brotli | [Brotli](https://github.com/google/brotli) | Add a Caddy module supporting Brotli compression | + +When using `go build` directly, pass the additional `-tags` option followed by the comma-separated list of tags: + +```console +go build -tags brotli +``` + +When using `xcaddy`, set the `-tags` option in the `XCADDY_GO_BUILD_FLAGS` environment variable: + +```console +XCADDY_GO_BUILD_FLAGS="-tags brotli" +```