# Mail Safehouse — Docker Compose stack.
#
# First time:   ./init.sh            (creates .env and secrets/)
# Start:        docker compose up -d --build
# Logs:         docker compose logs -f api worker
#
# Docker Engine with the Compose plugin (Ubuntu, or Docker CE on RHEL).
# Podman is not supported.
#
# Exposure: only the proxy publishes a port (HTTPS). PostgreSQL, the API and
# the worker are reachable only inside their Compose networks:
#   web       proxy <-> api; also their route out (Microsoft sign-in, setup,
#             alert email; Let's Encrypt if enabled)
#   data      api, worker, migrate <-> postgres  (internal: no route out)
#   outbound  worker -> Microsoft 365, SMTP relay (not reachable from the proxy)
# Publish anything else only by editing this file, and firewall it.

x-app: &app
  image: localhost/mailsafehouse:${S365B_VERSION:-dev}
  build:
    context: ..
  env_file: .env
  environment:
    S365B_DATABASE_URL: postgresql+psycopg://s365b:${POSTGRES_PASSWORD:?run ./init.sh first}@postgres:5432/s365b
    S365B_MASTER_KEY_FILE: /run/secrets/master_key
  secrets: [master_key]
  read_only: true
  tmpfs: [/tmp]
  cap_drop: [ALL]
  security_opt: ["no-new-privileges:true"]
  restart: unless-stopped
  # Rotated JSON logs: up to 10 x 20 MB per container (`docker compose logs`).
  logging:
    driver: json-file
    options: { max-size: "20m", max-file: "10" }

services:
  postgres:
    image: docker.io/library/postgres:17
    environment:
      POSTGRES_USER: s365b
      POSTGRES_DB: s365b
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?run ./init.sh first}
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks: [data]   # no published port, no route out
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U s365b -d s365b"]
      interval: 5s
      timeout: 5s
      retries: 20
    restart: unless-stopped

  # One-shot: applies database migrations, then exits. api and worker wait for it.
  migrate:
    <<: *app
    command: ["migrate"]
    restart: "no"
    networks: [data]
    depends_on:
      postgres: { condition: service_healthy }

  api:
    <<: *app
    command: ["api", "--port", "8000"]
    networks: [web, data]
    volumes:
      - backup-repo:/repo   # creates repositories; browse and restore read from it
    depends_on:
      migrate: { condition: service_completed_successfully }
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:8000/healthz', timeout=3).status == 200 else 1)"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 10s

  # Runs backup jobs and the per-minute scheduler tick. Scale with
  # `docker compose up -d --scale worker=N` (one is plenty for small tenants).
  worker:
    <<: *app
    command: ["worker", "--concurrency", "2"]
    networks: [data, outbound]
    volumes:
      - backup-repo:/repo
    depends_on:
      migrate: { condition: service_completed_successfully }

  proxy:
    image: docker.io/library/caddy:2
    environment:
      S365B_HOSTNAME: ${S365B_HOSTNAME:-localhost}
    networks: [web]
    ports:
      # S365B_BIND: the host address to listen on, e.g. the LAN interface's
      # IP, so the web UI is not published on every interface.
      - "${S365B_BIND:-0.0.0.0}:${S365B_HTTPS_PORT:-443}:443"
      # Plain HTTP only redirects to HTTPS. Not needed with the local CA;
      # uncomment for a Let's Encrypt certificate (HTTP-01 challenge).
      # - "${S365B_BIND:-0.0.0.0}:${S365B_HTTP_PORT:-80}:80"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
      - caddy-config:/config
    depends_on:
      api: { condition: service_healthy }
    restart: unless-stopped

networks:
  web: {}
  data:
    internal: true   # PostgreSQL has no route to anything outside the stack
  outbound: {}

volumes:
  pgdata: {}
  backup-repo: {}   # the backup repository; back this volume up or bind-mount a disk
  caddy-data: {}
  caddy-config: {}

secrets:
  master_key:
    file: ./secrets/master_key
