FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder

ARG PACKAGE

ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy

RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    uv sync --frozen --no-dev --no-install-workspace --package ${PACKAGE}

WORKDIR /app

COPY pyproject.toml uv.lock ./
COPY packages ./packages
COPY apps ./apps

RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev --no-editable --package ${PACKAGE}

FROM python:3.12-slim-bookworm AS runtime

ARG PACKAGE

COPY --from=builder --chown=app:app /app/.venv /venv

ENV PATH="/venv/bin:$PATH"

COPY --from=builder --chown=app:app /app/apps/${PACKAGE}/api /api

EXPOSE 80

CMD ["python3", "-m", "uvicorn", "api.server:app", "--host", "0.0.0.0", "--port", "80"]
