version: '3.8'

services:
  postgres:
    image: postgres:14.1
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_DB=indexer
      - PGDATA=/data/postgres
      - POSTGRES_HOST_AUTH_METHOD=trust
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -q -U postgres -d indexer" ]
    ports:
      # deconflict with postgres that might be running already on
      # the host machine
      - "5433:5432"
    volumes:
      - postgres_data:/data/postgres
      - ./migrations:/docker-entrypoint-initdb.d/

  index:
    build:
      context: ..
      dockerfile: indexer/Dockerfile
    command: ["/bin/sh", "-c", "indexer migrate && indexer index"]
    expose:
      - "8100"
      - "7300"
    environment:
      - INDEXER_CONFIG=/app/indexer/config.toml
      - INDEXER_L1_RPC_URL=http://host.docker.internal:8545
      - INDEXER_L2_RPC_URL=http://host.docker.internal:9545
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_NAME=indexer
    volumes:
      - ./indexer.toml:/app/indexer/config.toml/:ro
      # needed only when running against the local devnet such
      # that it can bootstrap the local deployment addresses
      - ../go.mod:/app/go.mod/:ro
      - ../.devnet/addresses.json:/app/.devnet/addresses.json/:ro
    healthcheck:
      test: wget index:8100/healthz -q -O - > /dev/null 2>&1
    depends_on:
      postgres:
        condition: service_healthy

  api:
    build:
      context: ..
      dockerfile: indexer/Dockerfile
    command: ["indexer", "api"]
    environment:
      - INDEXER_CONFIG=/app/indexer/config.toml
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=postgres
      - DB_NAME=indexer
    ports:
      - "8100:8100"
    expose:
      - "7300"
    volumes:
      - ./indexer.toml:/app/indexer/config.toml/:ro
      # needed only when running against the local devnet such
      # that it can bootstrap the local deployment addresses
      - ../go.mod:/app/go.mod/:ro
      - ../.devnet/addresses.json:/app/.devnet/addresses.json/:ro
    healthcheck:
      test: wget api:8100/healthz -q -O - > /dev/null 2>&1
    depends_on:
      postgres:
        condition: service_healthy
        
  prometheus:
    image: prom/prometheus:latest
    expose:
      - "9090"
    volumes:
      - ./ops/prometheus:/etc/prometheus/:ro
      - prometheus_data:/prometheus
    depends_on:
      index:
        condition: service_healthy
      api:
        condition: service_healthy

  grafana:
    image: grafana/grafana:latest
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=optimism
      - GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH=/var/lib/grafana/dashboards/indexer.json
    ports:
      - "3000:3000"
    volumes:
      - ./ops/grafana/provisioning:/etc/grafana/provisioning/:ro
      - ./ops/grafana/dashboards:/var/lib/grafana/dashboards/:ro
      - grafana_data:/var/lib/grafana
    depends_on:
      prometheus:
        condition: service_started

volumes:
  postgres_data:
  prometheus_data:
  grafana_data:
