version: "3.8"
name: knightcrawler

x-restart: &restart-policy "unless-stopped"

x-basehealth: &base-health
  interval: 10s
  timeout: 10s
  retries: 3
  start_period: 10s

x-rabbithealth: &rabbitmq-health
  test: rabbitmq-diagnostics -q ping
  <<: *base-health

x-mongohealth: &mongodb-health
  test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
  <<: *base-health

x-postgreshealth: &postgresdb-health
  test: pg_isready
  <<: *base-health

x-apps: &knightcrawler-app
  depends_on:
    mongodb:
      condition: service_healthy
    postgres:
      condition: service_healthy
    rabbitmq:
      condition: service_healthy
  restart: *restart-policy

services:
  postgres:
    image: postgres:latest
    env_file:
      - .env
    environment:
      PGUSER: postgres # needed for healthcheck.
    # # If you need the database to be accessible from outside, please open the below port.
    # # Furthermore, please, please, please, change the username and password in the .env file.
    # # If you want to enhance your security even more, create a new user for the database with a strong password.
    # ports:
    #   - "5432:5432"
    volumes:
      - postgres:/var/lib/postgresql/data
    healthcheck: *postgresdb-health
    restart: *restart-policy
    networks:
      - knightcrawler-network

  mongodb:
    image: mongo:latest
    env_file:
      - .env
    # # If you need the database to be accessible from outside, please open the below port.
    # # Furthermore, please, please, please, change the username and password in the .env file.
    # ports:
    #   - "27017:27017"
    volumes:
      - mongo:/data/db
    restart: *restart-policy
    healthcheck: *mongodb-health
    networks:
      - knightcrawler-network

  rabbitmq:
    image: rabbitmq:3-management
    # # If you need the database to be accessible from outside, please open the below port.
    # # Furthermore, please, please, please, look at the documentation for rabbit on how to secure the service.
    # ports:
    #   - "5672:5672"
    #   - "15672:15672"
    #   - "15692:15692"
    volumes:
      - rabbitmq:/var/lib/rabbitmq
    hostname: ${RabbitMqConfiguration__Host}
    restart: *restart-policy
    healthcheck: *rabbitmq-health
    networks:
      - knightcrawler-network

  producer:
    image: gabisonfire/knightcrawler-producer:latest
    labels:
      logging: "promtail"
    env_file:
      - .env
    <<: *knightcrawler-app
    networks:
      - knightcrawler-network

  consumer:
    image: gabisonfire/knightcrawler-consumer:latest
    env_file:
      - .env
    labels:
      logging: "promtail"
    deploy:
      replicas: ${CONSUMER_REPLICAS}
    <<: *knightcrawler-app
    networks:
      - knightcrawler-network

  addon:
    image: gabisonfire/knightcrawler-addon:latest
    ports:
      - "7000:7000"
    labels:
      logging: "promtail"
    env_file:
      - .env
    <<: *knightcrawler-app
    networks:
      - knightcrawler-network

networks:
  knightcrawler-network:
    driver: bridge
    name: knightcrawler-network

volumes:
  postgres:
  mongo:
  rabbitmq:
