diff --git a/api/.env b/api/.env index 409c2c11e2..94598182e3 100644 --- a/api/.env +++ b/api/.env @@ -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 ###> symfony/framework-bundle ### APP_ENV=dev diff --git a/api/Dockerfile b/api/Dockerfile index d036635e90..4e9210c93b 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -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 diff --git a/api/config/packages/api_platform.yaml b/api/config/packages/api_platform.yaml index df00eeaf03..677d251179 100644 --- a/api/config/packages/api_platform.yaml +++ b/api/config/packages/api_platform.yaml @@ -7,6 +7,18 @@ api_platform: defaults: stateless: true cache_headers: + max_age: 0 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)%'] \ No newline at end of file diff --git a/api/docker/caddy/Caddyfile b/api/docker/caddy/Caddyfile index 7cf0fd6f14..ba2902145f 100644 --- a/api/docker/caddy/Caddyfile +++ b/api/docker/caddy/Caddyfile @@ -6,6 +6,8 @@ log +cache + # 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 `( diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 22d8e1d481..67abd543b4 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -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: diff --git a/docker-compose.yml b/docker-compose.yml index f9d20b69d6..28bf371375 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: | + order cache before rewrite + cache { + api { + souin + } + } restart: unless-stopped volumes: - php_socket:/var/run/php