FROM python:3.12-slim AS builder

# 修改APT源为传入的镜像

RUN apt-get update && apt-get install -y gnupg2
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3B4FE6ACC0B21F32 871920D1991BC93C
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc g++ musl-dev curl libffi-dev gfortran libopenblas-dev \
    poppler-utils \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir poetry -i https://pypi.tuna.tsinghua.edu.cn/simple


# Add Rust to PATH
ENV PATH="/root/.cargo/bin:${PATH}"

RUN mkdir -p /app/py
WORKDIR /app/py
COPY pyproject.toml /app/py/pyproject.toml

#RUN poetry config repositories.pypi https://mirrors.aliyun.com/pypi/simple/
#RUN export POETRY_PYPI_REPOSITORIES="https://pypi.tuna.tsinghua.edu.cn/simple"

# Install dependencies
RUN poetry config virtualenvs.create false \
    && poetry install --extras "core ingestion-bundle" --only main --no-root \
    && pip install --no-cache-dir gunicorn uvicorn -i https://pypi.tuna.tsinghua.edu.cn/simple

# Create the final image
FROM python:3.12-slim

# Install runtime dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl poppler-utils \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Add poppler to PATH
ENV PATH="/usr/bin:${PATH}"

# Debugging steps
RUN echo "PATH: $PATH"
RUN which pdfinfo
RUN pdfinfo -v

WORKDIR /app

COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Expose the port and set environment variables
ARG R2R_PORT=8000 R2R_HOST=0.0.0.0
ENV R2R_PORT=$R2R_PORT R2R_HOST=$R2R_HOST
EXPOSE $R2R_PORT

COPY . /app
# Copy the application and config
COPY core /app/core
COPY r2r /app/r2r
COPY shared /app/shared
COPY r2r.toml /app/r2r.toml
COPY pyproject.toml /app/pyproject.toml

# Run the application
CMD ["sh", "-c", "uvicorn core.main.app_entry:app --host $R2R_HOST --port $R2R_PORT"]
