version: '3'

services:
  server:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - db
      - filehosting
      - createbucket
    command: ['yarn', 'coko-server', 'start-dev']
    ports:
      - ${SERVER_PORT:-3000}:${SERVER_PORT:-3000}
    environment:
      - NODE_ENV=${NODE_ENV:-development}
      - NODE_CONFIG_DIR=./dev/config
      - POSTGRES_HOST=db
      - POSTGRES_PORT=${POSTGRES_PORT:-5432}
      - POSTGRES_DB=${POSTGRES_DB:-test_db}
      - POSTGRES_USER=${POSTGRES_USER:-test_user}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
      # - POSTGRES_ALLOW_SELF_SIGNED_CERTIFICATES=1
      - SECRET=${SECRET:-theSecret}
      - SERVER_PORT=${SERVER_PORT:-3000}
      - ALLOW_CONFIG_MUTATIONS=${ALLOW_CONFIG_MUTATIONS:-''}
      # - CLIENT_URL=${CLIENT_URL:-http://0.0.0.0:4000////}
      # - SERVER_URL=http://www.google.com///
      # - MAILER_SENDER=${MAILER_SENDER:-dev@example.com}
      # - MAILER_HOSTNAME=${MAILER_HOSTNAME}
      # - MAILER_PORT=${MAILER_PORT}
      # - MAILER_USER=${MAILER_USER}
      # - MAILER_PASSWORD=${MAILER_PASSWORD}
      - 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}
    volumes:
      - ./dev:/home/node/app/dev
      - ./src:/home/node/app/src
      - ./authorization:/home/node/app/authorization
    tty: true

  db:
    image: postgres:15-alpine
    ports:
      - ${POSTGRES_PORT:-5432}:5432
    environment:
      - POSTGRES_DB=${POSTGRES_DB:-test_db}
      - POSTGRES_USER=${POSTGRES_USER:-test_user}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}

  filehosting:
    image: minio/minio:RELEASE.2023-03-24T21-41-23Z
    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: 0.5s
      timeout: 20s
      retries: 30

  createbucket:
    image: minio/mc:RELEASE.2023-02-16T19-20-11Z
    depends_on:
      filehosting:
        condition: service_healthy
    entrypoint: >
      /bin/sh -c "
        /usr/bin/mc config host add cokoServer ${S3_PROTOCOL:-http}://${S3_HOST:-filehosting}:9000 ${MINIO_ROOT_USER:-admin} ${MINIO_ROOT_PASSWORD:-superSecretAdminPassword};
        /usr/bin/mc admin user add cokoServer/ ${S3_ACCESS_KEY_ID:-cokoServerUser} ${S3_SECRET_ACCESS_KEY:-superSecretUserPassword};
        /usr/bin/mc admin user enable cokoServer/ ${S3_ACCESS_KEY_ID:-cokoServerUser};
        /usr/bin/mc mb cokoServer/${S3_BUCKET:-uploads};
        /usr/bin/mc admin policy set cokoServer/ readwrite user=${S3_ACCESS_KEY_ID:-cokoServerUser};
        exit 0;
      "

volumes:
  minio_storage:
