FROM python:3.13-slim AS builder

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1
WORKDIR /build

COPY pyproject.toml README.md README.fa.md LICENSE CHANGELOG.md ./
COPY enterpipelite ./enterpipelite
RUN python -m pip install --upgrade pip build \
    && python -m build --wheel \
    && mkdir /wheelhouse \
    && python -m pip wheel --wheel-dir /wheelhouse "./dist/enterpipelite-"*.whl "uvicorn>=0.30.0,<1"

FROM python:3.13-slim AS runtime

ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1

RUN groupadd --gid 10001 enterpipe \
    && useradd --uid 10001 --gid 10001 --create-home --shell /usr/sbin/nologin enterpipe

WORKDIR /app
COPY --from=builder /wheelhouse /wheelhouse
RUN python -m pip install --no-index --find-links=/wheelhouse "enterpipelite[server]==1.0.0a2" \
    && rm -rf /wheelhouse
COPY templates/container_app/app ./app

USER 10001:10001
EXPOSE 8000
STOPSIGNAL SIGTERM

CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1", "--lifespan", "on", "--timeout-graceful-shutdown", "20"]
