# 考培练系统 SaaS 超级管理后台 Docker Compose 配置
# 
# 启动命令：
#   cd /root/aiedu && docker compose -f docker-compose.admin.yml up -d
#
# 重新构建后端：
#   docker compose -f docker-compose.admin.yml build --no-cache kaopeilian-admin-backend
#
# 服务说明：
#   - kaopeilian-admin-frontend: 管理后台前端 (端口 3030)
#   - kaopeilian-admin-backend: 管理后台后端 (端口 8030)
#
# 域名：admin.kpl.ireborn.com.cn
#
# 注意：敏感配置从 .env.admin 文件读取

name: kaopeilian-admin

services:
  # ============================================
  # 管理后台前端
  # ============================================
  kaopeilian-admin-frontend:
    build:
      context: ./kaopeilian-admin-frontend
      dockerfile: Dockerfile
    image: kaopeilian-admin-frontend:1.0.0
    container_name: kaopeilian-admin-frontend
    restart: unless-stopped
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "127.0.0.1:3030:80"  # 仅本地访问，通过 Nginx 反向代理
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    networks:
      - kaopeilian-network
    depends_on:
      kaopeilian-admin-backend:
        condition: service_started
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:80/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 256M
        reservations:
          cpus: '0.1'
          memory: 64M

  # ============================================
  # 管理后台后端（开发环境 - 热重载）
  # ============================================
  kaopeilian-admin-backend:
    build:
      context: ./kaopeilian-backend
      dockerfile: Dockerfile.admin
    image: kaopeilian-admin-backend:1.0.0
    container_name: kaopeilian-admin-backend
    restart: unless-stopped
    env_file:
      - .env.admin
    environment:
      - TZ=Asia/Shanghai
      - PYTHONPATH=/app
    ports:
      - "127.0.0.1:8030:8000"  # 仅本地访问，通过 Nginx 反向代理
    volumes:
      # 代码热重载 - 挂载整个 app 目录
      - ./kaopeilian-backend/app:/app/app:rw
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    # 开发模式命令 - 启用热重载
    command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--reload-dir", "/app/app"]
    networks:
      - kaopeilian-network
      - prod-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '0.25'
          memory: 256M

networks:
  kaopeilian-network:
    external: true
    name: kaopeilian-network
  prod-network:
    external: true
    name: prod-network
