# bp-gateway — standalone compose
#
# Works with ANY ERPNext deployment:
#   - Self-hosted (same VPS): set base_url: http://host.docker.internal:8000
#   - Frappe Cloud:           set base_url: https://yoursite.frappe.cloud
#
# Setup:
#   1. cp ../config.example.yaml gateway.yaml
#   2. cp Caddyfile.example Caddyfile
#   3. Edit gateway.yaml — set frappe.base_url, api_key/secret, license JWT
#   4. Edit Caddyfile    — set your domain
#   5. docker compose up -d

services:
  caddy:
    image: caddy:2-alpine
    container_name: bp-gateway-caddy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"   # HTTP/3
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
      - caddy-config:/config
    depends_on:
      - gateway

  gateway:
    # Pinned by install.sh (in .env, next to this file) to the exact version
    # its compat-manifest resolution picked — never bare `:latest` for an
    # actual install, so an update elsewhere can't silently skew compat.
    image: public.ecr.aws/j1z8j2i7/bp-gateway:${GATEWAY_IMAGE_TAG:-latest}
    container_name: bp-gateway
    restart: unless-stopped
    # Makes `base_url: http://host.docker.internal:8000` (same-VPS self-host,
    # see header comment) actually resolve. host.docker.internal is a Docker
    # Desktop (Mac/Windows) convenience host — on plain Docker Engine, the
    # default on any Linux VPS, it does NOT resolve without this mapping.
    # Inert if base_url points elsewhere (Frappe Cloud, a separate VPS): the
    # container just carries an unused /etc/hosts entry.
    extra_hosts:
      - "host.docker.internal:host-gateway"
    # No host port — Caddy reaches the gateway over the internal network.
    expose:
      - "8001"
    environment:
      - CONFIG_PATH=/gateway.yaml
    volumes:
      - ./gateway.yaml:/gateway.yaml:ro
      # ReBAC (OpenFGA) runs IN-PROCESS in this same container now — see
      # internal/rebac's package doc + docs/APP-OVERVIEW.md §5.3 in the
      # batch_projects repo. rebac.db_path in gateway.yaml points inside
      # this volume, so the SQLite file survives container recreation the
      # same way redis-data already does; the durable source of truth
      # (Frappe/MariaDB) means even losing this volume is recoverable via
      # the startup rebuild, not a permanent authorization gap.
      - rebac-data:/data/rebac
    depends_on:
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://localhost:8001/health || exit 1"]
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 10s

  redis:
    image: redis:6.2-alpine
    container_name: bp-gateway-redis
    restart: unless-stopped
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

volumes:
  caddy-data:
  caddy-config:
  redis-data:
  rebac-data:
