services:
  database:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: photolibrary
      POSTGRES_USER: photolibrary
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    volumes:
      - database:/var/lib/postgresql/data
    secrets:
      - db_password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U photolibrary -d photolibrary"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - redis:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  photolibrary:
    image: ghcr.io/example/photolibrary:1.12.3
    restart: unless-stopped
    depends_on:
      database:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - "8080:8080"
    environment:
      APP_ENV: production
      APP_URL: https://photos.example.com
      DATABASE_URL: postgresql://photolibrary@database:5432/photolibrary
      DATABASE_PASSWORD_FILE: /run/secrets/db_password
      REDIS_URL: redis://redis:6379/0
      PHOTO_STORAGE_PATH: /data/photos
      THUMBNAIL_STORAGE_PATH: /data/thumbnails
      MAX_UPLOAD_SIZE: 10G
      WORKER_CONCURRENCY: "4"
      ENABLE_FACE_RECOGNITION: "true"
      ENABLE_REVERSE_GEOCODING: "true"
      TZ: America/Toronto
    volumes:
      - /srv/photos/library:/data/photos
      - thumbnails:/data/thumbnails
      - /srv/photos/import:/data/import:ro
    secrets:
      - db_password
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://localhost:8080/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  worker:
    image: ghcr.io/example/photolibrary:1.12.3
    restart: unless-stopped
    command: ["worker"]
    depends_on:
      photolibrary:
        condition: service_healthy
    environment:
      APP_ENV: production
      DATABASE_URL: postgresql://photolibrary@database:5432/photolibrary
      DATABASE_PASSWORD_FILE: /run/secrets/db_password
      REDIS_URL: redis://redis:6379/0
      PHOTO_STORAGE_PATH: /data/photos
      THUMBNAIL_STORAGE_PATH: /data/thumbnails
      WORKER_CONCURRENCY: "4"
      TZ: America/Toronto
    volumes:
      - /srv/photos/library:/data/photos
      - thumbnails:/data/thumbnails
    secrets:
      - db_password

volumes:
  database:
  redis:
  thumbnails:

secrets:
  db_password:
    file: ./secrets/database-password.txt