# Apache Pulsar Stack - Smart City Digital Twin Martinique
# Includes: Pulsar Standalone + Pulsar Manager
# Pulsar Admin: https://pulsar.digitribe.fr/admin
# Pulsar Manager: https://pulsar.digitribe.fr
version: '3.8'

services:
  # Pulsar Standalone
  pulsar:
    image: apachepulsar/pulsar:3.2.0
    container_name: smart-city-pulsar
    restart: unless-stopped
    user: "10000:0"
    ports:
      - "6650:6650"
    environment:
      PULSAR_MEM: "-Xms512m -Xmx512m -XX:MaxDirectMemorySize=512m"
      PULSAR_STANDALONE_USE_ZOOKEEPER: "true"
    volumes:
      - pulsar-data:/pulsar/data
    networks:
      - traefik-public
      - smartcity-shared
    command: ["/pulsar/bin/pulsar", "standalone"]
    # healthcheck désactivé car web service 8080 instable
    # healthcheck:
    #   test: ["CMD-SHELL", "curl -sf http://localhost:8080/admin/v2/clusters || exit 1"]
    #   interval: 30s
    #   timeout: 10s
    #   retries: 10
    #   start_period: 60s
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.pulsar-admin.rule=Host(`pulsar.digitribe.fr`) && PathPrefix(`/admin`, `/ws`, `/lookup`)"
      - "traefik.http.routers.pulsar-admin.entrypoints=websecure"
      - "traefik.http.routers.pulsar-admin.tls=true"
      - "traefik.http.services.pulsar-admin.loadbalancer.server.port=8080"

  # Pulsar Manager - Web UI
  pulsar-manager:
    image: apachepulsar/pulsar-manager:v0.4.0
    container_name: smart-city-pulsar-manager
    restart: unless-stopped
    depends_on:
      pulsar:
        condition: service_healthy
    environment:
      - URL=jdbc:postgresql://127.0.0.1:5432/pulsar_manager
      - POSTGRES_PASSWORD=Digitribe972
    networks:
      - traefik-public
      - smartcity-shared
    ports:
      - "7750:7750"
    healthcheck:
      test: ["CMD-SHELL", "curl -sf http://localhost:7750 || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 120s
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.pulsar-manager.rule=Host(`pulsar.digitribe.fr`)"
      - "traefik.http.routers.pulsar-manager.entrypoints=web"
      - "traefik.http.services.pulsar-manager.loadbalancer.server.port=7750"

  # Pulsar Distribution Service - Consumer → Republish to Brokers
  pulsar-distribution:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: smart-city-pulsar-distribution
    restart: unless-stopped
    depends_on:
      - pulsar
    environment:
      - PULSAR_HOST=smart-city-pulsar
      - PULSAR_PORT=6650
      - EMQX_HOST=emqx_emqx_1
      - EMQX_PORT=1883
      - MOSQUITTO_HOST=mainfluxlabs-mosquitto
      - MOSQUITTO_PORT=1883
      - ORION_URL=http://fiware-gis-quickstart-orion-1:1026
      - STELLIO_URL=http://stellio-api-gateway:8080
      - FROST_URL=http://frost-api-8090:8080/FROST-Server/v1.1
    networks:
      - smartcity-shared
    healthcheck:
      test: ["CMD-SHELL", "ps aux | grep -q distribution || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s

networks:
  traefik-public:
    external: true
  smartcity-shared:
    external: true

volumes:
  pulsar-data:
  pulsar-manager-data:
