# A Python app, for a server rather than a laptop.
#
# Two stages so the runtime carries no build tooling: `uv` resolves and
# installs into /app/.venv, and the second stage copies that venv and the
# source next to it. The install is deliberately editable, which matters if
# the app reads a file from beside its package (an example config, a template
# directory): that path is true in a checkout and true here, and false in
# site-packages.
#
# Already in place at deploy/Dockerfile, with its ignore file beside it.
# 2-build-image.ps1 reads it out of a `git archive` of the production commit,
# so the recipe is versioned with the code it builds.
#
# The package is `app` and stays `app` — a project started from this repo
# renames nothing. Edit the two `uv sync` lines when you add extras.

ARG PYTHON=3.14-slim
ARG UV=0.9.17

FROM ghcr.io/astral-sh/uv:${UV} AS uv

# ------------------------------------------------------------------ build

FROM python:${PYTHON} AS build
COPY --from=uv /uv /usr/local/bin/uv

ENV UV_LINK_MODE=copy \
    UV_COMPILE_BYTECODE=1 \
    UV_PYTHON_DOWNLOADS=never

WORKDIR /app

# Dependencies first and on their own layer, keyed on the lock file alone, so
# editing a Python file rebuilds in seconds rather than re-resolving the tree.
COPY pyproject.toml uv.lock README.md ./
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev --no-install-project

COPY app app
# The two module folders this repo ships beside the app, here and again in the
# runtime stage below. `pyproject.toml` lists `pipeline*` and `auth*` in its
# packages as well, or the install leaves them out.
COPY pipeline pipeline
# The door. A project that does not want one drops this line *and* the
# `auth.connect` call in `app/main.py` together: dropping the folder and
# leaving the call is a container that fails at startup, and dropping the call
# and leaving the folder is a container that starts with the door open.
COPY auth auth
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --frozen --no-dev

# ---------------------------------------------------------------- runtime

FROM python:${PYTHON}

# Nothing is built here, so nothing but the interpreter's own runtime is
# needed. `tini` is the one addition: a web server's shutdown and a worker
# pool's stop both hang off SIGTERM, and PID 1 without an init reaps no
# children and forwards no signals, so a `docker compose down` would wait the
# full ten seconds and then kill the workers mid-job.
RUN apt-get update \
 && apt-get install -y --no-install-recommends tini \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY --from=build /app/.venv /app/.venv
COPY app app
COPY pipeline pipeline
# The login page and the two scripts are files inside this folder, served from
# routes; see the build stage above.
COPY auth auth

ENV PATH=/app/.venv/bin:$PATH \
    PYTHONUNBUFFERED=1 \
    APP_CONFIG=/data/config.yaml \
    HOME=/data/home

# In the volume, not the image: whatever uid the compose file runs as owns
# /data, and a CLI the app shells out to — the `claude` binary, for one —
# wants somewhere to write. An image-baked /home/app would be right for uid
# 1000 and unwritable for anyone else.
VOLUME /data

EXPOSE 8787

# A session-free probe that checks the application can still read its store.
# `docker compose up --wait` in deploy.sh waits on exactly this, so a probe
# that only proves the port is open buys a deployment that reports success
# while the database is unreachable.
HEALTHCHECK --interval=60s --timeout=5s --start-period=20s --retries=3 \
    CMD python -c "import urllib.request as u; u.urlopen('http://127.0.0.1:8787/health', timeout=4)"

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["app", "serve", "--host", "0.0.0.0", "--port", "8787"]
