networks:
  cl-mojaloop-net:
    name: cl-mojaloop-net


# @see https://uninterrupted.tech/blog/hassle-free-redis-cluster-deployment-using-docker/
x-redis-node: &REDIS_NODE
  image: docker.io/bitnamilegacy/redis-cluster:7.4.2
  environment: &REDIS_ENVS
    ALLOW_EMPTY_PASSWORD: yes
    REDIS_NODES: redis-node-0:6379 redis-node-1:6380 redis-node-2:6381 redis-node-3:6382 redis-node-4:6383 redis-node-5:6384
    REDIS_CLUSTER_ANNOUNCE_HOSTNAME: localhost
    REDIS_CLUSTER_PREFERRED_ENDPOINT_TYPE: hostname
  healthcheck:
    test: [ "CMD", "redis-cli", "ping" ]
    timeout: 2s

services:
  central-ledger:
    image: mojaloop/central-ledger:local
    deploy:
      replicas: 1
    user: root
    build:
      context: .
      cache_from:
        - mojaloop/central-ledger
        - central-ledger
    container_name: cl_central-ledger
    command:
      - "sh"
      - "-c"
      - "sh /opt/wait-for/wait-for-central-ledger.sh && node src/api/index.js"
    ports:
      - "3001:3001"
    volumes:
       - ./docker/central-ledger/default.json:/opt/app/config/default.json
       - ./docker/wait-for:/opt/wait-for
    environment:
      - LOG_LEVEL=verbose
#      - CSL_LOG_TRANSPORT=file
      - CLEDG_MONGODB__DISABLED=false
    networks:
      - cl-mojaloop-net
    extra_hosts:
      - "redis-node-0:host-gateway"
    depends_on:
      - mysql
      - kafka
      - objstore
      - redis-node-0
      # - redis
    healthcheck:
      test: ["CMD", "sh", "-c" ,"apk --no-cache add curl", "&&", "curl", "http://localhost:3001/health"]
      timeout: 20s
      retries: 10
      interval: 30s

  ml-api-adapter:
    image: mojaloop/ml-api-adapter:latest
    container_name: cl_ml-api-adapter
    deploy:
      replicas: 1
    user: root
    command:
      - "sh"
      - "-c"
      - "sh /opt/wait-for/wait-for-ml-api-adapter.sh && node src/api/index.js"
    ports:
      - "3000:3000"
    volumes:
      - ./docker/ml-api-adapter/default.json:/opt/app/config/default.json
      - ./docker/wait-for:/opt/wait-for
    environment:
      - LOG_LEVEL=info
    extra_hosts:
      - "host.docker.internal:host-gateway"
      - "simulator:host-gateway"
    networks:
      - cl-mojaloop-net
    depends_on:
      - kafka
    healthcheck:
      test: ["CMD", "sh", "-c" ,"apk --no-cache add curl", "&&", "curl", "http://localhost:3000/health"]
      timeout: 20s
      retries: 10
      interval: 30s

  mysql:
    image: mysql/mysql-server:8.0
    container_name: cl_mysql
    deploy:
      replicas: 1
    # Disable logging as it is far too verbose for debugging locally
    logging:
      driver: none
    ports:
      - "3306:3306"
    volumes:
      # Note: this fixes the permissions issue, but docker-compose up will fail on first attempt
      - ./docker/sql-init/:/docker-entrypoint-initdb.d/
    environment:
      - MYSQL_USER=${DBUSER:-central_ledger}
      - MYSQL_PASSWORD=${DBPASS:-password}
      - MYSQL_DATABASE=${DBUSER:-central_ledger}
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
    networks:
      - cl-mojaloop-net
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "mysql"]
      timeout: 20s
      retries: 10
      start_period: 40s
      interval: 30s

  #  To use with proxyCache.type === 'redis-cluster'
  redis-node-0:
    container_name: redis-node-0
    <<: *REDIS_NODE
    ports:
      - 6379:6379
    environment:
      <<: *REDIS_ENVS
      REDIS_PORT_NUMBER: 6379
      REDIS_CLUSTER_CREATOR: yes
    depends_on:
      - redis-node-1
      - redis-node-2
      - redis-node-3
      - redis-node-4
      - redis-node-5
  redis-node-1:
    container_name: redis-node-1
    <<: *REDIS_NODE
    ports:
      - 6380:6380
    environment:
      <<: *REDIS_ENVS
      REDIS_PORT_NUMBER: 6380
  redis-node-2:
    container_name: redis-node-2
    <<: *REDIS_NODE
    ports:
      - 6381:6381
    environment:
      <<: *REDIS_ENVS
      REDIS_PORT_NUMBER: 6381
  redis-node-3:
    container_name: redis-node-3
    <<: *REDIS_NODE
    ports:
      - 6382:6382
    environment:
      <<: *REDIS_ENVS
      REDIS_PORT_NUMBER: 6382
  redis-node-4:
    container_name: redis-node-4
    <<: *REDIS_NODE
    ports:
      - 6383:6383
    environment:
      <<: *REDIS_ENVS
      REDIS_PORT_NUMBER: 6383
  redis-node-5:
    container_name: redis-node-5
    <<: *REDIS_NODE
    ports:
      - 6384:6384
    environment:
      <<: *REDIS_ENVS
      REDIS_PORT_NUMBER: 6384

  ##  To use with proxyCache.type === 'redis'
  #  redis:
  #    image: redis:6.2.4-alpine
  #    restart: "unless-stopped"
  #    environment:
  #      - ALLOW_EMPTY_PASSWORD=yes
  #      - REDIS_PORT=6379
  #      - REDIS_REPLICATION_MODE=master
  #    ports:
  #      - "${REDIS_STANDALONE_PORT}:6379"

  mockserver:
    image: jamesdbloom/mockserver
    container_name: mockserver
    deploy:
      replicas: 1
    ports:
      - "1080:1080"
    networks:
      - cl-mojaloop-net

  temp_curl:
    image: byrnedo/alpine-curl
    container_name: cl_temp_curl
    links:
      - mockserver
    volumes:
      - ./docker/wait-for:/opt/wait-for
    entrypoint: [ "sh", "-c" ]
    command:
      - /opt/wait-for/wait-for-mockserver.sh
    environment: []
    networks:
      - cl-mojaloop-net
    depends_on:
      - mockserver

  kafka:
    image: docker.io/bitnamilegacy/kafka:3.5
    container_name: cl_kafka
    networks:
      - cl-mojaloop-net
    ports:
      - "9092:9092"
    environment:
      # BITNAMI_DEBUG: "yes"
      ALLOW_PLAINTEXT_LISTENER: "yes"
      KAFKA_ADVERTISED_HOST_NAME: kafka
      KAFKA_CFG_LISTENERS: CONTROLLER://:9093,LISTENER_DOCKER://:29092,LISTENER_EXTERN://:9092
      KAFKA_CFG_ADVERTISED_LISTENERS: LISTENER_DOCKER://kafka:29092,LISTENER_EXTERN://localhost:9092
      KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,LISTENER_DOCKER:PLAINTEXT,LISTENER_EXTERN:PLAINTEXT
      KAFKA_CFG_INTER_BROKER_LISTENER_NAME: LISTENER_DOCKER
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_CFG_MESSAGE_MAX_BYTES: 200000000
      KAFKA_CFG_NODE_ID: 1
      KAFKA_CFG_PROCESS_ROLES: broker,controller
      KAFKA_CFG_CONTROLLER_LISTENER_NAMES: CONTROLLER
      KAFKA_CFG_CONTROLLER_QUORUM_VOTERS: 1@127.0.0.1:9093
      KAFKA_AUTO_CREATE_TOPICS_ENABLE: "true"
      KAFKA_ENABLE_KRAFT: "true"
    healthcheck:
      test: ["CMD" ,"/opt/bitnami/kafka/bin/kafka-broker-api-versions.sh","--bootstrap-server","kafka:29092"]
      timeout: 20s
      retries: 10
      start_period: 40s
      interval: 30s

  init-kafka:
    container_name: cl_init_kafka
    networks:
      - cl-mojaloop-net
    image: docker.io/bitnamilegacy/kafka:3.5
    depends_on:
      - kafka
    volumes:
      - ./docker/kafka/scripts:/tmp/kafka/scripts
    command: bash /tmp/kafka/scripts/provision.sh

  objstore:
    image: mongo:latest
    container_name: cl_objstore
    deploy:
      replicas: 1
    # Disable logging as it is far too verbose for debugging locally
    logging:
      driver: none
    ports:
      - "27017:27017"
    networks:
      - cl-mojaloop-net
    healthcheck:
      test: mongo --eval 'db.runCommand("ping").ok' localhost:27017/test --quiet
      interval: 10s
      timeout: 10s
      retries: 5
      start_period: 40s

  simulator:
    image: mojaloop/simulator:latest
    container_name: cl_simulator
    user: root
    deploy:
      replicas: 1
    ports:
      - "8444:8444"
    extra_hosts:
      - "host.docker.internal:host-gateway"
      - "simulator:host-gateway"
    environment:
      - LOG_LEVEL=info
      - TRANSFERS_ENDPOINT=http://host.docker.internal:3000
      - QUOTES_ENDPOINT=http://host.docker.internal:3002
      - PARTIES_ENDPOINT=http://host.docker.internal:4002
      - TRANSFERS_FULFIL_RESPONSE_DISABLED=false
      - TRANSFERS_CONDITION=HOr22-H3AfTDHrSkPjJtVPRdKouuMkDXTR4ejlQa8Ks
      - TRANSFERS_FULFILMENT=XoSz1cL0tljJSCp_VtIYmPNw-zFUgGfbUqf69AagUzY
    networks:
      - cl-mojaloop-net
    healthcheck:
      test: ["CMD", "sh", "-c" ,"apk --no-cache add curl", ";", "curl", "http://localhost:8444/health"]
      timeout: 20s
      retries: 10
      interval: 30s

  ## Debug utilities
  kafka-debug-console:
    # image: quay.io/cloudhut/kowl:v1.4.0
    image: docker.redpanda.com/redpandadata/console:latest
    deploy:
      replicas: 1
    restart: on-failure
    hostname: kafka-debug-console
    ports:
    - "9080:8080"
    networks:
      - cl-mojaloop-net
    environment:
      - KAFKA_BROKERS=kafka:29092
