services:
############################## PROXY ########################################
  opencdmp.proxy:
    restart: unless-stopped
    cpus: 1
    mem_limit: 256m
    ports:
      - "${PROXY_APP_PORT}:8081"
    volumes:
      - ./proxy/nginx.conf:/etc/nginx/nginx.conf
      - ./proxy/ProxyNginx.conf:/etc/nginx/conf.d/default.conf
      - ./logs/proxy:/tmp/logs
    networks:
      - opencdmp-proxy-network

############################## OPENDMP APP #################################
  opencdmp.backend:
    restart: unless-stopped
    cpus: 1
    mem_limit: 2048m
    environment:
      - PROFILE=${PROFILE}
    ports:
      - "0.0.0.0:50030:8080"
    volumes:
      - ./opencdmp/backend/config:/config
      - ./opencdmp/backend/certificates:/certificates
      - ./opencdmp/backend/i18n:/i18n
      - ./opencdmp/backend/material:/material
      - ./opencdmp/backend/Semantics.json:/Semantics.json
      - ./storage/opencdmp/backend:/storage
      - ./logs/opencdmp/backend:/logs
    networks:
      - opencdmp-proxy-network
      - opencdmp-backend-network
      - opencdmp-plugins-network
      - opencdmp-elastic-shared-network
      - opencdmp-keycloak-shared-network
      - opencdmp-postgres-shared-network
      - opencdmp-rabbitmq-shared-network

  opencdmp.frontend:
    restart: unless-stopped
    cpus: 1
    mem_limit: 2048m
    ports:
      - "0.0.0.0:50031:8080"
    volumes:
      - ./opencdmp/frontend/config.json:/usr/share/nginx/html/assets/config/config.json
      - ./opencdmp/frontend/nginx.conf:/etc/nginx/nginx.conf
      - ./opencdmp/frontend/WebAppNginx.conf:/etc/nginx/conf.d/default.conf
    networks:
      - opencdmp-proxy-network

  opencdmp.notification:
    cpus: 1
    mem_limit: 1024m
    restart: unless-stopped
    ports:
      - "0.0.0.0:50036:8080"
    environment:
      - PROFILE=${PROFILE}
    volumes:
      - ./opencdmp/notification/config:/config
      - ./opencdmp/notification/messages:/messages
      - ./opencdmp/notification/notification_templates:/notification_templates
      - ./logs/opencdmp/notification:/logs
    networks:
      - opencdmp-proxy-network
      - opencdmp-backend-network
      - opencdmp-rabbitmq-shared-network
      - opencdmp-keycloak-shared-network
      - opencdmp-postgres-shared-network

  opencdmp.annotation:
    cpus: 1
    mem_limit: 1024m
    restart: unless-stopped
    ports:
      - "0.0.0.0:50035:8080"
    environment:
      - PROFILE=${PROFILE}
    volumes:
      - ./opencdmp/annotation/config:/config
      - ./opencdmp/annotation/messages:/messages
      - ./logs/opencdmp/annotation:/logs
    networks:
      - opencdmp-proxy-network
      - opencdmp-backend-network
      - opencdmp-rabbitmq-shared-network
      - opencdmp-keycloak-shared-network
      - opencdmp-postgres-shared-network

# # ############################## FILE-TRANSFORMER #################################

  opencdmp.file.transformer.docx:
    restart: unless-stopped
    cpus: 1
    mem_limit: 1024m
    expose:
      - "8080"
    environment:
      - SERVER_PORT=${SERVER_PORT}
      - PROFILE=${PROFILE}
    volumes:
      - ./opencdmp/file-transformer-docx/config:/config
      - ./logs/opencdmp/file-transformer-docx:/logs
      # - ./storage/opencdmp/file-transformer-docx:/storage
    networks:
      - opencdmp-plugins-network
      - opencdmp-gotenberg-shared-network

  opencdmp.file.transformer.rdajson:
    restart: unless-stopped
    cpus: 1
    mem_limit: 1024m
    expose:
      - "8080"
    environment:
      - SERVER_PORT=${SERVER_PORT}
      - PROFILE=${PROFILE}
    volumes:
      - ./opencdmp/file-transformer-rda-json/config:/config
      - ./opencdmp/file-transformer-rda-json/internal:/internal
      - ./logs/opencdmp/file-transformer-rda-json:/logs
      # - ./storage/opencdmp/file-transformer-rda-json:/storage
    networks:
      - opencdmp-plugins-network

# ############################## ZENODO #######################################
  opencdmp.zenodo:
    restart: unless-stopped
    cpus: 1
    mem_limit: 1024m
    expose:
      - "8080"
    environment:
      - PROFILE=${PROFILE}
    volumes:
      - ./opencdmp/zenodo/config:/config
      - ./opencdmp/zenodo/zenodo.jpg:/zenodo.jpg
      - ./logs/opencdmp/zenodo:/logs
      # - ./storage/opencdmp/zenodo:/storage
    networks:
      - opencdmp-plugins-network
      - opencdmp-keycloak-shared-network

############################## POSTGRES 16 #################################

  opencdmp.postgres:
      restart: unless-stopped
      mem_limit: 2048M
      ports:
        # - "127.0.0.1:${POSTGRES_PORT}:5432" # If you want to make it accessible locally only
        - "${POSTGRES_PORT}:5432"
      env_file:
        - ./postgres/postgres.env
      volumes:
        - ./storage/postgres/data:/var/lib/postgresql/data
        - ./postgres/opencdmp_init.sql:/docker-entrypoint-initdb.d/opencdmp_init.sql
      networks:
        - opencdmp-postgres-shared-network
      healthcheck:
        test: ["CMD-SHELL", "sh -c 'pg_isready -U postgres -d opencdmp'"]
        interval: 15s
        timeout: 60s
        retries: 5


################################# ELASTICSEARCH #################################################
  opencdmp.elasticsearch:
    restart: unless-stopped
    cpus: 2
    mem_limit: 1024m
    env_file:
      - ./elk/elk.env
    environment:
      ES_JAVA_OPTS: -Xmx512m -Xms512m
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ./elk/elasticsearch/index_init.sh:/usr/share/elasticsearch/config/index_init.sh
      - ./elk/elasticsearch/log4j2.properties:/usr/share/elasticsearch/config/log4j2.properties:ro
      - ./elk/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro
      - ./storage/elasticsearch:/usr/share/elasticsearch/data
      - ./logs/elasticsearch:/usr/share/elasticsearch/logs
    expose:
      - "9200"
      - "9300"
    networks:
      - opencdmp-elastic-shared-network
    healthcheck:
      test: curl -s http://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
      interval: 30s
      timeout: 10s
      retries: 5

#################################### KEYCLOAK ###############################################
  opencdmp.keycloak:
    restart: unless-stopped
    command: ["start-dev", "--log=console,file", "--log-file=/tmp/logs/keycloak.log", "--import-realm"]
    cpus: 2
    mem_limit: 2048M
    env_file:
      - ./keycloak/keycloak.env
    environment:
      - JAVA_OPTS_APPEND="-Djava.net.preferIPv4Stack=true"
    volumes:
      - ./logs/keycloak:/tmp/logs
      - ./keycloak/user_init.sh:/tmp/user_init.sh
      - ./keycloak/imports/opencdmp-realm.json:/opt/keycloak/data/import/opencdmp-realm.json
    expose:
      - "8080"
    networks:
      - opencdmp-proxy-network
      - opencdmp-postgres-shared-network
      - opencdmp-keycloak-shared-network

################################# RABBITMQ ###############################################
  opencdmp.rabbitmq:
    labels:
      NAME: "rabbitmq"
    cpus: 1
    mem_limit: 512m
    restart: unless-stopped
    ports:
      - "0.0.0.0:15672:15672"
      - "0.0.0.0:5672:5672"
    env_file:
      - ./rabbitmq/rabbitmq.env
    volumes:
     - /rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
    networks:
      - opencdmp-rabbitmq-shared-network

################################# GOTENBERG ##############################################
  opencdmp.gotenberg:
    cpus: 2
    mem_limit: 2048m
    restart: unless-stopped
    expose:
     - "3000"
    hostname: gotenberg
    env_file:
      - ./gotenberg/gotenberg.env
    networks:
      - opencdmp-gotenberg-shared-network

networks:
  opencdmp-proxy-network:
    name:  opencdmp-proxy-network
  opencdmp-backend-network:
    name: opencdmp-backend-network
  opencdmp-plugins-network:
    name: opencdmp-plugins-network
  opencdmp-keycloak-shared-network:
    external: true
    name: opencdmp-keycloak-shared-network
  opencdmp-gotenberg-shared-network:
    external: true
    name: opencdmp-gotenberg-shared-network
  opencdmp-rabbitmq-shared-network:
    external: true
    name: opencdmp-rabbitmq-shared-network
  opencdmp-elastic-shared-network:
    external: true
    name: opencdmp-elastic-shared-network
  opencdmp-postgres-shared-network:
    external: true
    name: opencdmp-postgres-shared-network