FROM python:3.12-slim-bookworm

# Tesseract (French, English, page orientation) and Ghostscript for the scanned PDFs.
RUN apt-get update \
 && apt-get install -y --no-install-recommends tesseract-ocr tesseract-ocr-fra tesseract-ocr-eng tesseract-ocr-osd ghostscript \
 && rm -rf /var/lib/apt/lists/*

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

WORKDIR /app
COPY app/ /app/
COPY Dockerfile requirements.txt docker-compose.yml LICENSE /app/_source/
# AGPL: the code of the service is offered to its users.
RUN python -c "import zipfile,os; z=zipfile.ZipFile('/app/static/source.zip','w',zipfile.ZIP_DEFLATED); [z.write(os.path.join(r,f), os.path.relpath(os.path.join(r,f),'/app')) for r,_,fs in os.walk('/app') for f in fs if not f.endswith(('.zip','.pyc','.woff2'))]; z.close()" \
 && useradd -r -u 10001 pdfword && mkdir -p /data/jobs && chown -R pdfword /data

USER pdfword
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers", "--forwarded-allow-ips", "*", "--workers", "1"]
