version: "3"

services:

  dbadmin:
    image: postgres:12
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=${DBADMIN_USER}
      - POSTGRES_PASSWORD=${DBADMIN_PASSWORD}
      - POSTGRES_DB=dbadmin

  dbproject1:
    image: postgres:12
    expose:
      - "5433"
    ports:
      - "5433:5433"
    command: -p 5433
    environment:
      - POSTGRES_USER=${DBPROJECT_USER}
      - POSTGRES_PASSWORD=${DBPROJECT_PASSWORD}

  esap_api:
    image: esap_api:latest
    container_name: esap-api
    environment:
      - OIDC_RP_CLIENT_ID=669d7bef-32c0-4980-ae35-d8ede56bd5ef
      - OIDC_RP_CLIENT_SECRET
      - OIDC_OP_JWKS_ENDPOINT=https://iam-escape.cloud.cnaf.infn.it/jwk
      - OIDC_OP_AUTHORIZATION_ENDPOINT=https://iam-escape.cloud.cnaf.infn.it/authorize
      - OIDC_OP_TOKEN_ENDPOINT=https://iam-escape.cloud.cnaf.infn.it/token
      - OIDC_OP_USER_ENDPOINT=https://iam-escape.cloud.cnaf.infn.it/userinfo
      - LOGIN_REDIRECT_URL=http://localhost:3000/esap-gui/login
      - LOGOUT_REDIRECT_URL=http://localhost:3000/esap-gui/logout
      - LOGIN_REDIRECT_URL_FAILURE=http://localhost:3000/esap-gui/error
    build:
      context: ${ESAP_ROOT}/esap-api-gateway/esap
    ports:
      - 8000:8000
    restart: always
    command: "python manage.py runserver 0.0.0.0:8000 --settings=esap.settings.dev"

  esap_db:
    image: esap_db:latest
    container_name: esap-db
    stdin_open: true  # docker run -i
    tty: true         # docker run -t
    environment:
      - SERVER_NAME=${DOMAIN?Variable not set}
      - SERVER_HOST=https://${DOMAIN?Variable not set}
      - SENTRY_DSN=https://nowhere.com
      - PROJECT_NAME=ESAP-DB
      - DBADMIN_SERVER=dbadmin
      - DBADMIN_USER
      - DBADMIN_PASSWORD
      - DBADMIN_DB=dbadmin
      - DBPROJECT_USER
      - DBPROJECT_PASSWORD
      - DBPROJECT_SERVERS=["dbproject1:5433"]
      - FIRST_SUPERUSER=esapadmin@nowhere.com
      - FIRST_SUPERUSER_PASSWORD=esapadmin
    build:
      context: ${ESAP_ROOT}/esap-db
      args:
        INSTALL_DEV: ${INSTALL_DEV-false}
    command: bash -c "scripts/prepare-app.sh && uvicorn app.main:app --port 8001 --host 0.0.0.0 --reload"
    volumes:
      - ${ESAP_ROOT}/esap-db:/code
    ports:
      - "8001:8001"
    depends_on:
      - dbadmin

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4:latest
    environment:
      - PGADMIN_DEFAULT_EMAIL=pgadmin4@pgadmin.org
      - PGADMIN_DEFAULT_PASSWORD=admin
    ports:
      - "5050:80"
    depends_on:
      - dbadmin
