# =============================================================================
# bibimbap 로컬 개발 스택 (PostgreSQL + 앱)
#   - 비밀값은 .env 에서 주입(.env 는 gitignore, .env.example 만 추적).
#   - db: 최초 기동 시 db/schema.sql 을 docker-entrypoint-initdb.d 로 1회 실행.
#   - app: SPRING_DATASOURCE_* 환경변수로 db 접속(이미지/깃에 비밀값 비포함).
# 사용:  cp .env.example .env  &&  docker compose up --build
# =============================================================================

services:
  db:
    image: postgres:16
    container_name: bibimbap-db
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    ports:
      - "${DB_PORT:-5432}:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
      # 최초 init(빈 데이터 볼륨)에서만 실행됨. 스키마 변경 시 볼륨 삭제 후 재기동 필요.
      - ./db/schema.sql:/docker-entrypoint-initdb.d/01-schema.sql:ro
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 10

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: bibimbap-app
    depends_on:
      db:
        condition: service_healthy
    environment:
      # Spring relaxed binding: SPRING_DATASOURCE_URL -> spring.datasource.url 등.
      SPRING_DATASOURCE_DRIVER_CLASS_NAME: org.postgresql.Driver
      SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/${POSTGRES_DB}?currentSchema=${APP_SCHEMA:-dev}
      SPRING_DATASOURCE_USERNAME: ${POSTGRES_USER}
      SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD}
      # 업로드 저장 경로(영속 볼륨). app.upload.game-storage-path 매핑.
      APP_UPLOAD_GAME_STORAGE_PATH: /app/uploads/game
    ports:
      - "${APP_PORT:-8080}:8080"
    volumes:
      - uploads:/app/uploads

volumes:
  pgdata:
  uploads:
