From 7cb8668cb20f8c19086d128392dc487e997e806f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 2 Dec 2020 00:29:12 +0100 Subject: [PATCH] feat: add built-in Mercure support (#89) * feat: Mercure support * feat: add native Mercure support * improvements * feat: add Mercure UI * fix: typo * finish --- Dockerfile | 6 +++++- README.md | 5 +++-- docker-compose.prod.yml | 6 ++++++ docker-compose.yml | 6 +++++- docker/caddy/Caddyfile | 14 ++++++++++++++ docs/build.md | 6 ++++-- docs/extra-services.md | 1 + 7 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 403ef03..7b6bda6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ # https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact ARG PHP_VERSION=7.4 -ARG CADDY_VERSION=2.1.1 +ARG CADDY_VERSION=2 # "php" stage FROM php:${PHP_VERSION}-fpm-alpine AS symfony_php @@ -109,12 +109,16 @@ CMD ["php-fpm"] FROM caddy:${CADDY_VERSION}-builder-alpine AS symfony_caddy_builder RUN xcaddy build \ + --with github.com/dunglas/mercure@main \ + --with github.com/dunglas/mercure/caddy@main \ --with github.com/dunglas/vulcain/caddy FROM caddy:${CADDY_VERSION} AS symfony_caddy WORKDIR /srv/app +ENV MERCURE_DEMO="demo /srv/mercure-assets/" +COPY --from=dunglas/mercure:v0.11 /srv/public /srv/mercure-assets/ COPY --from=symfony_caddy_builder /usr/bin/caddy /usr/bin/caddy COPY --from=symfony_php /srv/app/public public/ COPY docker/caddy/Caddyfile /etc/caddy/Caddyfile diff --git a/README.md b/README.md index d9e612a..d33c3d0 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,9 @@ A [Docker](https://www.docker.com/)-based installer and runtime for the [Symfony * Production, development and CI ready * Automatic HTTPS (in dev and in prod!) -* HTTP/2, HTTP/3 and [Server Push](https://symfony.com/doc/current/web_link.html) support -* [Vulcain](https://vulcain.rocks)-enabled +* HTTP/2, HTTP/3 and [Preload](https://symfony.com/doc/current/web_link.html) support +* Built-in [Mercure](https://symfony.com/doc/current/mercure.html) hub +* [Vulcain](https://vulcain.rocks) support * Just 2 services (PHP FPM and Caddy server) * Super-readable configuration diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 6544bc6..8e787fe 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -5,3 +5,9 @@ services: php: environment: APP_ENV: prod + + caddy: + environment: + MERCURE_DEMO: # Disable the demo + MERCURE_PUBLISHER_JWT: ${MERCURE_PUBLISHER_JWT} + MERCURE_SUBSCRIBER_JWT: ${MERCURE_SUBSCRIBER_JWT} diff --git a/docker-compose.yml b/docker-compose.yml index d124b8f..f4d525b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,6 +17,8 @@ services: environment: # Run "composer require symfony/orm-pack" to install and configure Doctrine ORM DATABASE_URL: postgresql://${POSTGRES_USER:-symfony}:${POSTGRES_PASSWORD:-ChangeMe}@database:5432/${POSTGRES_DB:-app}?serverVersion=${POSTGRES_VERSION:-13} + # Run "composer require symfony/mercure-bundle" to install and configure the Mercure integration + MERCURE_PUBLISH_URL: ${MERCURE_URL:-http://caddy/.well-known/mercure} SYMFONY_VERSION: caddy: @@ -24,7 +26,9 @@ services: context: . target: symfony_caddy environment: - SERVER_NAME: ${SERVER_NAME:-localhost} + SERVER_NAME: ${SERVER_NAME:-localhost, caddy:80} + MERCURE_PUBLISHER_JWT: ${MERCURE_PUBLISHER_JWT:-!ChangeMe!} + MERCURE_SUBSCRIBER_JWT: ${MERCURE_SUBSCRIBER_JWT:-!ChangeMe!} restart: unless-stopped ports: - "80:80" diff --git a/docker/caddy/Caddyfile b/docker/caddy/Caddyfile index ff255a3..e6d81ff 100644 --- a/docker/caddy/Caddyfile +++ b/docker/caddy/Caddyfile @@ -8,6 +8,20 @@ log route { root * /srv/app/public + mercure { + # Transport to use (default to Bolt) + transport_url bolt:///data/mercure.db + # Enable the demo endpoint (disable it in production!) + {$MERCURE_DEMO} + # Publisher JWT key + publisher_jwt {$MERCURE_PUBLISHER_JWT} + # Subscriber JWT key + subscriber_jwt {$MERCURE_SUBSCRIBER_JWT} + # Allow anonymous subscribers (double-check that it's what you want) + anonymous + # Enable the subscription API (double-check that it's what you want) + subscriptions + } vulcain push php_fastcgi php:9000 diff --git a/docs/build.md b/docs/build.md index 23aa09d..b6a6a10 100644 --- a/docs/build.md +++ b/docs/build.md @@ -17,6 +17,8 @@ For instance, use the following command to use the `master` branch of Symfony: ## Customizing the Server Name -Use the `SERVER_NAME` environment variable to define your custom server name. +Use the `SERVER_NAME` environment variable to define your custom server name(s). - $ SERVER_NAME=symfony.wip docker-compose up --build + $ SERVER_NAME="symfony.wip, caddy:80" docker-compose up --build + +If you use Mercure, keep `caddy:80` in the list to allow the PHP container to request the caddy service. diff --git a/docs/extra-services.md b/docs/extra-services.md index 75fec31..05f24a7 100644 --- a/docs/extra-services.md +++ b/docs/extra-services.md @@ -6,4 +6,5 @@ the recipe will automatically modify the `Dockerfile` and `docker-compose.yml` t The currently supported packages are: * `symfony/orm-pack`: install a PostgreSQL service +* `symfony/mercure-bundle`: use the Mercure.rocks module shipped with Caddy * `symfony/messenger`: install a RabbitMQ service