FROM python:3.12-slim

WORKDIR /app

# 系统依赖（asyncpg 编译需要 gcc）
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc libpq-dev \
    && rm -rf /var/lib/apt/lists/*

# 先拷贝依赖清单，利用 Docker 层缓存
COPY server/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 拷贝全部后端代码
COPY server/ .

EXPOSE 8000

# 生产模式：Gunicorn 多进程管理 + UvicornWorker 异步处理
# -w 4: 4 个 Worker 进程充分利用多核
# --timeout 600: 兼容 AI SSE 流式长请求
# --graceful-timeout 30: 平滑重启时等待旧连接结束
CMD ["gunicorn", "app.main:app", \
     "-k", "uvicorn.workers.UvicornWorker", \
     "-w", "4", \
     "--bind", "0.0.0.0:8000", \
     "--timeout", "600", \
     "--graceful-timeout", "30", \
     "--access-logfile", "-"]
