# Compose v2 - NATS stack

x-common-env: &common-env
  env_file:
    - ../env/.env.runtime

volumes:
  nats_data: {}

networks:
  sendico-net:
    external: true
    name: sendico-net

services:
  sendico_nats:
    <<: *common-env
    build:
      context: .
      dockerfile: nats.dockerfile
    container_name: sendico-nats
    restart: unless-stopped
    command:
      - --jetstream
      - --http_port=${NATS_MONITORING_PORT}
      - --server_name=sendico-nats
      - --user=${NATS_USER}
      - --pass=${NATS_PASSWORD}
      - --port=${NATS_PORT}
      - --store_dir=/data
    environment:
      NATS_SERVER_OPTS: --jetstream
    volumes:
      - nats_data:/data
    ports:
      - "0.0.0.0:${NATS_PORT}:${NATS_PORT}"
      - "0.0.0.0:${NATS_MONITORING_PORT}:${NATS_MONITORING_PORT}"
    healthcheck:
      test: wget http://localhost:${NATS_MONITORING_PORT}/healthz -q -S -O -
      start_period: 3s
      retries: 3
      timeout: 3s
      interval: 14s
    networks:
      - sendico-net

  sendico_nats_exporter:
    <<: *common-env
    image: natsio/prometheus-nats-exporter:latest
    container_name: sendico-nats-exporter
    restart: unless-stopped
    depends_on:
      sendico_nats:
        condition: service_healthy
    environment:
      NATS_URL: http://sendico-nats:${NATS_MONITORING_PORT}
    ports:
      - "0.0.0.0:${NATS_PROMETHEUS_PORT}:${NATS_PROMETHEUS_PORT}"
    command:
      - -varz
      - -connz
      - -routez
      - -subz
      - -leafz
      - -gatewayz
      - -healthz
      - -accstatz
      - -jsz=all
      - -addr=0.0.0.0
      - -port=${NATS_PROMETHEUS_PORT}
      - http://sendico-nats:${NATS_MONITORING_PORT}
    networks:
      - sendico-net
