diff --git a/README.md b/README.md index 4992ce7..1c47b29 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ pnpm --filter api test:e2e # API end to end; needs the containers abo ## Deploying -`docker-compose.yml` runs the whole app on one server: Caddy in front (HTTPS with certificates it fetches itself), the web app, the API, PostgreSQL, Redis and LiveKit. +`docker-compose.yml` runs the app on one server: the web app, the API, PostgreSQL, Redis and LiveKit. The way in is the server's own Caddy, a separate compose project that other projects share: it gets the HTTPS certificates and reaches this one over the external Docker network `web`. 1. Point three DNS names at the server: `example.com`, `api.example.com` and `livekit.example.com` (yours in place of `example.com`). The web app and the API must share a parent domain like this: the session cookie is only sent between pages of one site. -2. Open the firewall for 80 and 443 (TCP), 443 (UDP), and for calls 7881 (TCP) and 7882 (UDP). +2. Let 80 and 443 (TCP) through to Caddy, and for calls 7881 (TCP) and 7882 (UDP) through to this server: on the firewall, and on the router if the server is behind one. 3. On the server, in a checkout of this repository: ```bash @@ -49,6 +49,8 @@ pnpm --filter api test:e2e # API end to end; needs the containers abo docker compose up -d --build ``` +4. Mount `deploy/Caddyfile` into the shared Caddy as one of its site files (`/etc/caddy/sites/electro4ort.caddy`) and restart it. The three names are written out in that file: change them there if `DOMAIN` is not `electro4ort.com.ua`. + To update: `git pull`, then `docker compose up -d --build` again. Migrations are applied by the `migrate` service before the new API starts. What to know before relying on it: diff --git a/deploy/Caddyfile b/deploy/Caddyfile index 87e5fa8..80fed75 100644 --- a/deploy/Caddyfile +++ b/deploy/Caddyfile @@ -1,7 +1,12 @@ -# The one door from the internet: gets and renews the TLS certificates, and -# hands each name to its service. DOMAIN comes from .env beside -# docker-compose.yml; all three names must point at this server. -(common) { +# electro4ort's sites for the server's Caddy, which mounts this file among its +# sites (/etc/caddy/sites/electro4ort.caddy), gets the TLS certificates and +# reaches the services over the shared "web" network. +# +# The names are written out because that Caddy doesn't read this project's +# .env; they must match DOMAIN there, and all three must point at the server. +# The snippet is named after the project because every site file shares one +# namespace. +(electro4ort) { encode zstd gzip header { Strict-Transport-Security "max-age=31536000; includeSubDomains" @@ -10,8 +15,8 @@ } # The web app. -{$DOMAIN} { - import common +electro4ort.com.ua { + import electro4ort header { X-Content-Type-Options nosniff X-Frame-Options DENY @@ -22,18 +27,18 @@ # Built files carry a hash of their content in their name. @built path /assets/* header @built Cache-Control "public, max-age=31536000, immutable" - reverse_proxy web:3000 + reverse_proxy electro4ort-web:3000 } # The API and its socket. It sets its own security headers. -api.{$DOMAIN} { - import common - reverse_proxy api:3000 +api.electro4ort.com.ua { + import electro4ort + reverse_proxy electro4ort-api:3000 } # Signalling for calls. The media itself doesn't pass through here: browsers # send it straight to LiveKit, on the ports docker-compose.yml opens. -livekit.{$DOMAIN} { - import common - reverse_proxy livekit:7880 +livekit.electro4ort.com.ua { + import electro4ort + reverse_proxy electro4ort-livekit:7880 } diff --git a/docker-compose.yml b/docker-compose.yml index 480503e..1dece44 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,12 @@ # cp .env.example .env # then fill it in # docker compose up -d --build # Development uses docker-compose.local.yml instead. See README.md. +# +# The server's own Caddy (a separate compose project) is the way in: it loads +# deploy/Caddyfile and reaches these services over the shared "web" network. +# Every project on that network sees the others' service names, so the three +# services that join it carry the project's name: a plain "web" or "api" would +# answer for another project's lookups, and theirs for ours. name: electro4ort x-api: &api @@ -26,21 +32,21 @@ x-api: &api S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:-} # Browsers come in through Caddy; the API asks over the private network. LIVEKIT_URL: wss://livekit.${DOMAIN} - LIVEKIT_INTERNAL_URL: http://livekit:7880 + LIVEKIT_INTERNAL_URL: http://electro4ort-livekit:7880 LIVEKIT_API_KEY: ${LIVEKIT_API_KEY:?set LIVEKIT_API_KEY in .env} LIVEKIT_API_SECRET: ${LIVEKIT_API_SECRET:?set LIVEKIT_API_SECRET in .env} services: - web: + electro4ort-web: build: context: . target: web args: VITE_API_URL: https://api.${DOMAIN} - networks: - - web image: electro4ort-web restart: unless-stopped + networks: + - web # only the proxy talks to it # Brings the database up to this version, then exits. The API waits for it. migrate: @@ -53,13 +59,14 @@ services: postgres: condition: service_healthy - api: + electro4ort-api: <<: *api volumes: # Uploaded files, while STORAGE_DRIVER is "local". - files:/data/files networks: - - web + - default # reaches postgres, redis and livekit + - web # shared with the proxy depends_on: migrate: condition: service_completed_successfully @@ -94,8 +101,9 @@ services: retries: 10 # Voice and video. Signalling comes through Caddy; the media goes straight - # between browsers and these two ports, which the firewall must let in. - livekit: + # between browsers and these two ports, which the router and the firewall + # must let in. + electro4ort-livekit: image: livekit/livekit-server:v1.13 restart: unless-stopped environment: @@ -110,10 +118,13 @@ services: webhook: api_key: ${LIVEKIT_API_KEY} urls: - - http://api:3000/livekit/webhook + - http://electro4ort-api:3000/livekit/webhook ports: - "7881:7881" - "7882:7882/udp" + networks: + - default # reaches the API, for the webhook + - web # shared with the proxy volumes: postgres-data: