services:
  db:
    image: mdillon/postgis:11
    restart: always
    environment:
      - POSTGRES_USER=dev
      - POSTGRES_PASSWORD=dev
    ports:
      - "${EXPOSE_PG_PORT:-4204}:5432"
    volumes:
      - pg_data:/var/lib/postgresql/data

  minio:
    image: bitnami/minio:2022-debian-10
    ports:
      - "${EXPOSE_MINO_PORT:-4290}:9000"
      - "${EXPOSE_MINIO_CONSOLE_PORT:-4291}:9001"
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minio-admin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minio-admin}
      MINIO_FORCE_NEW_KEYS: "yes"
      BITNAMI_DEBUG: "true"
    volumes:
      - minio_data:/data
  minio-setup:
    image: minio/mc
    depends_on:
      - minio
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minio-admin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minio-admin}
    entrypoint: >
      /bin/sh -c "
      set -e
      mc alias set myminio http://minio:9000 ${MINIO_ROOT_USER:-minio-admin} ${MINIO_ROOT_PASSWORD:-minio-admin};
      until mc ls myminio; do
        echo 'Waiting for MinIO to be ready...';
        sleep 1;
      done;
      echo 'MinIO is up and running, configuring...';
      mc mb myminio/avatar;
      mc mb myminio/audio;
      mc anonymous set download myminio/avatar;
      "

  rabbitmq:
    image: docker.io/bitnami/rabbitmq:3.9
    restart: always
    volumes:
      - rabbitmq_data:/bitnami
    ports:
      - ${EXPOSE_RABBITMQ_NODE_PORT}:5672
      - ${EXPOSE_RABBITMQ_MANAGEMENT_PORT_PORT}:15672
    environment:
      - RABBITMQ_USERNAME=dev
      - RABBITMQ_PASSWORD=dev
      - RABBITMQ_DISK_FREE_ABSOLUTE_LIMIT=2GB

  redis-q-dedup:
    image: &redisImage bitnami/redis:7.2
    # restart: always
    ports:
      - "${EXPOSE_REDIS_QD_PORT:-4278}:6379"
    environment: # https://hub.docker.com/r/bitnami/redis
      REDIS_PASSWORD: redis-password
      REDIS_PORT: "6379"
      REDIS_AOF_ENABLED: "no"

  redis-hot-geodata:
    image: *redisImage
    # restart: always
    ports:
      - "${EXPOSE_REDIS_HG_PORT:-4279}:6379"
    volumes:
      - redis_hg_data:/bitnami/redis/data
    environment: # https://hub.docker.com/r/bitnami/redis
      REDIS_PASSWORD: redis-password
      REDIS_PORT: "6379"
      REDIS_AOF_ENABLED: "yes"
      REDIS_DISABLE_COMMANDS: FLUSHDB,FLUSHALL

  keydb-cold-geodata:
    image: eqalpha/keydb:x86_64_v6.3.4
    # restart: always
    ports:
      - "${EXPOSE_KEYDB_CG_PORT:-4277}:6379"
    volumes:
      - keydb_cg_data:/data
    command:
      - keydb-server
      - /etc/keydb/redis.conf

      - --requirepass
      - keydb-password

      - --server-threads
      - "2"
      
      - --storage-provider
      - flash
      - /data/flash
      
      - --maxmemory
      - "1G"

      - --maxmemory-policy
      - allkeys-lfu
  hasura:
    image: helpme_hasura
    restart: always
    build:
      context: .
      dockerfile: ./services/hasura/Dockerfile
    volumes:
      - ./services/hasura/metadata:/hasura-metadata
      - ./services/hasura/migrations:/hasura-migrations
    ports:
      - ${EXPOSE_HASURA_PORT:-4201}:8080
    depends_on:
      - db
      - api
    environment:
      HASURA_GRAPHQL_INFER_FUNCTION_PERMISSIONS: "false"
      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET:-admin}
      HASURA_GRAPHQL_DATABASE_URL: postgres://dev:dev@db:5432/dev
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
      HASURA_GRAPHQL_DEV_MODE: "true"
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      # HASURA_GRAPHQL_JWT_SECRET: '{"jwk_url": "http://api:${API_PORT:-4200}/api/v1/oas/jwks","header":{"type":"Cookie","name":"bearer"},"claims_namespace":"${CLAIMS_NAMESPACE:-https://alertesecours.fr/claims}"}'
      HASURA_GRAPHQL_JWT_SECRET: '{"type":"EdDSA", "jwk_url": "http://api:${API_PORT:-4200}/api/v1/oas/jwks","claims_namespace":"${CLAIMS_NAMESPACE:-https://alertesecours.fr/claims}"}'
      HASURA_GRAPHQL_UNAUTHORIZED_ROLE: "anonymous"
      HASURA_REMOTE_SCHEMA_API_GRAPHQL_ENDPOINT: ${HASURA_REMOTE_SCHEMA_API_GRAPHQL_ENDPOINT:-http://api:4200/api/v1/graphql}
      API_PORT: ${API_PORT:-4200}

  hasura_console:
    image: helpme_hasura_console
    restart: always
    build:
      context: .
      dockerfile: ./services/hasura/console/Dockerfile
    volumes:
      - ./services/hasura:/hasura
    ports:
      - ${HASURA_CONSOLE_PORT:-4295}:${HASURA_CONSOLE_PORT:-4295}
      - ${HASURA_CONSOLE_API_PORT:-4293}:${HASURA_CONSOLE_API_PORT:-4293}
    depends_on:
      - db
      - api
      - hasura
    environment:
      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET:-admin}
      HASURA_CONSOLE_INTERNAL_HOSTNAME: ${HASURA_CONSOLE_INTERNAL_HOSTNAME:-hasura_console}
      HASURA_CONSOLE_PORT: ${HASURA_CONSOLE_PORT:-4295}
      HASURA_CONSOLE_API_PORT: ${HASURA_CONSOLE_API_PORT:-4293}
      HASURA_GRAPHQL_ENDPOINT: http://localhost:${EXPOSE_HASURA_PORT:-4201}
      HASURA_INTERNAL_HOSTNAME: ${HASURA_INTERNAL_HOSTNAME:-hasura}
      HASURA_EXPOSE_PORT: ${EXPOSE_HASURA_PORT:-4201}

  api:
    image: helpme_api:prod
    restart: always
    build:
      context: .
      dockerfile: ./services/api/Dockerfile.dev
    volumes:
      - ./.dev-secrets:/secrets:ro
      - ./:/app
    ports:
      - ${EXPOSE_API_PORT:-4200}:${API_PORT:-4200}
    environment:
      LOGLEVEL: ${API_LOGLEVEL:-debug}
      PORT: "${API_PORT:-4200}"
      JWK_FILE: /secrets/jwk.json
      OLDJWK_FILE: /secrets/jwk2.json
      DATABASE_URL: ${DATABASE_URL-psql://dev:dev@db/dev}
      HASURA_GRAPHQL_URL: ${HASURA_GRAPHQL_URL:-http://hasura:8080/v1/graphql}
      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET:-admin}
      NOMINATIM_URL: ${NOMINATIM_URL:-http://nominatim:8080}
      WHAT3WORDS_API_KEY: ${WHAT3WORDS_API_KEY-}
      LOG_REQUESTS: ${LOG_REQUESTS:-false}
      CLAIMS_NAMESPACE: ${CLAIMS_NAMESPACE:-https://hasura.io/jwt/claims}
      AMQP_URL: ${AMQP_URL-amqp://dev:dev@rabbitmq}
      AMQP_HOST: rabbitmq
      AMQP_PORT: 5672
      EXTERNAL_RINGOVER_CALL_EVENT_WEBHOOK_KEY: ${EXTERNAL_RINGOVER_CALL_EVENT_WEBHOOK_KEY:-}
      REDIS_QUEUE_DEDUP_HOST: redis-q-dedup
      REDIS_QUEUE_DEDUP_PASSWORD: redis-password
      REDIS_HOT_GEODATA_HOST: redis-hot-geodata
      REDIS_HOT_GEODATA_PASSWORD: redis-password
      SENTRY_DSN: ${SENTRY_DSN_API:-""}
      SENTRY_ENVIRONMENT: ${SENTRY_ENVIRONMENT:-development}
    depends_on:
      - db
      - rabbitmq
      - redis-hot-geodata
      - keydb-cold-geodata
      - redis-q-dedup

  files:
    image: helpme_file
    restart: always
    build:
      context: .
      dockerfile: ./services/files/Dockerfile.dev
    volumes:
      - ./.dev-secrets:/secrets:ro
      - ./:/app
    ports:
      - ${EXPOSE_FILES_PORT:-4292}:${FILES_PORT:-4292}
    environment:
      LOGLEVEL: ${FILES_LOGLEVEL:-debug}
      PORT: "${FILES_PORT:-4292}"
      JWK_FILE: /secrets/jwk.json
      OLDJWK_FILE: /secrets/jwk2.json
      DATABASE_URL: ${DATABASE_URL-psql://dev:dev@db/dev}
      MINIO_ENDPOINT: minio
      MINIO_PORT: 9000
      MINIO_ACCESS_KEY: ${MINIO_ROOT_USER:-minio-admin}
      MINIO_SECRET_KEY: ${MINIO_ROOT_PASSWORD:-minio-admin}
      HASURA_GRAPHQL_URL: ${HASURA_GRAPHQL_URL:-http://hasura:8080/v1/graphql}
      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET:-admin}
      LOG_REQUESTS: ${LOG_REQUESTS:-false}
      CLAIMS_NAMESPACE: ${CLAIMS_NAMESPACE:-https://hasura.io/jwt/claims}
      AMQP_URL: ${AMQP_URL-amqp://dev:dev@rabbitmq}
      AMQP_HOST: rabbitmq
      AMQP_PORT: 5672
      SENTRY_DSN: ${SENTRY_DSN_FILES:-""}
      SENTRY_ENVIRONMENT: ${SENTRY_ENVIRONMENT:-development}
    depends_on:
      - minio
      - db
      - rabbitmq

  tasks:
    image: helpme_tasks
    restart: always
    build:
      context: .
      dockerfile: ./services/tasks/Dockerfile.dev
    volumes:
      - ./:/app
    environment:
      API_URL: ${API_URL:-http://localhost:${EXPOSE_API_PORT:-4200}/api}
      LOGLEVEL: ${TASKS_LOGLEVEL:-debug}
      DATABASE_URL: ${DATABASE_URL-psql://dev:dev@db/dev}
      AMQP_URL: ${AMQP_URL-amqp://dev:dev@rabbitmq}
      AMQP_PORT: 5672
      HASURA_GRAPHQL_URL: ${HASURA_GRAPHQL_URL:-http://hasura:8080/v1/graphql}
      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET:-admin}
      NOMINATIM_URL: ${NOMINATIM_URL:-http://nominatim:8080}
      WHAT3WORDS_API_KEY: ${WHAT3WORDS_API_KEY-}
      GOOGLE_SERVICE_ACCOUNT_KEY_FILE: ${GOOGLE_SERVICE_ACCOUNT_KEY_FILE:-/app/services/tasks/src/googleServiceAccountKey.json}
      SMTP_USER: ""
      SMTP_PASS: ""
      SMTP_HOST: maildev
      SMTP_PORT: 1025
      SMTP_FROM: ""
      REDIS_QUEUE_DEDUP_HOST: redis-q-dedup
      REDIS_QUEUE_DEDUP_PASSWORD: redis-password
      REDIS_HOT_GEODATA_HOST: redis-hot-geodata
      REDIS_HOT_GEODATA_PASSWORD: redis-password
      KEYDB_COLD_GEODATA_HOST: keydb-cold-geodata
      KEYDB_COLD_GEODATA_PASSWORD: keydb-password
      SENTRY_DSN: ${SENTRY_DSN_TASKS:-""}
      SENTRY_ENVIRONMENT: ${SENTRY_ENVIRONMENT:-development}
    depends_on:
      - db
      - rabbitmq
      - redis-hot-geodata
      - keydb-cold-geodata
      - redis-q-dedup

  watchers:
    image: helpme_watchers
    restart: always
    build:
      context: .
      dockerfile: ./services/watchers/Dockerfile.dev
    volumes:
      - ./:/app
    environment:
      LOGLEVEL: ${WATCHERS_LOGLEVEL:-debug}
      DATABASE_URL: ${DATABASE_URL-psql://dev:dev@db/dev}
      AMQP_URL: ${AMQP_URL-amqp://dev:dev@rabbitmq}
      AMQP_PORT: 5672
      HASURA_GRAPHQL_URL: ${HASURA_GRAPHQL_URL:-http://hasura:8080/v1/graphql}
      HASURA_GRAPHQL_ADMIN_SECRET: ${HASURA_GRAPHQL_ADMIN_SECRET:-admin}
      REDIS_HOT_GEODATA_HOST: redis-hot-geodata
      REDIS_HOT_GEODATA_PASSWORD: redis-password
      KEYDB_COLD_GEODATA_HOST: keydb-cold-geodata
      KEYDB_COLD_GEODATA_PASSWORD: keydb-password
      SENTRY_DSN: ${SENTRY_DSN_WATCHERS:-""}
      SENTRY_ENVIRONMENT: ${SENTRY_ENVIRONMENT:-development}
    depends_on:
      - db
      - rabbitmq
      - redis-hot-geodata
      - keydb-cold-geodata

  osrm-car: &osrm
    image: osrm/osrm-backend:latest
    restart: always
    # image: helpme_osrm
    # build:
    # context: .
    # dockerfile: ./services/osrm/Dockerfile
    volumes:
      # - osrm-data:/data
      - ./osm-files:/data
    ports:
      - ${OSRM_CAR_PORT:-4261}:5000
    environment:
      ALLOW_CORS: enabled
      # UPDATES: enabled
    command: osrm-routed --algorithm mld /data/car/switzerland-latest.osrm
  osrm-foot:
    <<: *osrm
    command: osrm-routed --algorithm mld /data/foot/switzerland-latest.osrm
    ports:
      - ${OSRM_FOOT_PORT:-4262}:5000
  # osrm-bicycle:
  #   image: osrm/osrm-backend:latest
  #   restart: always
  #   # image: helpme_osrm
  #   # build:
  #     # context: .
  #     # dockerfile: ./services/osrm/Dockerfile
  #   volumes:
  #     # - osrm-data:/data
  #     - ./osm-files:/data
  #   ports:
  #     - ${OSRM_BICYCLE_PORT:-4263}:5000
  #   environment:
  #     ALLOW_CORS: enabled
  #     # UPDATES: enabled
  #   command: osrm-routed --algorithm mld /data/bicycle/switzerland-latest.osrm

  tileserver-gl:
    image: maptiler/tileserver-gl:v4.4.3
    restart: always
    volumes:
      - ./tileserver-files:/data
    ports:
      - ${TILESERVERGL_PORT:-4282}:8080

  nominatim:
    image: mediagis/nominatim:4.0
    restart: always
    ports:
      - ${NOMINATIM_PORT:-4283}:8080
    environment:
      # see https://github.com/mediagis/nominatim-docker/tree/master/4.0#configuration for more options
      # PBF_URL: https://download.geofabrik.de/europe/switzerland-latest.osm.pbf
      PBF_PATH: /data/switzerland-latest.osm.pbf
      REPLICATION_URL: https://download.geofabrik.de/europe/switzerland-updates/
      NOMINATIM_PASSWORD: very_secure_password
    volumes:
      - nominatim-data:/var/lib/postgresql/12/main
      - ./osm-files:/data
    shm_size: 1gb

  maildev:
    image: maildev/maildev:2.1.0
    ports:
      - "${MAILDEV_SMTP_PORT:-4225}:1025"
      - "${MAILDEV_WEB_PORT:-4226}:1080"
    restart: always

  web:
    image: helpme_web
    restart: always
    build:
      context: .
      dockerfile: ./services/web/Dockerfile
    ports:
      - ${WEB_PORT:-4203}:8080

  app:
    image: helpme_app
    restart: always
    build:
      context: .
      dockerfile: ./services/app/Dockerfile
    ports:
      - ${SERVICE_APP_PORT:-4209}:8080

# networks:
#   hostnet:
#     external: true
#     name: host

volumes:
  pg_data:
    name: helpme-pgdata
    labels:
      project: "alertesecours"
  minio_data:
    name: helpme-miniodata
    labels:
      project: "alertesecours"
  redis_hg_data:
    name: helpme-redishg-data
    labels:
      project: "alertesecours"
  keydb_cg_data:
    name: helpme-keydbcg-data
    labels:
      project: "alertesecours"
  rabbitmq_data:
    name: helpme-rabbitmq-data
    labels:
      project: "alertesecours"
  osm-data:
    name: helpme-osm-data
    labels:
      project: "alertesecours"
    # external: true
  osm-rendered-tiles:
    name: helpme-osm-rendered-tiles
    labels:
      project: "alertesecours"
    # external: true
  nominatim-data:
    name: helpme-nominatim-data
    labels:
      project: "alertesecours"
  # osrm-data:
  #   name: helpme-osrm-data
