x-transcoder: &transcoder-base
  build: ./transcoder
  networks:
    default:
      aliases:
        - transcoder
  restart: on-failure
  env_file:
    - ./.env
  environment:
    - GOCODER_PREFIX=/video
  volumes:
    - ${LIBRARY_ROOT}:/video:ro
    - ${CACHE_ROOT}:/cache
    - metadata:/metadata

services:
  back:
    build: ./back
    restart: on-failure
    environment:
      - TRANSCODER_URL=${TRANSCODER_URL:-http://transcoder:7666}
    env_file:
      - ./.env
    depends_on:
      postgres:
        condition: service_healthy
      meilisearch:
        condition: service_healthy
      rabbitmq:
        condition: service_healthy
      migrations:
        condition: service_completed_successfully
    volumes:
      - kyoo:/metadata
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.rule=PathPrefix(`/api/`)"
      - "traefik.http.routers.api.middlewares=api-sp"
      - "traefik.http.middlewares.api-sp.stripprefix.prefixes=/api"
      - "traefik.http.middlewares.api-sp.stripprefix.forceSlash=false"

  migrations:
    build:
      context: ./back
      dockerfile: Dockerfile.migrations
    restart: "no"
    depends_on:
      postgres:
        condition: service_healthy
    env_file:
      - ./.env

  front:
    build: ./front
    restart: on-failure
    environment:
      - KYOO_URL=${KYOO_URL:-http://back:5000}
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.front.rule=PathPrefix(`/`)"

  scanner:
    build: ./scanner
    restart: on-failure
    depends_on:
      back:
        condition: service_healthy
    env_file:
      - ./.env
    environment:
      - KYOO_URL=${KYOO_URL:-http://back:5000}
    volumes:
      - ${LIBRARY_ROOT}:/video:ro

  matcher:
    build: ./scanner
    command: matcher
    restart: on-failure
    depends_on:
      back:
        condition: service_healthy
    env_file:
      - ./.env
    environment:
      - KYOO_URL=${KYOO_URL:-http://back:5000}

  autosync:
    build: ./autosync
    restart: on-failure
    depends_on:
      rabbitmq:
        condition: service_healthy
    env_file:
      - ./.env

  transcoder:
    <<: *transcoder-base
    profiles: ['', 'cpu']

  transcoder-nvidia:
    <<: *transcoder-base
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [gpu]
    environment:
      - GOCODER_PREFIX=/video
      - GOCODER_HWACCEL=nvidia
    profiles: ['nvidia']

  transcoder-vaapi:
    <<: *transcoder-base
    devices:
      - /dev/dri:/dev/dri
    environment:
      - GOCODER_PREFIX=/video
      - GOCODER_HWACCEL=vaapi
      - GOCODER_VAAPI_RENDERER=${GOCODER_VAAPI_RENDERER:-/dev/dri/renderD128}
    profiles: ['vaapi']
  # qsv is the same setup as vaapi but with the hwaccel env var different
  transcoder-qsv:
    <<: *transcoder-base
    devices:
      - /dev/dri:/dev/dri
    environment:
      - GOCODER_PREFIX=/video
      - GOCODER_HWACCEL=qsv
      - GOCODER_VAAPI_RENDERER=${GOCODER_VAAPI_RENDERER:-/dev/dri/renderD128}
    profiles: ['qsv']

  traefik:
    image: traefik:v3.0
    restart: on-failure
    command:
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entryPoints.web.address=:8901"
      - "--accesslog=true"
    ports:
      - "8901:8901"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  postgres:
    image: postgres:15
    restart: on-failure
    env_file:
      - ./.env
    volumes:
      - db:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 5

  meilisearch:
    image: getmeili/meilisearch:v1.4
    restart: on-failure
    volumes:
      - search:/meili_data
    environment:
      - MEILI_ENV=production
    env_file:
      - .env
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--spider", "http://meilisearch:7700/health"]
      interval: 30s
      timeout: 5s
      retries: 5

  rabbitmq:
    image: rabbitmq:3-alpine
    restart: on-failure
    environment:
      - RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER}
      - RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS}
    healthcheck:
      test: rabbitmq-diagnostics -q ping
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s

volumes:
  kyoo:
  db:
  metadata:
  search:
