# 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 . .

# uid 1000 spelled out: the certbot sidecar's deploy hook (deploy/tls/certbot.sh) hands the TLS key to
# exactly this uid, so it is a contract with the deploy kit, not an accident of useradd.
RUN useradd -m -u 1000 app && chown -R app /app
USER app

# The console ships IN the image: Atrium serves it itself (there is no proxy to serve it from).
# It comes from the build's second context, `frontend` - compose passes
# `additional_contexts: { frontend: ../frontend }`; by hand:
#   docker build --build-context frontend=../frontend backend
COPY --from=frontend . /frontend
ENV ATRIUM_FRONTEND_DIR=/frontend

EXPOSE 8000
# HTTPS, only when TLS=on (app/serve.py, deploy/compose.tls.yaml publishes it as :443).
EXPOSE 8443
# Behind the front door Atrium's API lives at /atrium/api/v1; the standalone stack
# (docker-compose.yml) still runs app.main at the root, so the probe tries both.
HEALTHCHECK --interval=15s --timeout=5s --retries=10   CMD python -c "import urllib.request as u; u.urlopen('http://localhost:8000/atrium/api/v1/health', timeout=2)"    || python -c "import urllib.request as u; u.urlopen('http://localhost:8000/api/v1/health', timeout=2)"
# Atrium IS the platform's edge (app/frontdoor.py): `/` and the trailing-slash redirects, Atrium
# mounted at /atrium, every other module streamed to its backend from FRONTDOOR_UPSTREAMS.
#   --no-proxy-headers  nothing is in front of this process, so X-Forwarded-For is a client's claim:
#                       the peer address is the client, and that is what client_ip() and the audit
#                       log record. (The front door drops inbound X-Forwarded-* and sets them anew
#                       for the modules, which trust them only from this container.)
#   --no-server-header  no `Server` header naming the software on the platform's edge.
#   --http h11          the parser that hands the request target over AS RECEIVED. httptools (what
#                       uvicorn[standard] makes `auto` pick) reduces an absolute-form target
#                       (`GET http://evil.example/galena/`) to its path before the app sees it, so
#                       frontdoor.check_target could never refuse it; with h11 it does (400).
#   --timeout-keep-alive 5, --limit-concurrency  an idle or flooding client cannot pin a worker; past
#                       the limit uvicorn answers 503 instead of queueing without bound.
# No --root-path: Mount("/atrium") sets Atrium's root_path in-process. ROOT_PATH must be empty or
# /atrium (build() refuses anything else).
# app/serve.py builds exactly that uvicorn command line. TLS off (the default) it EXECs the one uvicorn
# on :8000 and is gone; TLS=on it supervises two (plain :8000 and HTTPS :8443) and exits when the
# certbot sidecar renews the certificate, so the restart policy reloads it (deploy/RUNBOOK-tls.md).
ENV WEB_WORKERS=2 WEB_CONCURRENCY_LIMIT=1000
CMD ["python", "-m", "app.serve"]
