FROM python:3.11-slim-bookworm

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    DATA_DIR=/app/data

WORKDIR /app

# 中文字体：导出 PDF 需要。必须是 TrueType 轮廓的字体（文泉驿微米黑）；Noto CJK 这类 CFF 字体 reportlab 读不了。
RUN apt-get update \
 && apt-get install -y --no-install-recommends fonts-wqy-microhei \
 && rm -rf /var/lib/apt/lists/*

# 国内网络下载慢时，在 .env 里设置 PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple 即可（compose 会传进来）
ARG PIP_INDEX_URL=
COPY requirements.txt .
RUN pip install --no-cache-dir ${PIP_INDEX_URL:+--index-url "$PIP_INDEX_URL"} -r requirements.txt 'gunicorn>=22,<24'
COPY app.py .

EXPOSE 5000

# 健康检查：/healthz 不需要登录，只确认进程和数据库正常
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
  CMD python -c "import urllib.request;urllib.request.urlopen('http://127.0.0.1:5000/healthz',timeout=4)"

CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--worker-class", "gthread", "--workers", "2", "--threads", "4", "--timeout", "60", "--keep-alive", "5", "app:app"]
