version: '3.8'

networks:
  betting_duck_network:
    driver: bridge


services:

  nginx:
    image: nginx:latest
    container_name: nginx-server
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./frontend/dist:/static
      # SSL 설정
      - ./fullchain.pem:/ssl/fullchain.pem
      - ./privkey.pem:/ssl/privkey.pem
    depends_on:
      - betting_duck_app
    networks:
      - betting_duck_network
    profiles:
      - prod

  nginx_dev:
    image: nginx:latest
    container_name: nginx-server
    ports:
      - "80:80"
    volumes:
      - ./nginx.dev.conf:/etc/nginx/nginx.conf
      - ./frontend/dist:/static
    depends_on:
      - betting_duck_app_dev
    networks:
      - betting_duck_network
    profiles:
      - dev

  betting_duck_app:
    image: node:22.11.0-alpine
    container_name: betting_duck_app
    working_dir: /app/backend
    volumes:
      - ./:/app
    ports:
      - "${SERVER_PORT}:3000"
    # 배포용 커맨드
    command: >
      sh -c " npm install -g pnpm && \
      cd ../backend && pnpm install && \
      pnpm run start "
    environment:
      POSTGRES_HOSTNAME: ${POSTGRES_HOSTNAME}
      POSTGRES_PORT: ${POSTGRES_PORT}
      POSTGRES_DB_NAME: ${POSTGRES_DB_NAME}
      POSTGRES_USERNAME: ${POSTGRES_USERNAME}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB_NAME_DB_SYNCHRONIZE: ${POSTGRES_DB_NAME_DB_SYNCHRONIZE}
      REDIS_HOSTNAME: ${REDIS_HOSTNAME}
      REDIS_PORT: ${REDIS_PORT}
    depends_on:
      - postgres_db
      - redis
    networks:
      - betting_duck_network
    profiles:
      - prod

  betting_duck_app_dev:
    image: node:22.11.0-alpine
    container_name: betting_duck_app
    working_dir: /app/backend
    volumes:
      - ./:/app
    ports:
      - "${SERVER_PORT}:3000"
    # 테스트용 커맨드
    command: >
      sh -c " npm install -g pnpm && \
      cd ../backend && pnpm install && \
      tail -f /dev/null "
    environment:
      POSTGRES_HOSTNAME: ${POSTGRES_HOSTNAME}
      POSTGRES_PORT: ${POSTGRES_PORT}
      POSTGRES_DB_NAME: ${POSTGRES_DB_NAME}
      POSTGRES_USERNAME: ${POSTGRES_USERNAME}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB_NAME_DB_SYNCHRONIZE: ${POSTGRES_DB_NAME_DB_SYNCHRONIZE}
      REDIS_HOSTNAME: ${REDIS_HOSTNAME}
      REDIS_PORT: ${REDIS_PORT}
    depends_on:
      - postgres_db
      - redis
    networks:
      - betting_duck_network
    profiles:
      - dev


  postgres_db:
    image: postgres:15
    container_name: postgres_db
    environment:
      POSTGRES_USER: ${POSTGRES_USERNAME}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB_NAME}
    ports:
      - "${POSTGRES_PORT}:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      # - ./db-init.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - betting_duck_network

  redis:
    image: redis:latest
    container_name: redis
    ports:
      - "${REDIS_PORT}:6379"
    networks:
      - betting_duck_network

volumes:
  postgres_data:
