version: "3.9"

services:
  transit-planner:
    image: registry.metro.example/transit/planner-api:2.8.1
    restart: unless-stopped
    ports:
      - "8080:8080"
    environment:
      APP_ENV: production
      LOG_LEVEL: info
      HTTP_PORT: "8080"
      DATABASE_URL: postgresql://planner:${DB_PASSWORD}@postgres:5432/transit
      REDIS_URL: redis://redis:6379/0
      GTFS_SOURCE_URL: https://data.metro.example/gtfs/current.zip
      GTFS_REFRESH_CRON: "15 3 * * *"
      DEFAULT_TIMEZONE: America/Toronto
      DEFAULT_LOCALE: en-CA
      ROUTING_MAX_TRANSFERS: "3"
      ROUTING_WALKING_SPEED_KPH: "4.8"
      ROUTING_MAX_WALK_DISTANCE_METERS: "1200"
      REALTIME_ENABLED: "true"
      REALTIME_VEHICLE_POSITIONS_URL: https://realtime.metro.example/vehicle-positions.pb
      REALTIME_TRIP_UPDATES_URL: https://realtime.metro.example/trip-updates.pb
      HEALTHCHECK_PATH: /health
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - gtfs-data:/var/lib/transit/gtfs
    healthcheck:
      test:
        - CMD
        - wget
        - --quiet
        - --tries=1
        - --spider
        - http://localhost:8080/health
      interval: 30s
      timeout: 5s
      retries: 3
      start_period: 20s
    deploy:
      resources:
        limits:
          cpus: "2.0"
          memory: 2G
        reservations:
          cpus: "0.5"
          memory: 512M

  postgres:
    image: postgis/postgis:16-3.4
    restart: unless-stopped
    environment:
      POSTGRES_DB: transit
      POSTGRES_USER: planner
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test:
        - CMD-SHELL
        - pg_isready -U planner -d transit
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7.2-alpine
    restart: unless-stopped
    command:
      - redis-server
      - --appendonly
      - "yes"
      - --maxmemory
      - 512mb
      - --maxmemory-policy
      - allkeys-lru
    volumes:
      - redis-data:/data
    healthcheck:
      test:
        - CMD
        - redis-cli
        - ping
      interval: 10s
      timeout: 3s
      retries: 5

  gtfs-importer:
    image: registry.metro.example/transit/gtfs-importer:1.6.0
    restart: "no"
    environment:
      DATABASE_URL: postgresql://planner:${DB_PASSWORD}@postgres:5432/transit
      GTFS_SOURCE_URL: https://data.metro.example/gtfs/current.zip
      VALIDATE_FEED: "true"
      RETAIN_IMPORTS: "5"
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - gtfs-data:/var/lib/transit/gtfs
    profiles:
      - maintenance

volumes:
  postgres-data:
  redis-data:
  gtfs-data: