name: relaybox
version: '3.8'

x-db-config: &db-config
  DB_HOST: db
  DB_NAME: &db-name ${DB_NAME:-relaybox_core_platform}
  DB_USER: &db-user ${DB_USER:-relaybox_user}
  DB_PASSWORD: &db-password ${DB_PASSWORD:-password}
  DB_PORT: 5432
  DB_TLS_DISABLED: ${DB_TLS_DISABLED:-true}
  DB_PROXY_ENABLED: ${DB_PROXY_ENABLED:-false}
  DB_MAX_CONNECTIONS: ${DB_MAX_CONNECTIONS:-30}
  DB_IDLE_TIMEOUT_MS: ${DB_IDLE_TIMEOUT_MS:-30000}

x-redis-config: &redis-config
  REDIS_HOST: cache
  REDIS_PORT: 6379
  REDIS_TLS_DISABLED: ${REDIS_TLS_DISABLED:-true}

x-rabbitmq-config: &rabbitmq-config
  RABBIT_MQ_CONNECTION_STRING: amqp://guest:guest@broker:5672
  RABBIT_MQ_QUEUE_AUTO_DELETE: true
  RABBIT_MQ_QUEUE_COUNT: 20

services:
  proxy:
    image: nginx:latest
    depends_on:
      - core
    ports:
      - ${PROXY_PORT:-9000}:8080
    volumes:
      - ./services/proxy/nginx.conf:/etc/nginx/nginx.conf
    networks:
      - platform_network

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: *db-name
      POSTGRES_USER: *db-user
      POSTGRES_PASSWORD: *db-password
    ports:
      - ${DB_PORT:-9001}:5432
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - platform_network
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -h localhost -p 5432 -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
      interval: 30s
      timeout: 10s
      retries: 5

  cache:
    image: redis:7-alpine
    environment:
      - REDIS_PASSWORD=${REDIS_PASSWORD:-password}
      - REDIS_DATABASES=${REDIS_DATABASES:-1}
    volumes:
      - cache_data:/root/redis
      - ./services/cache/conf:/usr/local/etc/redis
    networks:
      - platform_network
    healthcheck:
      test: ['CMD', 'redis-cli', 'ping']
      interval: 30s
      timeout: 10s
      retries: 5

  broker:
    image: rabbitmq:3-management-alpine
    volumes:
      - broker_data:/var/lib/rabbitmq/
      - broker_log:/var/log/rabbitmq/
    networks:
      - platform_network
    healthcheck:
      test: ['CMD-SHELL', 'rabbitmq-diagnostics -q ping']
      interval: 30s
      timeout: 10s
      retries: 5

  core:
    image: relaybox/core:2.16.0
    depends_on:
      cache:
        condition: service_healthy
      broker:
        condition: service_healthy
      db:
        condition: service_healthy
    environment:
      <<: [*db-config, *redis-config, *rabbitmq-config]
      RELAYBOX_AUTH_SERVICE_URL: http://auth:4005
      WS_IDLE_TIMEOUT_MS: ${WS_IDLE_TIMEOUT_MS:-30000}
      LOG_LEVEL: ${LOG_LEVEL:-info}
    networks:
      - platform_network

  auth:
    image: relaybox/auth:3.11.0
    depends_on:
      cache:
        condition: service_healthy
      db:
        condition: service_healthy
    environment:
      <<: [*db-config, *redis-config]
      EMULATOR: true
      AUTH_SERVICE_URL: ${AUTH_SERVICE_URL:-http://localhost:${PROXY_PORT:-9000}/auth/dev}
      AUTO_VERIFY: ${AUTO_VERIFY:-true}
      JWT_ISSUER: ${JWT_ISSUER:-https://relaybox.net}
      LOG_LEVEL: ${LOG_LEVEL:-info}
      SMTP_HOST: ${SMTP_HOST:-''}
      SMTP_USERNAME: ${SMTP_USERNAME:-''}
      SMTP_PASSWORD: ${SMTP_PASSWORD:-''}
      SMTP_AUTH_EMAIL: ${SMTP_AUTH_EMAIL:-''}
    networks:
      - platform_network

  session:
    image: relaybox/session:2.11.0
    depends_on:
      cache:
        condition: service_healthy
      broker:
        condition: service_healthy
      db:
        condition: service_healthy
    environment:
      <<: [*db-config, *redis-config, *rabbitmq-config]
      WS_IDLE_TIMEOUT_MS: ${WS_IDLE_TIMEOUT_MS:-30000}
      LOG_LEVEL: ${LOG_LEVEL:-info}
      CRON_SCHEDULE_MINS: 5
    networks:
      - platform_network

  presence:
    image: relaybox/presence:2.6.0
    depends_on:
      cache:
        condition: service_healthy
      broker:
        condition: service_healthy
      db:
        condition: service_healthy
    environment:
      <<: [*db-config, *redis-config, *rabbitmq-config]
      LOG_LEVEL: ${LOG_LEVEL:-info}
    networks:
      - platform_network

  metrics:
    image: relaybox/metrics:2.2.0
    depends_on:
      cache:
        condition: service_healthy
      broker:
        condition: service_healthy
      db:
        condition: service_healthy
    environment:
      <<: [*db-config, *redis-config, *rabbitmq-config]
      LOG_LEVEL: ${LOG_LEVEL:-info}
    networks:
      - platform_network

  history:
    image: relaybox/history:2.3.3
    depends_on:
      cache:
        condition: service_healthy
      broker:
        condition: service_healthy
      db:
        condition: service_healthy
    environment:
      <<: [*db-config, *redis-config, *rabbitmq-config]
      LOG_LEVEL: ${LOG_LEVEL:-info}
      BATCH_PREFETCH_COUNT: 20
      BATCH_SIZE: 10
      BATCH_TIMEOUT_MS: 5000

    networks:
      - platform_network

  webhook:
    image: relaybox/webhook:1.0.7
    depends_on:
      cache:
        condition: service_healthy
      db:
        condition: service_healthy
    environment:
      <<: [*db-config, *redis-config]
      LOG_LEVEL: ${LOG_LEVEL:-info}
      LOG_STREAM_MAX_LEN: 1000
      LOG_STREAM_BUFFER_MAX_LENGTH: 100
    networks:
      - platform_network

networks:
  platform_network:
    driver: bridge

volumes:
  db_data:
    driver: local
  cache_data:
    driver: local
  broker_data:
    driver: local
  broker_log:
    driver: local
