version: "3.8"

services:

  db:
    image: mariadb:10.6
    container_name: vs-db
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: easystream
      MYSQL_USER: easystream
      MYSQL_PASSWORD: easystream
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql
      - ./__install/easystream.sql:/docker-entrypoint-initdb.d/1-main_schema.sql:ro
      - ./__install/add_advanced_features.sql:/docker-entrypoint-initdb.d/2-advanced_features.sql:ro
      - ./deploy/init_settings.sql:/docker-entrypoint-initdb.d/3-init_settings.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u root -proot --silent || exit 1"]
      start_period: 120s
      interval: 10s
      timeout: 5s
      retries: 30

  php:
    build:
      context: .
      dockerfile: Dockerfile.php
    container_name: vs-php
    working_dir: /srv/easystream
    environment:
      TZ: UTC
      DB_HOST: db
      DB_NAME: easystream
      DB_USER: easystream
      DB_PASS: easystream
      PREFLIGHT_ENABLE: "1"
      MAIN_URL: http://localhost:8083
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_DB: 0
    volumes:
      - ./:/srv/easystream
      - rtmp_hls:/var/www/hls:ro
      - rtmp_rec:/mnt/rec:ro



  caddy:
    image: caddy:2-alpine
    container_name: vs-caddy
    depends_on:
      db:
        condition: service_healthy
      php:
        condition: service_started
      srs:
        condition: service_started
    ports:
      - "8083:80"
      - "8443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - ./:/srv/easystream:ro
      - rtmp_hls:/var/www/hls:ro
      - caddy_data:/data
      - caddy_config:/config

  srs:
    image: ossrs/srs:5
    container_name: vs-srs
    ports:
      - "1935:1935"   # RTMP ingest
    volumes:
      - ./deploy/srs.conf:/usr/local/srs/conf/srs.conf:ro
      - rtmp_hls:/srs/hls
      - rtmp_rec:/srs/rec
    command: ["/usr/local/srs/objs/srs", "-c", "/usr/local/srs/conf/srs.conf"]
    restart: unless-stopped

  cron:
    build:
      context: .
      dockerfile: Dockerfile.cron
    container_name: vs-cron
    depends_on:
      php:
        condition: service_started
    environment:
      TZ: UTC
      DB_HOST: db
      DB_NAME: easystream
      DB_USER: easystream
      DB_PASS: easystream
      CRON_BASE_URL: http://localhost:8083
      CRON_SSK: IOpeCV5jyJ6SIbg2ySIB5os5RX3a1a9LIjyJmGfShcPovdx43GmBHgsJTXbo71CD4df4oFKikCZxJS4f
      VOD_REC_PATH: /mnt/rec
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_DB: 0
    volumes:
      - ./:/srv/easystream
      - rtmp_rec:/mnt/rec:ro
  redis:
    image: redis:7-alpine
    container_name: vs-redis
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  queue-worker:
    build:
      context: .
      dockerfile: Dockerfile.php
    container_name: vs-queue-worker
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      TZ: UTC
      DB_HOST: db
      DB_NAME: easystream
      DB_USER: easystream
      DB_PASS: easystream
      REDIS_HOST: redis
      REDIS_PORT: 6379
      WORKER_QUEUES: "default,video,email,notifications"
      WORKER_SLEEP: 3
      WORKER_TIMEOUT: 300
    volumes:
      - ./:/srv/easystream
      - rtmp_hls:/var/www/hls:ro
      - rtmp_rec:/mnt/rec:ro
    command: php f_scripts/queue_worker.php
    restart: unless-stopped

  abr:
    image: jrottenberg/ffmpeg:5.1-ubuntu
    container_name: vs-abr
    entrypoint: ["/bin/bash"]
    command: ["/abr.sh"]
    depends_on:
      srs:
        condition: service_started
    environment:
      ABR_STREAM_KEY: testkey
    volumes:
      - rtmp_hls:/var/www/hls
      - ./deploy/abr.sh:/abr.sh:ro
    restart: unless-stopped

volumes:
  db_data:
  redis_data:
  rtmp_hls:
  rtmp_rec:
  caddy_data:
  caddy_config:
