###################################################################
# This docker-compose file should only be used for local
# installations.
#
# docker-compose sets up all services to be served by nginx
# and exposes the following ports:
#
# - 8080 for BadgerDoc microservices through nginx
# - 5432 for PostgreSQL (the port can be changed in the .env
#   file for local needs)
# - 8082 for Keycloak.
# - 8083 for BadgerDoc web
# - 9001 for minio
#
###################################################################
services:
  nginx:
    image: badgerdoc_nginx
    container_name: badgerdoc_nginx
    networks:
      - badgerdoc
    depends_on:
      - badgerdoc-annotation
      - badgerdoc-users
      - badgerdoc-processing
      - badgerdoc-jobs
      - badgerdoc-assets
    ports:
      - "8080:8080"

  # BadgerDoc dependencies

  badgerdoc-minio:
    image: "bitnami/minio:2023.3.13"
    container_name: badgerdoc-minio
    environment:
      - MINIO_ROOT_USER=${S3_ACCESS_KEY}
      - MINIO_ROOT_PASSWORD=${S3_SECRET_KEY}
      - MINIO_DEFAULT_BUCKETS=${MINIO_DEFAULT_BUCKETS}
    networks:
      - badgerdoc
    volumes:
      - minio-data:/data
    ports:
      - 9000:9000
      - 9001:9001

  badgerdoc-postgresql:
    image: postgres:13.4
    container_name: badgerdoc-postgresql
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    ports:
      - ${POSTGRES_PORT}:${POSTGRES_PORT}
    restart: always
    networks:
      - badgerdoc
    volumes:
      - badgerdoc-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5

  badgerdoc-keycloak:
    image: badgerdoc_keycloak
    container_name: badgerdoc-keycloak
    ports:
      - "8082:8080"
    environment:
      DB_VENDOR: POSTGRES
      DB_ADDR: badgerdoc-postgresql
      DB_DATABASE: ${POSTGRES_DB}
      DB_USER: ${POSTGRES_USER}
      DB_SCHEMA: public
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      KEYCLOAK_USER: ${KEYCLOAK_USER}
      KEYCLOAK_PASSWORD: ${KEYCLOAK_PASSWORD}
      KEYCLOAK_LOGLEVEL: DEBUG
    depends_on:
      badgerdoc-postgresql:
        condition: service_healthy
    networks:
      - badgerdoc
    restart: on-failure

  # BadgerDoc microservices

  badgerdoc-annotation:
    image: badgerdoc_annotation
    container_name: annotation
    env_file:
      - .env
    environment:
      - ROOT_PATH=/annotation
    networks:
      - badgerdoc
    depends_on:
      badgerdoc-postgresql:
        condition: service_healthy
      badgerdoc-keycloak:
        condition: service_started

  badgerdoc-users:
    image: badgerdoc_users
    container_name: users
    env_file:
      - .env
    environment:
      - ROOT_PATH=/users
    networks:
      - badgerdoc
    depends_on:
      badgerdoc-postgresql:
        condition: service_healthy
      badgerdoc-keycloak:
        condition: service_started

  badgerdoc-processing:
    image: badgerdoc_processing
    container_name: processing
    env_file:
      - .env
    environment:
      - ROOT_PATH=/processing
    networks:
      - badgerdoc
    depends_on:
      badgerdoc-postgresql:
        condition: service_healthy
      badgerdoc-keycloak:
        condition: service_started

  badgerdoc-jobs:
    image: badgerdoc_jobs
    container_name: jobs
    env_file:
      - .env
    environment:
      - ROOT_PATH=/jobs
    networks:
      - badgerdoc
    depends_on:
      badgerdoc-postgresql:
        condition: service_healthy
      badgerdoc-keycloak:
        condition: service_started

  badgerdoc-assets:
    image: badgerdoc_assets
    container_name: assets
    env_file:
      - .env
    environment:
      - ROOT_PATH=/assets
    networks:
      - badgerdoc
    depends_on:
      badgerdoc-postgresql:
        condition: service_healthy
      badgerdoc-keycloak:
        condition: service_started

  badgerdoc-web:
    image: badgerdoc_web
    container_name: web
    networks:
      - badgerdoc
    ports:
      - 8083:3000

  badgerdoc-taxonomy:
    image: badgerdoc_taxonomy
    container_name: taxonomy
    env_file:
      - .env
    environment:
      - ROOT_PATH=/taxonomy
    networks:
      - badgerdoc
    depends_on:
      badgerdoc-postgresql:
        condition: service_healthy
    restart: on-failure

  # TBD: Gotenberg

networks:
  badgerdoc:
    driver: bridge

volumes:
  badgerdoc-data: {}
  minio-data: {}
