Skip to content

Commit

Permalink
feat: add build tag to skip Brotli support (#1070)
Browse files Browse the repository at this point in the history
* feat: add build tag to skip Brotli support

* update docs
  • Loading branch information
dunglas authored Oct 8, 2024
1 parent 56d2f99 commit 95c381e
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion alpine.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions caddy/br-skip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !brotli

package caddy

var brotli = false
5 changes: 5 additions & 0 deletions caddy/br.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build brotli

package caddy

var brotli = true
27 changes: 21 additions & 6 deletions caddy/php-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
}

Expand Down
2 changes: 1 addition & 1 deletion dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
22 changes: 21 additions & 1 deletion docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
```

0 comments on commit 95c381e

Please sign in to comment.