# 考培练系统完整部署配置
version: '3.8'

services:
  # MySQL数据库服务
  mysql:
    image: mysql:8.0.43
    container_name: kaopeilian-mysql
    restart: unless-stopped
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: Kaopeilian2025!@#
      MYSQL_DATABASE: kaopeilian
      MYSQL_CHARACTER_SET_SERVER: utf8mb4
      MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci
    ports:
      - "3307:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./考培练系统规划/数据库-里程碑备份/7-完成数据分析模块-20251016_075159.sql:/docker-entrypoint-initdb.d/init.sql
      - ./kaopeilian-backend/mysql.cnf:/etc/mysql/conf.d/mysql.cnf
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    command: --default-authentication-plugin=mysql_native_password
    networks:
      - kaopeilian-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

  # 后端API服务
  backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile
    container_name: kaopeilian-backend
    restart: unless-stopped
    env_file:
      - ./kaopeilian-backend/.env.production
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
    ports:
      - "8000:8000"
    volumes:
      - ./kaopeilian-backend/app:/app/app  # 代码热重载
      - /data/kaopeilian/uploads/demo:/app/uploads  # 迁移到数据盘
      - ./kaopeilian-backend/logs:/app/logs
      - ./kaopeilian-backend/secrets:/app/secrets:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
    networks:
      - kaopeilian-network
    depends_on:
      mysql:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s

  # 前端服务
  frontend:
    build:
      context: ./kaopeilian-frontend
      dockerfile: Dockerfile
      target: production
      args:
        - NODE_ENV=production
        - VITE_API_BASE_URL=https://aiedu.ireborn.com.cn
        - VITE_WS_BASE_URL=wss://aiedu.ireborn.com.cn
        - VITE_USE_MOCK_DATA=false
    container_name: kaopeilian-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - ./kaopeilian-frontend/docker/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./kaopeilian-frontend/docker/default.conf:/etc/nginx/conf.d/default.conf:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - kaopeilian-network
    depends_on:
      - backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Redis缓存服务
  redis:
    image: redis:7.2-alpine
    container_name: kaopeilian-redis
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - kaopeilian-network
    command: redis-server --appendonly yes --maxmemory 256mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3

  # Nginx反向代理和SSL终止
  nginx:
    image: nginx:1.25-alpine
    container_name: kaopeilian-nginx
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - /etc/letsencrypt:/etc/letsencrypt:ro
      - /var/www/certbot:/var/www/certbot:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - kaopeilian-network
      - kpl-dev-network
    depends_on:
      - frontend
      - backend
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:80/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # SSL证书续期服务
  certbot:
    image: certbot/certbot:latest
    container_name: kaopeilian-certbot
    restart: "no"
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt
      - /var/www/certbot:/var/www/certbot
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
    profiles:
      - ssl

# 网络配置
networks:
  kaopeilian-network:
    driver: bridge
    name: kaopeilian-network
  kpl-dev-network:
    external: true
    name: kpl-dev-network

# 数据卷配置
volumes:
  redis_data:
    driver: local
    name: kaopeilian-redis-data
  mysql_data:
    driver: local
    name: kaopeilian-mysql-data