version: '3.8'

services:
  # PostgreSQL 데이터베이스 (SSD 최적화)
  database:
    image: postgres:15-alpine
    container_name: document-server-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: document_server
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: ${DB_PASSWORD:-postgres123}
      POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C"
    volumes:
      # SSD: 데이터베이스 성능 최적화
      - /volume1/docker/document-server/database:/var/lib/postgresql/data
      - /volume1/docker/document-server/config/postgresql.conf:/etc/postgresql/postgresql.conf:ro
    ports:
      - "24101:5432"
    command: >
      postgres
      -c config_file=/etc/postgresql/postgresql.conf
      -c shared_buffers=8GB
      -c effective_cache_size=24GB
      -c work_mem=256MB
      -c maintenance_work_mem=2GB
      -c checkpoint_completion_target=0.9
      -c wal_buffers=64MB
      -c random_page_cost=1.1
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - document-network

  # Redis 캐시 (SSD 최적화)
  redis:
    image: redis:7-alpine
    container_name: document-server-redis
    restart: unless-stopped
    volumes:
      # SSD: 캐시 성능 최적화
      - /volume1/docker/document-server/redis:/data
    ports:
      - "24103:6379"
    command: >
      redis-server
      --maxmemory 4gb
      --maxmemory-policy allkeys-lru
      --save 900 1
      --save 300 10
      --save 60 10000
      --appendonly yes
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - document-network

  # FastAPI 백엔드
  backend:
    build: 
      context: ./backend
      dockerfile: Dockerfile
    container_name: document-server-backend
    restart: unless-stopped
    environment:
      - DATABASE_URL=postgresql://postgres:${DB_PASSWORD:-postgres123}@database:5432/document_server
      - REDIS_URL=redis://redis:6379
      - JWT_SECRET_KEY=${JWT_SECRET_KEY:-your-super-secret-jwt-key-change-this-in-production}
      - ENVIRONMENT=production
      - LOG_LEVEL=INFO
    volumes:
      # SSD: 로그 및 설정 (빠른 액세스)
      - /volume1/docker/document-server/logs:/app/logs
      - /volume1/docker/document-server/config:/app/config
      # HDD: 대용량 파일 저장 (비용 효율적)
      - /volume2/document-storage/uploads:/app/uploads
      - /volume2/document-storage/documents:/app/documents
    ports:
      - "24102:8000"
    depends_on:
      database:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - document-network

  # Nginx 웹서버
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    container_name: document-server-nginx
    restart: unless-stopped
    volumes:
      # SSD: 설정 및 캐시 (빠른 액세스)
      - /volume1/docker/document-server/nginx:/etc/nginx/conf.d
      - /volume1/docker/document-server/logs/nginx:/var/log/nginx
      # HDD: 정적 파일 서빙 (읽기 전용)
      - /volume2/document-storage/documents:/usr/share/nginx/html/documents:ro
      - ./frontend:/usr/share/nginx/html:ro
    ports:
      - "24100:80"
    depends_on:
      backend:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    networks:
      - document-network

networks:
  document-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

volumes:
  # 명시적 볼륨 정의 (시놀로지 경로 매핑)
  database_data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /volume1/docker/document-server/database
  
  redis_data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /volume1/docker/document-server/redis
  
  document_storage:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /volume2/document-storage

