Skip to content

Commit

Permalink
refactor: improve Caddy config (api-platform#2548)
Browse files Browse the repository at this point in the history
* refactor: improve Caddy config

* drop the useless matcher

* improved version

* caddy fmt

* Revert "caddy fmt"

This reverts commit 0471701.

* fix mixed tabs and spaces

* fix editorconfig
  • Loading branch information
dunglas authored Oct 25, 2023
1 parent b02c995 commit 06b46f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
9 changes: 4 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ indent_size = 4
[composer.json]
indent_size = 4

[compose{,.*}.{yaml,yml}]
[{,docker-}compose{,.*}.{yaml,yml}]
indent_style = space
indent_size = 2

[docker-compose{,.*}.{yaml,yml}]
indent_style = space
indent_size = 2
[{,*.*}Dockerfile]
indent_style = tab

[Dockerfile]
[{,*.*}Caddyfile]
indent_style = tab
73 changes: 29 additions & 44 deletions api/frankenphp/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,31 @@
frankenphp {
{$FRANKENPHP_CONFIG}
}
}

{$SERVER_NAME:localhost}
# https://caddyserver.com/docs/caddyfile/directives#sorting-algorithm
order mercure after encode
order vulcain after reverse_proxy
order php_server before file_server
}

{$CADDY_EXTRA_CONFIG}

log {
# Redact the authorization query parameter that can be set by Mercure
format filter {
wrap console
fields {
uri query {
replace authorization REDACTED
{$SERVER_NAME:localhost} {
log {
# Redact the authorization query parameter that can be set by Mercure
format filter {
wrap console
fields {
uri query {
replace authorization REDACTED
}
}
}
}
}

# Matches requests for HTML documents, for static files and for Next.js files,
# except for known API paths and paths with extensions handled by API Platform
@pwa expression `(
header({'Accept': '*text/html*'})
&& !path(
'/docs*', '/graphql*', '/bundles*', '/contexts*', '/_profiler*', '/_wdt*',
'*.json*', '*.html', '*.csv', '*.yml', '*.yaml', '*.xml'
)
)
|| path('/favicon.ico', '/manifest.json', '/robots.txt', '/_next*', '/sitemap*')`

route {
root * /app/public
encode zstd gzip

mercure {
# Transport to use (default to Bolt)
transport_url {$MERCURE_TRANSPORT_URL:bolt:///data/mercure.db}
Expand All @@ -49,37 +43,28 @@ route {
# Extra directives
{$MERCURE_EXTRA_DIRECTIVES}
}

vulcain

# Add links to the API docs and to the Mercure Hub if not set explicitly (e.g. the PWA)
header ?Link `</docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation", </.well-known/mercure>; rel="mercure"`
# Disable Topics tracking if not enabled explicitly: https://github.com/jkarlin/topics
header ?Permissions-Policy "browsing-topics=()"

# Matches requests for HTML documents, for static files and for Next.js files,
# except for known API paths and paths with extensions handled by API Platform
@pwa expression `(
header({'Accept': '*text/html*'})
&& !path(
'/docs*', '/graphql*', '/bundles*', '/contexts*', '/_profiler*', '/_wdt*',
'*.json*', '*.html', '*.csv', '*.yml', '*.yaml', '*.xml'
)
)
|| path('/favicon.ico', '/manifest.json', '/robots.txt', '/_next*', '/sitemap*')`

# Comment the following line if you don't want Next.js to catch requests for HTML documents.
# In this case, they will be handled by the PHP app.
reverse_proxy @pwa http://{$PWA_UPSTREAM}

# Add trailing slash for directory requests
@canonicalPath {
file {path}/index.php
not path */
}
redir @canonicalPath {path}/ 308

# If the requested file does not exist, try index files
@indexFiles file {
try_files {path} {path}/index.php index.php
split_path .php
}
rewrite @indexFiles {http.matchers.file.relative}

# FrankenPHP!
@phpFiles path *.php
php @phpFiles

encode zstd gzip
file_server

respond 404
php_server
}
1 change: 1 addition & 0 deletions compose.override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
extra_hosts:
# Ensure that host.docker.internal is correctly defined on Linux
- host.docker.internal:host-gateway
tty: true

pwa:
build:
Expand Down

0 comments on commit 06b46f4

Please sign in to comment.