# Dedicated VPS. Point DNS to the VPS first, then set SLIDELESS_DOMAIN in hPanel.
# Guide: https://docs.antasphere.com/slideless/self-hosting/hostinger
# Keep the project name and all named volumes when recreating containers.
#
# SLIDELESS_DOMAIN is required, but NOT at render time: hPanel downloads this
# file and runs it at once with no environment, and a `${VAR:?}` refusal there
# leaves no project and no readable error ("Deployment failed", 2026-09-18).
# The file renders with an empty domain instead, and the init container refuses
# it with the instruction the customer can act on from Docker Manager.
services:
  init:
    image: ghcr.io/antasphere/slideless:0.11.0@sha256:5d331cb052ef5f16017abaa0720d240b7e6ce2d591e4024ea7dd1978851f86a5
    restart: 'no'
    environment:
      SLIDELESS_DOMAIN: ${SLIDELESS_DOMAIN:-}
    # The image's /data directory belongs to its non-root node user. Docker
    # copies that ownership to the empty volume on its first mount.
    volumes:
      - db_credentials:/data
    read_only: true
    network_mode: none
    command:
      - node
      - --input-type=module
      - --eval
      - |
        import { randomBytes } from 'node:crypto';
        import { existsSync, readFileSync, writeFileSync, renameSync } from 'node:fs';
        import { join } from 'node:path';
        import { isIP } from 'node:net';
        const domain = process.env.SLIDELESS_DOMAIN || '';
        if (!domain) {
          throw new Error('SLIDELESS_DOMAIN is not set. In Docker Manager, open this project, add the environment variable SLIDELESS_DOMAIN with your hostname (for example slides.example.com), then redeploy.');
        }
        // Lowercase only: the same value reaches PUBLIC_BASE_URL and Caddy verbatim,
        // and browsers send lowercase Origins, so a capital letter would break the
        // app's origin comparison instead of failing here with a clear message.
        const label = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
        if (domain.length > 253 || !domain.includes('.') || isIP(domain) || !domain.split('.').every(part => label.test(part))) {
          throw new Error('SLIDELESS_DOMAIN must be a lowercase DNS hostname, without a scheme, port or path.');
        }
        const target = join(process.argv[1], 'postgres-password');
        if (existsSync(target)) {
          if (!/^[a-f0-9]{64}\n?$/.test(readFileSync(target, 'utf8'))) {
            throw new Error('Stored database credential is invalid; restore the db_credentials volume. It will not be replaced.');
          }
        } else {
          const temporary = target + '.tmp';
          writeFileSync(temporary, randomBytes(32).toString('hex') + '\n', { mode: 0o600 });
          renameSync(temporary, target);
        }
        console.log('Database credential ready.');
      - /data

  db:
    image: pgvector/pgvector:0.8.6-pg17@sha256:cf134a767f474095eeba57e0117be8e568e011a63f33fbf252f14c9b760f8e6f
    restart: unless-stopped
    logging:
      driver: json-file
      options:
        max-size: '10m'
        max-file: '5'
    depends_on:
      init:
        condition: service_completed_successfully
    environment:
      POSTGRES_USER: slideless
      POSTGRES_DB: slideless
      POSTGRES_PASSWORD_FILE: /run/slideless-secrets/postgres-password
    volumes:
      - pg_data:/var/lib/postgresql/data
      - db_credentials:/run/slideless-secrets:ro
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U slideless -d slideless']
      interval: 10s
      timeout: 5s
      retries: 12
      start_period: 15s

  app:
    image: ghcr.io/antasphere/slideless:0.11.0@sha256:5d331cb052ef5f16017abaa0720d240b7e6ce2d591e4024ea7dd1978851f86a5
    restart: unless-stopped
    stop_grace_period: 60s
    # Rotated logs: the app logs every request, and a VPS disk is finite.
    logging:
      driver: json-file
      options:
        max-size: '10m'
        max-file: '5'
    depends_on:
      db:
        condition: service_healthy
      init-renderer:
        condition: service_completed_successfully
    environment:
      PUBLIC_BASE_URL: https://${SLIDELESS_DOMAIN:-}
      TRUST_PROXY: 'true'
      ALLOW_INSECURE_SETUP: 'false'
      AUTO_MIGRATE: 'true'
      STORAGE_DRIVER: local
      # Mail is optional: with `none` every invitation and sign-in link stays
      # copyable from the dashboard, so nothing is blocked without it. To send
      # mail instead, set EMAIL_DRIVER to brevo (the house provider), resend or
      # smtp, add the matching credential and an EMAIL_FROM whose domain is
      # verified at the provider (SPF + DKIM), then redeploy:
      #   EMAIL_DRIVER: brevo
      #   BREVO_API_KEY: <your key>
      #   EMAIL_FROM: Slideless <noreply@example.com>
      EMAIL_DRIVER: none
    # Keep the image's tini entrypoint and replace the shell with the server.
    # The generated credential is never interpolated into the Compose file.
    command:
      - sh
      - -ec
      - |
        # Read the credential on its own line: `export X="$(cmd)"` returns
        # export's status, so a failed read would boot with an empty password
        # and surface as "app unhealthy" instead of the real error.
        pw="$$(cat /run/slideless-secrets/postgres-password)"
        test -n "$$pw"
        export DATABASE_URL="postgres://slideless:$${pw}@db:5432/slideless"
        # Deck pictures: hand each pushed version to the renderer. The app never waits
        # on it; while it is down the cards show a plain block.
        rs="$$(cat /run/slideless-renderer/renderer-secret)"
        test -n "$$rs"
        export SLIDELESS_RENDERER_URL=http://renderer:3100
        export SLIDELESS_RENDERER_SECRET="$$rs"
        exec node dist/index.js
    read_only: true
    volumes:
      - app_data:/data
      - db_credentials:/run/slideless-secrets:ro
      - renderer_credentials:/run/slideless-renderer:ro
    tmpfs:
      - /tmp
    healthcheck:
      test: ['CMD', 'wget', '--spider', '-q', 'http://localhost:3000/readyz']
      interval: 10s
      timeout: 5s
      retries: 18
      start_period: 30s

  # Deck pictures (PRDCT-2790). The renderer opens users' deck HTML in a headless
  # Chromium, so it gets its OWN secret in its OWN volume, written here, and never the
  # database credential: it mounts renderer_credentials and nothing else.
  init-renderer:
    image: ghcr.io/antasphere/slideless:0.11.0@sha256:5d331cb052ef5f16017abaa0720d240b7e6ce2d591e4024ea7dd1978851f86a5
    restart: 'no'
    volumes:
      - renderer_credentials:/data
    read_only: true
    network_mode: none
    command:
      - node
      - --input-type=module
      - --eval
      - |
        import { randomBytes } from 'node:crypto';
        import { existsSync, readFileSync, writeFileSync, renameSync } from 'node:fs';
        import { join } from 'node:path';
        const target = join(process.argv[1], 'renderer-secret');
        if (existsSync(target)) {
          if (!/^[a-f0-9]{64}\n?$/.test(readFileSync(target, 'utf8'))) {
            throw new Error('Stored renderer secret is invalid; delete the renderer_credentials volume and redeploy. It will not be replaced.');
          }
        } else {
          const temporary = target + '.tmp';
          writeFileSync(temporary, randomBytes(32).toString('hex') + '\n', { mode: 0o600 });
          renameSync(temporary, target);
        }
        console.log('Renderer secret ready.');
      - /data

  # The renderer: a headless Chromium that makes each deck version's still image,
  # reached by the app only (no published port). Chromium ALWAYS runs inside its own
  # sandbox, which needs Linux user namespaces. The standard install grants them with a
  # seccomp profile file beside the compose file; a one-file template cannot carry that
  # file, so here the container keeps Docker's DEFAULT seccomp profile and holds no
  # capability but SYS_ADMIN and SYS_CHROOT, as the non-root node user with
  # no-new-privileges. With SYS_ADMIN in the container, Docker's default profile lets
  # through every call it ties to that capability (the namespace calls the sandbox
  # needs, and also mount, bpf, perf_event_open and the like): a WIDER filter than the
  # profile file's. No process here holds an effective capability, so the kernel still
  # refuses every privileged one, and the browser starts sandboxed. Never --no-sandbox, never seccomp=unconfined, never
  # privileged. Where the sandbox cannot start, the renderer refuses to run and the deck
  # cards show a plain block; nothing else changes.
  renderer:
    image: ghcr.io/antasphere/slideless-renderer:0.11.0@sha256:e72e866c5ec6f3c20ffe3b8a5c5b71880b5f446470b7e36d5aabcf23f57198bb
    restart: unless-stopped
    stop_grace_period: 30s
    logging:
      driver: json-file
      options:
        max-size: '10m'
        max-file: '5'
    depends_on:
      init-renderer:
        condition: service_completed_successfully
    environment:
      SLIDELESS_URL: http://app:3000
      LOG_LEVEL: info
    # The secret is read from its file, never interpolated into this file.
    command:
      - sh
      - -ec
      - |
        secret="$$(cat /run/slideless-renderer/renderer-secret)"
        test -n "$$secret"
        export SLIDELESS_RENDERER_SECRET="$$secret"
        exec node dist/index.js
    cap_drop:
      - ALL
    cap_add:
      - SYS_ADMIN
      - SYS_CHROOT
    security_opt:
      - no-new-privileges:true
    read_only: true
    tmpfs:
      - /tmp
    volumes:
      - renderer_credentials:/run/slideless-renderer:ro

  caddy:
    image: caddy:2.11.4-alpine@sha256:6aeddd44c3078b0f9a35206472a11420648a79c184603ef95957d0a20044cb2b
    restart: unless-stopped
    logging:
      driver: json-file
      options:
        max-size: '10m'
        max-file: '5'
    depends_on:
      app:
        condition: service_healthy
    # Caddy's reverse-proxy command enables automatic certificates and HTTP
    # redirects. No companion Caddyfile or repository checkout is required.
    command:
      - caddy
      - reverse-proxy
      - --from
      - https://${SLIDELESS_DOMAIN:-}
      - --to
      - http://app:3000
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - caddy_data:/data
      - caddy_config:/config

volumes:
  db_credentials:
  renderer_credentials:
  pg_data:
  app_data:
  caddy_data:
  caddy_config:
