# syntax=docker/dockerfile:1

# ── build stage: resolve dependencies, including the private one ─────────────
# `dredd-core` is a private repository on the Haski Gitea pinned by tag in requirements.txt, so installing it
# needs two things the runtime image should never have: `git`, and a credential.
#
# The credential arrives as a BuildKit SECRET, not a build argument. A build arg is baked into the
# image history — `docker history` prints it — so a pushed image would publish the token to anyone
# who can pull it. A secret mount exists only for the duration of the one RUN that uses it.
# `GIT_CONFIG_*` scopes the rewrite to that same process, so nothing is written to a gitconfig
# that could survive into a layer either.
#
# Build with:  DREDD_CORE_TOKEN=... docker compose build     (compose passes it as the secret)
# (The same build as Sygna's, which its CI runs end to end.)
FROM python:3.12-slim AS build

RUN apt-get update \
 && apt-get install -y --no-install-recommends git \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /build
COPY requirements.txt .
# `required=true` only fails when the secret is ABSENT — compose passes an unset DREDD_CORE_TOKEN as
# an EMPTY secret, which would otherwise surface much later as a confusing git auth error. Hence the
# `test -s`. `tr -d` drops a stray CR/LF from a pasted token (git would echo a key containing one).
RUN --mount=type=secret,id=dredd_core_token,required=true \
    test -s /run/secrets/dredd_core_token \
      || { echo "build secret dredd_core_token is empty - export DREDD_CORE_TOKEN" >&2; exit 1; }; \
    GIT_CONFIG_COUNT=1 \
    GIT_CONFIG_KEY_0="url.http://x-access-token:$(tr -d '\r\n' < /run/secrets/dredd_core_token)@77.216.148.202:3000/.insteadOf" \
    GIT_CONFIG_VALUE_0="http://77.216.148.202:3000/" \
    pip install --no-cache-dir --prefix=/install -r requirements.txt

# ── runtime stage: no git, no credential, no compiler ────────────────────────
FROM python:3.12-slim

COPY --from=build /install /usr/local

WORKDIR /app
COPY . .

# The static console, which this backend serves itself (app/main.py mounts it at "/"; there is no
# reverse proxy). It lives in the repo's frontend/, outside this build context, so it arrives as a
# NAMED build context: compose passes `additional_contexts: { frontend: ./frontend }`, and a bare
# build from backend/ needs `docker build --build-context frontend=../frontend .`.
COPY --from=frontend . /frontend
ENV PARAFA_FRONTEND_DIR=/frontend

RUN useradd -m app && mkdir -p /data && chown -R app /data /app
ENV DATA_DIR=/data
USER app

EXPOSE 8000
HEALTHCHECK --interval=15s --timeout=5s --retries=10 \
  CMD python -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://localhost:8000/api/v1/health').status==200 else 1)"
# The backend is the edge: docker-compose.yml publishes it directly, with no reverse proxy. So
# X-Forwarded-For is trusted only from 127.0.0.1 by default — anything else is a client's claim, and
# the audit log records the peer address. On the Dredd platform set FORWARDED_ALLOW_IPS to the
# front door's (Atrium backend's) address, never `*`.
# The path prefix the platform's front door strips before the request reaches us. Empty (the
# default) is the standalone stack, served at the origin root; ROOT_PATH=/parafa mounts this app
# under a path prefix of a shared origin. uvicorn PREPENDS it to the incoming path, so it pairs with
# the front door forwarding `/parafa/*` with the prefix stripped: set one without the other and you get either a doubled
# prefix (404 everywhere) or server-generated URLs that escape the prefix. app/config.py reads the
# same variable, so FastAPI's root_path agrees. The healthcheck below needs no prefix — uvicorn
# prepends, Starlette strips, so the un-prefixed path still matches the route either way.
ENV ROOT_PATH=
# --no-server-header: nothing stands in front of uvicorn, so it must not announce itself.
CMD ["sh", "-c", "exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --proxy-headers --forwarded-allow-ips \"${FORWARDED_ALLOW_IPS:-127.0.0.1}\" --root-path \"$ROOT_PATH\" --no-server-header"]
