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 all 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
2 changes: 1 addition & 1 deletion api/Dockerfile
Expand Up @@ -119,7 +119,7 @@ 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/darkweak/souin/plugins/caddy /usr/bin/caddy

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

Expand Down
12 changes: 12 additions & 0 deletions api/config/packages/api_platform.yaml
Expand Up @@ -7,6 +7,18 @@ api_platform:
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

vary: ['Content-Type', 'Authorization', 'Origin']
extra_properties:
standard_put: true

when@prod:
defaults:
cache_headers:
shared_max_age: 3600
http_cache:
public: true
invalidation:
enabled: true
purger: 'api_platform.http_cache.purger.souin'
urls: ['%env(SOUIN_API_URL)%']
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
urls: ['%env(SOUIN_API_URL)%']
urls: ['%env(SOUIN_API_URL)%']

2 changes: 2 additions & 0 deletions api/docker/caddy/Caddyfile
Expand Up @@ -6,6 +6,8 @@

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
7 changes: 7 additions & 0 deletions docker-compose.prod.yml
Expand Up @@ -22,6 +22,13 @@ services:
environment:
MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}
MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}
CADDY_GLOBAL_OPTIONS: |
order cache before rewrite
cache {
api {
souin
}
}

database:
environment:
Expand Down
7 changes: 7 additions & 0 deletions docker-compose.yml
Expand Up @@ -36,6 +36,13 @@ services:
SERVER_NAME: ${SERVER_NAME:-localhost}, caddy:80
MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET:-!ChangeThisMercureHubJWTSecretKey!}
CADDY_GLOBAL_OPTIONS: |
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not enable cache on dev

order cache before rewrite
cache {
api {
souin
}
}
restart: unless-stopped
volumes:
- php_socket:/var/run/php
Expand Down