version: '3'

services:
  client:
    build:
      context: .
      dockerfile: ./Dockerfile-development
    depends_on:
      - server
    command:
      [
        'node_modules/.bin/webpack-dev-server',
        '--config',
        './webpack/webpack.development.config.js',
      ]
    ports:
      - ${CLIENT_PORT:-4000}:${CLIENT_PORT:-4000}
    environment:
      - NODE_ENV=development
      - CLIENT_PROTOCOL=${CLIENT_PROTOCOL:-http}
      - CLIENT_HOST=0.0.0.0
      - CLIENT_PORT=${CLIENT_PORT:-4000}
      - SERVER_PROTOCOL=http
      - SERVER_HOST=server
      - SERVER_PORT=3000
      - PUBLIC_CLIENT_HOST=${PUBLIC_CLIENT_HOST:-0.0.0.0}
      - USE_COLAB_EMAIL=${USE_COLAB_EMAIL:-false}
      - USE_COLAB_BIOPHYSICS_IMPORT=${USE_COLAB_BIOPHYSICS_IMPORT:-false}
      - FLAX_SITE_URL=${FLAX_SITE_URL:-http://localhost:8081}

    volumes:
      - ./app:/home/node/app/app
      - ./config:/home/node/app/config
      - ./certs:/home/node/app/certs

  server:
    build:
      context: .
      dockerfile: ./Dockerfile-development
    depends_on:
      - db
      - xsweet
    entrypoint:
      [
        'node_modules/.bin/wait-for-it',
        'db:5432',
        '--',
        'sh',
        'scripts/setupDevServer.sh',
      ]
    command:
      [
        'node_modules/.bin/nodemon',
        'startServer.js',
        '--',
        'server',
        '--',
        'config',
        '--ext',
        'js,graphql',
      ]
    ports:
      - ${SERVER_PORT:-3000}:3000
    environment:
      - NODE_ENV=development
      - POSTGRES_USER=${POSTGRES_USER:-dev}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
      - POSTGRES_DB=${POSTGRES_DB:-kotahi_dev}
      - POSTGRES_HOST=db
      - POSTGRES_PORT=5432
      - PUBSWEET_SECRET=${PUBSWEET_SECRET:-dev_secret}
      - CLIENT_PROTOCOL=${CLIENT_PROTOCOL:-http}
      - CLIENT_HOST=${CLIENT_HOST:-localhost}
      - CLIENT_PORT=${CLIENT_PORT:-4000}
      - PUBLIC_CLIENT_PROTOCOL=${PUBLIC_CLIENT_PROTOCOL:-}
      - PUBLIC_CLIENT_HOST=${PUBLIC_CLIENT_HOST:-}
      - PUBLIC_CLIENT_PORT=${PUBLIC_CLIENT_PORT:-}
      - ORCID_CLIENT_ID=${ORCID_CLIENT_ID}
      - ORCID_CLIENT_SECRET=${ORCID_CLIENT_SECRET}
      - USE_SANDBOXED_ORCID=${USE_SANDBOXED_ORCID:-true}
      - USE_COLAB_EMAIL=${USE_COLAB_EMAIL:-false}
      - KOTAHI_API_TOKENS=${KOTAHI_API_TOKENS:-}
      - JOURNAL_NAME=${JOURNAL_NAME:-}
      - JOURNAL_ABBREVIATED_NAME=${JOURNAL_ABBREVIATED_NAME:-}
      - JOURNAL_HOMEPAGE=${JOURNAL_HOMEPAGE:-}
      - GOOGLE_SPREADSHEET_CLIENT_EMAIL=${GOOGLE_SPREADSHEET_CLIENT_EMAIL:-}
      - GOOGLE_SPREADSHEET_PRIVATE_KEY=${GOOGLE_SPREADSHEET_PRIVATE_KEY:-}
      - GOOGLE_SPREADSHEET_ID=${GOOGLE_SPREADSHEET_ID:-}
      - HYPOTHESIS_API_KEY=${HYPOTHESIS_API_KEY:-}
      - HYPOTHESIS_GROUP=${HYPOTHESIS_GROUP:-}
      - HYPOTHESIS_REVERSE_FIELD_ORDER=${HYPOTHESIS_REVERSE_FIELD_ORDER:-}
      - HYPOTHESIS_ALLOW_TAGGING=${HYPOTHESIS_ALLOW_TAGGING:-}
      - CROSSREF_LOGIN=${CROSSREF_LOGIN:-}
      - CROSSREF_PASSWORD=${CROSSREF_PASSWORD:-}
      - CROSSREF_REGISTRANT=${CROSSREF_REGISTRANT:-}
      - CROSSREF_DEPOSITOR_NAME=${CROSSREF_DEPOSITOR_NAME:-}
      - CROSSREF_DEPOSITOR_EMAIL=${CROSSREF_DEPOSITOR_EMAIL:-}
      - CROSSREF_PUBLICATION_TYPE=${CROSSREF_PUBLICATION_TYPE:-article}
      - CROSSREF_USE_SANDBOX=${CROSSREF_USE_SANDBOX:-true}
      - DOI_PREFIX=${DOI_PREFIX:-}
      - PUBLISHED_ARTICLE_LOCATION_PREFIX=${PUBLISHED_ARTICLE_LOCATION_PREFIX:-}
      - PUBLICATION_LICENSE_URL=${PUBLICATION_LICENSE_URL:-}
      - MANUSCRIPTS_TABLE_COLUMNS=${MANUSCRIPTS_TABLE_COLUMNS}
      - GMAIL_NOTIFICATION_EMAIL_AUTH=${GMAIL_NOTIFICATION_EMAIL_AUTH}
      - GMAIL_NOTIFICATION_EMAIL_SENDER=${GMAIL_NOTIFICATION_EMAIL_SENDER}
      - GMAIL_NOTIFICATION_PASSWORD=${GMAIL_NOTIFICATION_PASSWORD}
      - TEST_EMAIL_RECIPIENT=${TEST_EMAIL_RECIPIENT}
      - PUBLISHING_WEBHOOK_URL=${PUBLISHING_WEBHOOK_URL:-}
      - PUBLISHING_WEBHOOK_TOKEN=${PUBLISHING_WEBHOOK_TOKEN:-}
      - PUBLISHING_WEBHOOK_REF=${PUBLISHING_WEBHOOK_REF:-}
      - NOTIFICATION_EMAIL_AUTOMATED=${NOTIFICATION_EMAIL_AUTOMATED:-false}
      - NOTIFICATION_EMAIL_CC_ENABLED=${NOTIFICATION_EMAIL_CC_ENABLED:-false}
      - S3_PROTOCOL=http
      - S3_HOST=fileHosting
      - S3_PORT=${S3_PORT:-9000}
      - S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID:-nonRootUser}
      - S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY:-nonRootPassword}
      - S3_BUCKET=${S3_BUCKET:-uploads}
      - MINIO_ROOT_USER=${MINIO_ROOT_USER:-admin}
      - MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-superSecretAdminPassword}
      - MINIO_CONSOLE_PORT=${MINIO_CONSOLE_PORT:-9001}
      - SERVICE_PAGEDJS_CLIENT_ID=59a3392b-0c4f-4318-bbe2-f86eff6d3de4
      - SERVICE_PAGEDJS_SECRET=asldkjLKJLaslkdf897kjhKUJH
      - SERVICE_PAGEDJS_PROTOCOL=${SERVICE_PAGEDJS_PROTOCOL:-http}
      - SERVICE_PAGEDJS_HOST=${SERVICE_PAGEDJS_HOST:-pagedjs}
      - SERVICE_PAGEDJS_PORT=${SERVICE_PAGEDJS_PORT:-3003}
      - SERVICE_ANYSTYLE_CLIENT_ID=${SERVICE_ANYSTYLE_CLIENT_ID}
      - SERVICE_ANYSTYLE_SECRET=${SERVICE_ANYSTYLE_SECRET}
      - SERVICE_ANYSTYLE_PROTOCOL=${SERVICE_ANYSTYLE_PROTOCOL:-http}
      - SERVICE_ANYSTYLE_HOST=${SERVICE_ANYSTYLE_HOST:-anystyle}
      - SERVICE_ANYSTYLE_PORT=${SERVICE_ANYSTYLE_PORT}
      - AUTO_IMPORT_HOUR_UTC=${AUTO_IMPORT_HOUR_UTC:-}
      - DISABLE_EVENT_NOTIFICATIONS=${DISABLE_EVENT_NOTIFICATIONS:-}
      - ARCHIVE_PERIOD_DAYS=${ARCHIVE_PERIOD_DAYS:-}
      - ALLOW_MANUAL_IMPORT=${ALLOW_MANUAL_IMPORT:-}
      - SEMANTIC_SCHOLAR_IMPORTS_RECENCY_PERIOD_DAYS=${SEMANTIC_SCHOLAR_IMPORTS_RECENCY_PERIOD_DAYS:-}
      - TEAM_TIMEZONE=${TEAM_TIMEZONE:-Etc/UTC}
      - REVIEW_SHARED=${REVIEW_SHARED:-false}
      - REVIEW_HIDE=${REVIEW_HIDE:-false}
      - DISPLAY_SHORTID_AS_IDENTIFIER=${DISPLAY_SHORTID_AS_IDENTIFIER:-false}
      - SERVICE_XSWEET_CLIENT_ID=59a3392b-0c4f-4318-bbe2-f86eff6d3de4
      - SERVICE_XSWEET_SECRET=asldkjLKJLaslkdf897kjhKUJH
      - SERVICE_XSWEET_PROTOCOL=${SERVICE_XSWEET_PROTOCOL:-http}
      - SERVICE_XSWEET_HOST=${SERVICE_XSWEET_HOST:-xsweet}
      - SERVICE_XSWEET_PORT=${SERVICE_XSWEET_PORT:-3004}
      - FLAX_EXPRESS_PORT=${FLAX_EXPRESS_PORT:-3000}
      - FLAX_EXPRESS_HOST=${FLAX_EXPRESS_HOST:-kotahi-flax-site}
      - FLAX_EXPRESS_PROTOCOL=${FLAX_EXPRESS_PROTOCOL:-http}
      - FLAX_CLIENT_ID=${FLAX_CLIENT_ID:-}
      - FLAX_CLIENT_SECRET=${FLAX_CLIENT_SECRET:-}
      - FLAX_CLIENT_API_URL=${FLAX_CLIENT_API_URL:-http://client:4000}
      - FLAX_SITE_URL=${FLAX_SITE_URL:-false}
      - USE_APERTURE_EMAIL=${USE_APERTURE_EMAIL:-false}
      - INSTANCE_GROUPS=${INSTANCE_GROUPS:-kotahi:aperture}
      - USE_COLAB_BIOPHYSICS_IMPORT=${USE_COLAB_BIOPHYSICS_IMPORT:-false}

    volumes:
      - ./config:/home/node/app/config
      - ./server:/home/node/app/server
      - ./uploads:/home/node/app/uploads

  pagedjs:
    depends_on:
      - db-pagedjs
    image: cokoapps/pagedjs:1.4.0
    ports:
      - ${SERVICE_PAGEDJS_PORT:-3003}:${SERVICE_PAGEDJS_PORT:-3003}
    environment:
      - PUBSWEET_SECRET=pagedjs_dev_secret
      - POSTGRES_USER=pagedjs_user_dev
      - POSTGRES_PASSWORD=pagedjs_user_password
      - POSTGRES_HOST=db-pagedjs
      - POSTGRES_DB=pagedjs_dev
      - POSTGRES_PORT=5432
      - SERVER_PORT=${SERVICE_PAGEDJS_PORT:-3003}
      - SERVER_PROTOCOL=http
      - SERVER_HOST=localhost
      - NODE_ENV=development
    security_opt:
      - seccomp:unconfined
    entrypoint:
      [
        'node_modules/.bin/wait-for-it',
        'db-pagedjs:5432',
        '--',
        'sh',
        'scripts/setupDevServer.sh',
      ]
    command: ['node', './server/startServer.js']

  db-pagedjs:
    image: postgres:12-alpine
    environment:
      - POSTGRES_USER=pagedjs_user_dev
      - POSTGRES_DB=pagedjs_dev
      - POSTGRES_PASSWORD=pagedjs_user_password
    volumes:
      - ./scripts/init-pgboss.sql:/docker-entrypoint-initdb.d/init-pgboss.sql

  xsweet:
    depends_on:
      - db-xsweet
    image: cokoapps/xsweet:2.1.2
    ports:
      - ${SERVICE_XSWEET_PORT:-3004}:${SERVICE_XSWEET_PORT:-3004}
    environment:
      - PUBSWEET_SECRET=xsweet_dev_secret
      - POSTGRES_USER=xsweet_user_dev
      - POSTGRES_PASSWORD=xsweet_user_password
      - POSTGRES_HOST=db-xsweet
      - POSTGRES_DB=xsweet_dev
      - POSTGRES_PORT=5432
      - SERVER_PORT=${SERVICE_XSWEET_PORT:-3004}
      - NODE_ENV=development
    entrypoint:
      [
        'sh',
        'scripts/wait-for-it',
        'db-xsweet:5432',
        '--',
        'sh',
        'scripts/setupDevServer.sh',
      ]
    command: ['node', 'server/startServer.js']

  db-xsweet:
    image: postgres:14.4-alpine
    environment:
      - POSTGRES_USER=xsweet_user_dev
      - POSTGRES_DB=xsweet_dev
      - POSTGRES_PASSWORD=xsweet_user_password
    volumes:
      - ./scripts/init-pgboss.sql:/docker-entrypoint-initdb.d/init-pgboss.sql

  db:
    image: postgres:10-alpine
    sysctls:
      kernel.shmmax: 100663296
    ports:
      - ${POSTGRES_PORT:-5432}:5432
    environment:
      - POSTGRES_DB=${POSTGRES_DB:-kotahi_dev}
      - POSTGRES_USER=${POSTGRES_USER:-dev}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
    volumes:
      - ./scripts/test.sql:/docker-entrypoint-initdb.d/test.sql
    command:
      - 'postgres'
      - '-c'
      - 'max_connections=1000'
      - '-c'
      - 'shared_buffers=3GB'
      - '-c'
      - 'sort_mem=4096'
      - '-c'
      - 'effective_cache_size=4000'
      - '-c'
      - 'wal_buffers=8'

  # nginx:
  #   image: nginx:1.19
  #   depends_on:
  #     - client
  #   ports:
  #     - 443:443
  #     - 88:80
  #   volumes:
  #     - ./nginx/conf.d:/etc/nginx/conf.d
  #     - ./certs:/etc/nginx/certs

  # Create the file server and give it a root user
  fileHosting:
    image: minio/minio:RELEASE.2023-05-04T21-44-30Z
    ports:
      - ${S3_PORT:-9000}:9000
      - ${MINIO_CONSOLE_PORT:-9001}:9001
    volumes:
      - minio_storage:/data
    environment:
      - MINIO_ROOT_USER=${MINIO_ROOT_USER:-admin}
      - MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD:-superSecretAdminPassword}
    command: server --console-address ":${MINIO_CONSOLE_PORT:-9001}" /data
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:9000/minio/health/live']
      interval: 30s
      timeout: 20s
      retries: 3

  # Create non-root user and bucket that we'll connect to
  createbucket:
    image: minio/mc:RELEASE.2023-05-04T18-10-16Z
    depends_on:
      - fileHosting
    entrypoint: >
      /bin/sh -c "
      /usr/bin/mc config host add cokoServer http://fileHosting:${S3_PORT:-9000} ${MINIO_ROOT_USER:-admin} ${MINIO_ROOT_PASSWORD:-superSecretAdminPassword};
      /usr/bin/mc admin user add cokoServer/ ${S3_ACCESS_KEY_ID:-nonRootUser} ${S3_SECRET_ACCESS_KEY:-nonRootPassword};
      /usr/bin/mc admin user enable cokoServer/ ${S3_ACCESS_KEY_ID:-nonRootUser};
      /usr/bin/mc mb cokoServer/${S3_BUCKET:-uploads};
      /usr/bin/mc admin policy attach cokoServer/ readwrite --user=${S3_ACCESS_KEY_ID:-nonRootUser};
      exit 0;
      "

  anystyle:
    depends_on:
      - db-anystyle
    image: cokoapps/anystyle:1.1.1
    ports:
      - ${SERVICE_ANYSTYLE_PORT:-4567}:${SERVICE_ANYSTYLE_PORT:-4567}
    environment:
      - SERVER_PORT=${SERVICE_ANYSTYLE_PORT:-4567}
      - SERVER_PROTOCOL=http
      - SERVER_HOST=localhost
      - PUBSWEET_SECRET=anystyle_dev_secret
      - POSTGRES_USER=anystyle_user_dev
      - POSTGRES_PASSWORD=anystyle_user_password
      - POSTGRES_HOST=db-anystyle
      - POSTGRES_DB=anystyle_dev
      - POSTGRES_PORT=5432
      - NODE_ENV=development

    security_opt:
      - seccomp:unconfined
    entrypoint:
      [
        'node_modules/.bin/wait-for-it',
        'db-anystyle:5432',
        '--',
        'sh',
        'scripts/setupDevServer.sh',
      ]
    command: ['node', './server/startServer.js']

  db-anystyle:
    image: postgres:12-alpine
    environment:
      - POSTGRES_USER=anystyle_user_dev
      - POSTGRES_DB=anystyle_dev
      - POSTGRES_PASSWORD=anystyle_user_password
    volumes:
      - ./scripts/init-pgboss.sql:/docker-entrypoint-initdb.d/init-pgboss.sql

  kotahi-flax-site:
    image: cokoapps/kotahi-flax:0.2.27
    depends_on:
      - client
    ports:
      - ${FLAX_EXPRESS_PORT:-8082}:${FLAX_EXPRESS_PORT:-3000}
      - ${FLAX_SITE_PORT:-8081}:80
    environment:
      - FLAX_EXPRESS_PORT=${FLAX_EXPRESS_PORT:-3000}
      - FLAX_CLIENT_ID=${FLAX_CLIENT_ID:-}
      - FLAX_CLIENT_SECRET=${FLAX_CLIENT_SECRET:-}
      - FLAX_CLIENT_API_URL=${FLAX_CLIENT_API_URL:-http://client:4000}
      - FLAX_DEFAULT_GROUP=${FLAX_DEFAULT_GROUP:-"kotahi"}

volumes:
  minio_storage:
