version: '3.8'

services:
  # 前端应用
  frontend:
    build:
      context: .
      target: production
    ports:
      - "80:80"
    environment:
      - NODE_ENV=production
    restart: unless-stopped
    networks:
      - jiebanke-network
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "https://admin.jiebanke.com/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # 开发环境前端
  frontend-dev:
    build:
      context: .
      target: development
    ports:
      - "5173:5173"
    volumes:
      - .:/app
      - /app/node_modules
    environment:
      - NODE_ENV=development
    restart: unless-stopped
    networks:
      - jiebanke-network
    command: npm run dev -- --host

  # 后端API服务
  backend:
    build:
      context: ../backend
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - DB_HOST=mysql
      - DB_PORT=3306
      - DB_NAME=jiebanke
      - DB_USER=root
      - DB_PASSWORD=password
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - RABBITMQ_HOST=rabbitmq
      - RABBITMQ_PORT=5672
    depends_on:
      - mysql
      - redis
      - rabbitmq
    restart: unless-stopped
    networks:
      - jiebanke-network

  # MySQL数据库
  mysql:
    image: mysql:8.0
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=jiebanke
      - MYSQL_USER=jiebanke
      - MYSQL_PASSWORD=password
    volumes:
      - mysql_data:/var/lib/mysql
      - ../backend/scripts/init.sql:/docker-entrypoint-initdb.d/init.sql
    restart: unless-stopped
    networks:
      - jiebanke-network

  # Redis缓存
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    restart: unless-stopped
    networks:
      - jiebanke-network

  # RabbitMQ消息队列
  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      - RABBITMQ_DEFAULT_USER=admin
      - RABBITMQ_DEFAULT_PASS=password
    restart: unless-stopped
    networks:
      - jiebanke-network

volumes:
  mysql_data:

networks:
  jiebanke-network:
    driver: bridge