x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

x-common-env: &common-env
  REDIS_URL: ${REDIS_URL:-redis://redis:6379}
  REDIS_RATE_LIMIT_URL: ${REDIS_URL:-redis://redis:6379}
  PLAYWRIGHT_MICROSERVICE_URL: ${PLAYWRIGHT_MICROSERVICE_URL:-http://playwright-service:3000/scrape}
  NUQ_DATABASE_URL: ${NUQ_DATABASE_URL:-postgres://postgres:postgres@nuq-postgres:5432/postgres}
  USE_DB_AUTHENTICATION: ${USE_DB_AUTHENTICATION:-false}
  OPENAI_API_KEY: ${OPENAI_API_KEY:-}
  OPENAI_BASE_URL: ${OPENAI_BASE_URL:-}
  MODEL_NAME: ${MODEL_NAME:-}
  MODEL_EMBEDDING_NAME: ${MODEL_EMBEDDING_NAME:-}
  OLLAMA_BASE_URL: ${OLLAMA_BASE_URL:-}
  SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL:-}
  BULL_AUTH_KEY: ${BULL_AUTH_KEY:-@}
  TEST_API_KEY: ${TEST_API_KEY:-}
  POSTHOG_API_KEY: ${POSTHOG_API_KEY:-}
  POSTHOG_HOST: ${POSTHOG_HOST:-}
  SUPABASE_ANON_TOKEN: ${SUPABASE_ANON_TOKEN:-}
  SUPABASE_URL: ${SUPABASE_URL:-}
  SUPABASE_SERVICE_TOKEN: ${SUPABASE_SERVICE_TOKEN:-}
  SELF_HOSTED_WEBHOOK_URL: ${SELF_HOSTED_WEBHOOK_URL:-}
  SERPER_API_KEY: ${SERPER_API_KEY:-}
  SEARCHAPI_API_KEY: ${SEARCHAPI_API_KEY:-}
  LOGGING_LEVEL: ${LOGGING_LEVEL:-info}
  PROXY_SERVER: ${PROXY_SERVER:-}
  PROXY_USERNAME: ${PROXY_USERNAME:-}
  PROXY_PASSWORD: ${PROXY_PASSWORD:-}
  SEARXNG_ENDPOINT: ${SEARXNG_ENDPOINT:-}
  SEARXNG_ENGINES: ${SEARXNG_ENGINES:-}
  SEARXNG_CATEGORIES: ${SEARXNG_CATEGORIES:-}

services:
  playwright-service:
    <<: *defaults
    image: ${GHCR_IO_REGISTRY:-ghcr.io}/firecrawl/playwright-service:${PLAYWRIGHT_VERSION:-latest}
    environment:
      PORT: 3000
      PROXY_SERVER: ${PROXY_SERVER:-}
      PROXY_USERNAME: ${PROXY_USERNAME:-}
      PROXY_PASSWORD: ${PROXY_PASSWORD:-}
      BLOCK_MEDIA: ${BLOCK_MEDIA:-true}
    deploy:
      resources:
        limits:
          cpus: ${PLAYWRIGHT_CPU_LIMIT:-1.0}
          memory: ${PLAYWRIGHT_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${PLAYWRIGHT_CPU_RESERVATION:-0.5}
          memory: ${PLAYWRIGHT_MEMORY_RESERVATION:-512M}

  api:
    <<: *defaults
    image: ${GHCR_IO_REGISTRY:-ghcr.io}/firecrawl/firecrawl:${FIRECRAWL_VERSION:-latest}
    environment:
      <<: *common-env
      HOST: 0.0.0.0
      PORT: ${INTERNAL_PORT:-3002}
      EXTRACT_WORKER_PORT: ${EXTRACT_WORKER_PORT:-3004}
      WORKER_PORT: ${WORKER_PORT:-3005}
      ENV: local
    depends_on:
      redis:
        condition: service_healthy
      playwright-service:
        condition: service_started
      nuq-postgres:
        condition: service_started
    ports:
      - '${FIRECRAWL_PORT_OVERRIDE:-3002}:${INTERNAL_PORT:-3002}'
    command: node dist/src/harness.js --start-docker
    deploy:
      resources:
        limits:
          cpus: ${FIRECRAWL_API_CPU_LIMIT:-2.0}
          memory: ${FIRECRAWL_API_MEMORY_LIMIT:-4G}
        reservations:
          cpus: ${FIRECRAWL_API_CPU_RESERVATION:-1.0}
          memory: ${FIRECRAWL_API_MEMORY_RESERVATION:-2G}
    healthcheck:
      test:
        - CMD
        - wget
        - --no-verbose
        - --tries=1
        - --spider
        - 'http://localhost:3002/health'

      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  redis:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-alpine}
    command: redis-server --bind 0.0.0.0
    volumes:
      - redis_data:/data
    deploy:
      resources:
        limits:
          cpus: ${REDIS_CPU_LIMIT:-0.5}
          memory: ${REDIS_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${REDIS_CPU_RESERVATION:-0.25}
          memory: ${REDIS_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

  nuq-postgres:
    <<: *defaults
    image: ${GHCR_IO_REGISTRY:-ghcr.io}/firecrawl/nuq-postgres:${NUQ_POSTGRES_VERSION:-latest}
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-postgres}
    ports:
      - '${POSTGRES_PORT_OVERRIDE:-5432}:5432'
    volumes:
      - postgres_data:/var/lib/postgresql/data
    deploy:
      resources:
        limits:
          cpus: ${NUQPOSTGRES_CPU_LIMIT:-1.0}
          memory: ${NUQPOSTGRES_MEMORY_LIMIT:-1G}
        reservations:
          cpus: ${NUQPOSTGRES_CPU_RESERVATION:-0.5}
          memory: ${NUQPOSTGRES_MEMORY_RESERVATION:-512M}
    healthcheck:
      test: [CMD-SHELL, 'pg_isready -U ${POSTGRES_USER:-postgres}']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

volumes:
  redis_data:
  postgres_data:
