services:
  nginx:
    image: nginx:alpine
    container_name: nginx
    ports:
      - "4000:80"
    volumes:
      - ./backend/nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - authcore
      - companycore

  authcore:
    build:
      context: ./backend/authCore
    container_name: authcore
    expose:
      - "4000"
    env_file:
      - ./backend/authCore/.env
    depends_on:
      mongo:
        condition: service_healthy
      mongo-init:
        condition: service_completed_successfully

  companycore:
    build:
      context: ./backend/companyCore
    container_name: companycore
    expose:
      - "4000"
    env_file:
      - ./backend/companyCore/.env
    depends_on:
      mongo:
        condition: service_healthy
      mongo-init:
        condition: service_completed_successfully
  productcore:
    build:
      context: ./backend/productCore
    container_name: productcore
    expose:
      - "4000"
    env_file:
      - ./backend/productCore/.env
    depends_on:
      mongo:
        condition: service_healthy
      mongo-init:
        condition: service_completed_successfully
  mongo:
    image: mongo:7
    container_name: mongo
    ports:
      - "27018:27017"
    command: [ "mongod", "--config", "/etc/mongo/mongod.conf" ]
    healthcheck:
      test: [ "CMD", "mongosh", "--quiet", "--eval", "db.adminCommand({ ping: 1 }).ok" ]
      interval: 5s
      timeout: 5s
      retries: 20
      start_period: 10s
    volumes:
      - ./backend/.data/mongo:/data/db
      - ./backend/mongod.conf:/etc/mongo/mongod.conf:ro
  mongo-init:
    image: mongo:7
    container_name: mongo-init
    depends_on:
      mongo:
        condition: service_healthy
    restart: "no"
    entrypoint:
      - /bin/bash
      - -lc
      - >
        until mongosh --host mongo:27017 --quiet --eval "db.adminCommand({ ping: 1 }).ok" >/dev/null 2>&1; do
          sleep 2;
        done;
        mongosh --host mongo:27017 --quiet --eval "try { rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo:27017' }] }); } catch (e) { if (e.codeName !== 'AlreadyInitialized') { throw e; } }";
        until mongosh --host mongo:27017 --quiet --eval "db.hello().isWritablePrimary" | grep -q "true"; do
          sleep 2;
        done;

