## Docker Compose configuration for Dereth Tracker microservices
version: "3.8"

services:
  # Application service: Dereth Tracker API and WebSockets server
  dereth-tracker:
    build: .
    ports:
      - "127.0.0.1:8765:8765"
    depends_on:
      - db
    volumes:
      - "./main.py:/app/main.py"
      - "./db_async.py:/app/db_async.py"
      - "./static:/app/static"
      - "./alembic:/app/alembic"
      - "./alembic.ini:/app/alembic.ini"
    environment:
      # Database connection URL for TimescaleDB (built from POSTGRES_PASSWORD)
      DATABASE_URL: "postgresql://postgres:${POSTGRES_PASSWORD}@db:5432/dereth"
      # Override application settings as needed
      DB_MAX_SIZE_MB: "${DB_MAX_SIZE_MB}"
      DB_RETENTION_DAYS: "${DB_RETENTION_DAYS}"
      DB_MAX_SQL_LENGTH: "${DB_MAX_SQL_LENGTH}"
      DB_MAX_SQL_VARIABLES: "${DB_MAX_SQL_VARIABLES}"
      DB_WAL_AUTOCHECKPOINT_PAGES: "${DB_WAL_AUTOCHECKPOINT_PAGES}"
      SHARED_SECRET: "${SHARED_SECRET}"
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # TimescaleDB service for telemetry data storage
  db:
    image: timescale/timescaledb:2.19.3-pg14
    container_name: dereth-db
    environment:
      POSTGRES_DB: dereth
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
    volumes:
      - timescale-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 30s
      timeout: 5s
      retries: 5
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
  # Grafana service for visualization and dashboards
  grafana:
    image: grafana/grafana:latest
    container_name: dereth-grafana
    ports:
      - "127.0.0.1:3000:3000"
    depends_on:
      - db
    environment:
      # Grafana admin settings
      GF_SECURITY_ADMIN_PASSWORD: "${GF_SECURITY_ADMIN_PASSWORD}"
      # Allow embedding Grafana dashboards in iframes
      GF_SECURITY_ALLOW_EMBEDDING: "true"
      # Enable anonymous access so embedded panels work without login
      GF_AUTH_ANONYMOUS_ENABLED: "true"
      GF_AUTH_ANONYMOUS_ORG_ROLE: "Viewer"
      GF_USERS_ALLOW_SIGN_UP: "false"
      # Serve Grafana under /grafana path
      GF_SERVER_ROOT_URL: "https://overlord.snakedesert.se/grafana"
      GF_SERVER_SERVE_FROM_SUB_PATH: "true"
      # Postgres password for provisioning datasource
      POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
    volumes:
      # Provisioning directories for automated data source and dashboards
      - ./grafana/provisioning:/etc/grafana/provisioning
      - ./grafana/dashboards:/var/lib/grafana/dashboards
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
 
volumes:
  timescale-data: