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

Conversation

darkweak
Copy link

Q A
Branch? main
Tickets Closes #1947
License MIT
Doc PR api-platform/docs#1720

Obsoletes #2383

@vincentchalamon vincentchalamon mentioned this pull request Aug 23, 2023
18 tasks
darkweak and others added 2 commits August 23, 2023 18:03
Co-authored-by: Vincent <407859+vincentchalamon@users.noreply.github.com>
@vincentchalamon
Copy link
Contributor

The following improvements could be great to add before merge:

  • enable Souin only on prod Symfony environment
  • is there a way to add a header on debug mode, for testing purpose?

@darkweak
Copy link
Author

darkweak commented Aug 23, 2023

Hey, do you have two different Caddyfiles (one for prod and another one for dev)? Assuming yes, just compile your docker image with the cache-handler caddy module, set the cache directive in the prod Caddyfile and don't set it in your dev Caddyfile
On debug environment, I suggest to update the Cache-Status name (using the directive cache_name) it will replace the default Souin value (e.g. Souin; hit; ttl="99".. become Your-debug-cache-name; hit; ...). Unfortunately it's not possible to set a debug header.

@vincentchalamon
Copy link
Contributor

Is something like that would do the work?

# Caddyfile
{
    order cache before rewrite
    cache {
        api {
            souin
        }
    }
}

{$CADDY_CACHE}

# ...
# docker-compose.prod.yml
services:
    caddy:
        environment:
            CADDY_CACHE: cache

I'm wondering if the caddy.api.souin declaration would cause issues even in dev mode. WDYT?

@darkweak
Copy link
Author

darkweak commented Aug 24, 2023

Why don't you link a Caddyfile.prod in your docker-compose.prod.yml instead of the default Caddyfile? Is it mandatory to have only one Caddyfile?

cache directive will crash if your caddy instance has been built with the cache-handler dependency in dev.

@vincentchalamon
Copy link
Contributor

vincentchalamon commented Aug 24, 2023

Using a Caddyfile.prod requires to duplicate the content from Caddyfile, or use import feature and juggle with Caddyfiles files such as:

# Caddyfile.prod

import Caddyfile.dev

cache
# Dockerfile

RUN mv /etc/caddy/Caddyfile /etc/caddy/Caddyfile.dev
COPY --link docker/caddy/Caddyfile.prod /etc/caddy/Caddyfile

But I don't find it as a good approach...

I'm working on an implementation here: api-platform/demo@02e5026

EDIT: implementation commit amended: api-platform/demo@73977b3

@dunglas
Copy link
Member

dunglas commented Aug 24, 2023

Having a single Caddyfile will help with maintenance. It's possible to inject any global options in CADDY_DEBUG. I opened #2502 to improve this.

Copy link
Contributor

@vincentchalamon vincentchalamon left a comment

Choose a reason for hiding this comment

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

Nice integration. Main issue: cache should only be enabled on prod env. Check api-platform/demo@73977b3 for example

api/config/packages/api_platform.yaml Outdated Show resolved Hide resolved
# 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

api/docker/caddy/Caddyfile Outdated Show resolved Hide resolved
api/Dockerfile Outdated Show resolved Hide resolved
@darkweak darkweak force-pushed the feat/httpcache/souin-integration branch 3 times, most recently from 2ec729b to b80d9ff Compare September 12, 2023 10:26
@darkweak darkweak force-pushed the feat/httpcache/souin-integration branch from b80d9ff to 6f0fb64 Compare September 12, 2023 10:28
@darkweak
Copy link
Author

Ping @vincentchalamon

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)%']

@@ -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

# 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.

Comment still valid for max_age header

@@ -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

@@ -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

@vincentchalamon
Copy link
Contributor

I tried to implement it in the demo (cf. api-platform/demo#327) but had to rolled it back because it caused issues. If you want to try and improve this implementation in API Platform, you can test it (locally or with a fork of my branch) with this branch: https://github.com/api-platform/demo/tree/chore/refacto

@darkweak
Copy link
Author

https://github.com/darkweak/souin/issues/new if you have issues using it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Varnish for APIP 2.6
3 participants