Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(httpcache): souin integration #2487

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/.env
Expand Up @@ -17,6 +17,8 @@
# API Platform distribution
TRUSTED_PROXIES=127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
TRUSTED_HOSTS=^(localhost|caddy)$
# The api url that is called to invalidate cached resources
SOUIN_API_URL=http://caddy/souin-api/souin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A declared env var without being used, especially in dev/test environments, will throw an error in Symfony commands


###> symfony/framework-bundle ###
APP_ENV=dev
Expand Down
4 changes: 3 additions & 1 deletion api/Dockerfile
Expand Up @@ -119,7 +119,9 @@ ARG TARGETARCH
WORKDIR /srv/app

# Download Caddy compiled with the Mercure and Vulcain modules
ADD --chmod=500 https://caddyserver.com/api/download?os=linux&arch=$TARGETARCH&p=github.com/dunglas/mercure/caddy&p=github.com/dunglas/vulcain/caddy /usr/bin/caddy
ADD --chmod=500 https://caddyserver.com/api/download?os=linux&arch=$TARGETARCH&p=github.com/dunglas/mercure/caddy&p=github.com/dunglas/vulcain/caddy&p=github.com/caddyserver/cache-handler /usr/bin/caddy
# Uncomment the following for experimental HTTP cache features
# ADD --chmod=500 https://caddyserver.com/api/download?os=linux&arch=$TARGETARCH&p=github.com/dunglas/mercure/caddy&p=github.com/dunglas/vulcain/caddy&p=github.com/darkweak/souin/plugins/caddy /usr/bin/caddy
darkweak marked this conversation as resolved.
Show resolved Hide resolved

COPY --link docker/caddy/Caddyfile /etc/caddy/Caddyfile

Expand Down
8 changes: 8 additions & 0 deletions api/config/packages/api_platform.yaml
Expand Up @@ -3,10 +3,18 @@ api_platform:
version: 1.0.0
# Mercure integration, remove if unwanted
mercure: ~
http_cache:
public: true
invalidation:
enabled: true
darkweak marked this conversation as resolved.
Show resolved Hide resolved
purger: 'api_platform.http_cache.purger.souin'
urls: ['%env(SOUIN_API_URL)%']
# Good defaults for REST APIs
defaults:
stateless: true
cache_headers:
max_age: 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP Cache Headers should only be set on prod env

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment still valid for max_age header

shared_max_age: 3600
vary: ['Content-Type', 'Authorization', 'Origin']
extra_properties:
standard_put: true
8 changes: 8 additions & 0 deletions api/docker/caddy/Caddyfile
@@ -1,12 +1,20 @@
{
# Debug
{$CADDY_DEBUG}
order cache before rewrite
cache {
darkweak marked this conversation as resolved.
Show resolved Hide resolved
api {
souin
}
}
}

{$SERVER_NAME}

log

cache
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be enabled through an env var:

Suggested change
cache
{$CADDY_CACHE}

Then, declare this env var in docker-compose.prod.yml and helm chart to only enable cache on production


# 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 `(
Expand Down