FROM python:slim

WORKDIR /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc libc6-dev wget \
    && rm -rf /var/lib/apt/lists/* \
    && adduser --disabled-password --gecos '' user

COPY readflag.c /readflag.c
RUN touch /readflag && chown user:user /readflag

ENV VIRTUAL_ENV=/app/.venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY wsgi.py .
COPY src/ src/
COPY entrypoint.sh /entrypoint.sh

RUN mkdir archive \
    && chown user:user archive /home/user \
    && chmod 755 /entrypoint.sh

USER user

EXPOSE 5000

ENTRYPOINT ["/entrypoint.sh"]
CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "1", "--worker-class", "gthread", "--threads", "8", "wsgi:app"]
