FROM python AS builder

RUN \
    apt update && \
    apt -y upgrade && \
    apt install -y \
    build-essential \
    libldap2-dev \
    libsasl2-dev \
    libssl-dev

COPY uv.lock pyproject.toml /opt/canaille/

RUN pip install uv

COPY canaille /opt/canaille/canaille

WORKDIR /opt/canaille

RUN uv sync --all-extras --upgrade

# We build a python wheel to install on the definitive docker image
RUN uv build --wheel

# We create a list of all the requirements to install next to canaille on the definitive docker image
RUN uv pip compile pyproject.toml --all-extras -o requirements.txt 

WORKDIR /opt/canaille/dist

# We build these two modules here to avoid including build libraries on the definitive docker image
RUN pip wheel python-ldap uwsgi

FROM python:slim

# We need libxml2 for uwsgi to work
RUN \
    apt update && \
    apt -y upgrade && \
    apt install -y \
    libxml2

# I create a volume so that I can mount it in docker using -v ./database:/opt/canaille/database
RUN mkdir -p /opt/canaille/database

COPY --from=builder /opt/canaille/dist /opt/canaille/dist
COPY --from=builder /opt/canaille/requirements.txt /opt/canaille/requirements.txt

RUN pip install --find-links /opt/canaille/dist canaille python-ldap uwsgi
RUN pip install -r /opt/canaille/requirements.txt

COPY uwsgi.ini /opt/canaille

WORKDIR /opt/canaille

ENTRYPOINT ["uwsgi","--ini","uwsgi.ini"]