services:
  caddy:
    image: caddy:2
    ports:
      - '8088:8088'
    volumes:
      - ./Caddyfile.dev:/etc/caddy/Caddyfile:ro
      - ../fluxer_app/dist:/app/dist:ro
    networks:
      - fluxer-shared
    extra_hosts:
      - 'host.docker.internal:host-gateway'
    restart: on-failure

  cloudflared:
    image: cloudflare/cloudflared:latest
    command: tunnel --no-autoupdate run --token ${CLOUDFLARE_TUNNEL_TOKEN}
    env_file:
      - ./.env
    networks:
      - fluxer-shared
    restart: on-failure

  api:
    image: node:24-bookworm-slim
    working_dir: /workspace
    command: bash -lc "corepack enable pnpm && CI=true pnpm install && npx tsx watch --clear-screen=false src/App.ts"
    env_file:
      - ./.env
    environment:
      - CI=true
      - VAPID_PUBLIC_KEY=BJHAPp7Xg4oeN_D6-EVu0D-bDyPDwFFJiLn7CzkUjUvaG_F-keQGpA_-RiNugCosTPhhdvdrn4mEOh-_1Bt35V8
      - FLUXER_METRICS_HOST=metrics:8080
    volumes:
      - ../fluxer_api:/workspace
      - api_node_modules:/workspace/node_modules
    networks:
      - fluxer-shared
    restart: on-failure

  worker:
    image: node:24-bookworm-slim
    working_dir: /workspace
    command: bash -lc "corepack enable pnpm && CI=true pnpm install && npm run dev:worker"
    env_file:
      - ./.env
    environment:
      - CI=true
      - FLUXER_METRICS_HOST=metrics:8080
    volumes:
      - ../fluxer_api:/workspace
      - api_node_modules:/workspace/node_modules
    networks:
      - fluxer-shared
    restart: on-failure
    depends_on:
      - postgres
      - redis
      - cassandra

  media:
    build:
      context: ../fluxer_media_proxy
      dockerfile: Dockerfile
      target: build
    working_dir: /workspace
    command: >
      bash -lc "
        corepack enable pnpm &&
        CI=true pnpm install &&
        pnpm dev
      "
    user: root
    env_file:
      - ./.env
    environment:
      - CI=true
      - NODE_ENV=development
      - FLUXER_METRICS_HOST=metrics:8080
    volumes:
      - ../fluxer_media_proxy:/workspace
      - media_node_modules:/workspace/node_modules
    networks:
      - fluxer-shared
    restart: on-failure

  admin:
    build:
      context: ../fluxer_admin
      dockerfile: Dockerfile.dev
    working_dir: /workspace
    env_file:
      - ./.env
    environment:
      - PORT=8080
      - APP_MODE=admin
      - FLUXER_METRICS_HOST=metrics:8080
    volumes:
      - admin_build:/workspace/build
    networks:
      - fluxer-shared
    restart: on-failure
    develop:
      watch:
        - action: rebuild
          path: ../fluxer_admin/src
        - action: rebuild
          path: ../fluxer_admin/tailwind.css

  marketing:
    build:
      context: ../fluxer_marketing
      dockerfile: Dockerfile.dev
    working_dir: /workspace
    env_file:
      - ./.env
    environment:
      - PORT=8080
      - FLUXER_METRICS_HOST=metrics:8080
    volumes:
      - marketing_build:/workspace/build
    networks:
      - fluxer-shared
    restart: on-failure
    develop:
      watch:
        - action: rebuild
          path: ../fluxer_marketing/src
        - action: rebuild
          path: ../fluxer_marketing/tailwind.css

  docs:
    image: node:24-bookworm-slim
    working_dir: /workspace
    command: bash -lc "corepack enable pnpm && CI=true pnpm install && pnpm dev"
    env_file:
      - ./.env
    environment:
      - CI=true
      - NODE_ENV=development
    volumes:
      - ../fluxer_docs:/workspace
      - docs_node_modules:/workspace/node_modules
    networks:
      - fluxer-shared
    restart: on-failure

  gateway:
    image: erlang:28-slim
    working_dir: /workspace
    command: bash -c "apt-get update && apt-get install -y --no-install-recommends build-essential linux-libc-dev curl ca-certificates gettext-base git && curl -fsSL https://github.com/erlang/rebar3/releases/download/3.24.0/rebar3 -o /usr/local/bin/rebar3 && chmod +x /usr/local/bin/rebar3 && rebar3 compile && exec ./docker-entrypoint.sh"
    hostname: gateway
    env_file:
      - ./.env
    environment:
      - RELEASE_NODE=fluxer_gateway@gateway
      - LOGGER_LEVEL=debug
      - CLUSTER_NAME=fluxer_gateway
      - CLUSTER_DISCOVERY_DNS=gateway
      - NODE_COOKIE=fluxer_dev_cookie
      - VAPID_PUBLIC_KEY=BJHAPp7Xg4oeN_D6-EVu0D-bDyPDwFFJiLn7CzkUjUvaG_F-keQGpA_-RiNugCosTPhhdvdrn4mEOh-_1Bt35V8
      - VAPID_PRIVATE_KEY=Ze8J4aSmwV5B77zz9NzTU_IdyFyR1hMiKaYF2G61Y-E
      - VAPID_EMAIL=support@fluxer.app
      - FLUXER_METRICS_HOST=metrics:8080
    volumes:
      - ../fluxer_gateway:/workspace
      - gateway_build:/workspace/_build
    networks:
      - fluxer-shared
    restart: on-failure

  postgres:
    image: postgres:17
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: fluxer
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - fluxer-shared
    restart: on-failure

  cassandra:
    image: scylladb/scylla:latest
    command: --smp 1 --memory 512M --overprovisioned 1 --developer-mode 1 --api-address 0.0.0.0
    ports:
      - '9042:9042'
    volumes:
      - scylla_data:/var/lib/scylla
    networks:
      - fluxer-shared
    restart: on-failure
    healthcheck:
      test: ['CMD-SHELL', 'cqlsh -e "describe cluster"']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 90s

  redis:
    image: valkey/valkey:latest
    volumes:
      - redis_data:/data
    command: valkey-server --save 60 1 --loglevel warning
    networks:
      - fluxer-shared
    restart: on-failure

  minio:
    image: minio/minio
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    volumes:
      - minio_data:/data
    networks:
      - fluxer-shared
    restart: on-failure
    healthcheck:
      test: ['CMD', 'mc', 'ready', 'local']
      interval: 5s
      timeout: 5s
      retries: 5

  minio-setup:
    image: minio/mc
    depends_on:
      minio:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
      mc alias set minio http://minio:9000 minioadmin minioadmin;
      mc mb --ignore-existing minio/fluxer-metrics;
      mc mb --ignore-existing minio/fluxer-uploads;
      exit 0;
      "
    networks:
      - fluxer-shared
    restart: 'no'

  clamav:
    image: clamav/clamav:latest
    volumes:
      - clamav_data:/var/lib/clamav
    environment:
      CLAMAV_NO_FRESHCLAMD: 'false'
      CLAMAV_NO_CLAMD: 'false'
      CLAMAV_NO_MILTERD: 'true'
    networks:
      - fluxer-shared
    restart: on-failure
    healthcheck:
      test: ['CMD', '/usr/local/bin/clamdcheck.sh']
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 300s

  meilisearch:
    image: getmeili/meilisearch:v1.25.0
    volumes:
      - meilisearch_data:/meili_data
    environment:
      MEILI_ENV: development
      MEILI_MASTER_KEY: masterKey
    networks:
      - fluxer-shared
    restart: on-failure

  clickhouse:
    image: clickhouse/clickhouse-server:24.8
    hostname: clickhouse
    profiles:
      - clickhouse
    environment:
      - CLICKHOUSE_DB=fluxer_metrics
      - CLICKHOUSE_USER=fluxer
      - CLICKHOUSE_PASSWORD=fluxer_dev
      - CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1
    volumes:
      - clickhouse_data:/var/lib/clickhouse
      - clickhouse_logs:/var/log/clickhouse-server
    networks:
      - fluxer-shared
    ports:
      - '8123:8123'
      - '9000:9000'
    restart: on-failure
    healthcheck:
      test: ['CMD', 'clickhouse-client', '--query', 'SELECT 1']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s
    ulimits:
      nofile:
        soft: 262144
        hard: 262144

  metrics:
    build:
      context: ../fluxer_metrics
      dockerfile: Dockerfile
    env_file:
      - ./.env
    environment:
      - METRICS_PORT=8080
      - METRICS_MODE=${METRICS_MODE:-noop}
      - CLICKHOUSE_URL=http://clickhouse:8123
      - CLICKHOUSE_DATABASE=fluxer_metrics
      - CLICKHOUSE_USER=fluxer
      - CLICKHOUSE_PASSWORD=fluxer_dev
      - ANOMALY_DETECTION_ENABLED=true
      - FLUXER_ADMIN_ENDPOINT=${FLUXER_ADMIN_ENDPOINT:-}
    networks:
      - fluxer-shared
    restart: on-failure

  metrics-clickhouse:
    extends:
      service: metrics
    profiles:
      - clickhouse
    environment:
      - METRICS_MODE=clickhouse
    depends_on:
      clickhouse:
        condition: service_healthy

  cassandra-migrate:
    image: debian:bookworm-slim
    command:
      [
        'bash',
        '-lc',
        'apt-get update && apt-get install -y dnsutils && sleep 30 && /cassandra-migrate --host cassandra --username cassandra --password cassandra up',
      ]
    working_dir: /workspace
    volumes:
      - ../scripts/cassandra-migrate/target/release/cassandra-migrate:/cassandra-migrate
      - ../fluxer_devops/cassandra/migrations:/workspace/fluxer_devops/cassandra/migrations
    networks:
      - fluxer-shared
    depends_on:
      cassandra:
        condition: service_healthy
    restart: 'no'

  livekit:
    image: livekit/livekit-server:latest
    command: --config /etc/livekit.yaml --dev
    env_file:
      - ./.env
    volumes:
      - ./livekit.yaml:/etc/livekit.yaml:ro
    ports:
      - '7880:7880'
      - '7882:7882/udp'
      - '7999:7999/udp'
    networks:
      - fluxer-shared
    restart: on-failure

networks:
  fluxer-shared:
    name: fluxer-shared
    external: true

volumes:
  postgres_data:
  scylla_data:
  redis_data:
  minio_data:
  clamav_data:
  meilisearch_data:
  clickhouse_data:
  clickhouse_logs:
  api_node_modules:
  media_node_modules:
  admin_build:
  marketing_build:
  gateway_build:
  docs_node_modules:
