# 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)
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 .
RUN --mount=type=secret,id=dredd_core_token,required=true \
    GIT_CONFIG_COUNT=1 \
    GIT_CONFIG_KEY_0="url.http://x-access-token:$(cat /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 now serves itself (app/main.py mounts it at "/"; nothing
# stands in front of it). 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 .`.
# /frontend is exactly where main.py's default (two levels above app/main.py) looks.
COPY --from=frontend . /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)"
# Which proxy may set X-Forwarded-For / -Proto. On the platform that is Atrium's front door, pinned to
# Sygna's network in Atrium's compose.yaml; standalone the backend is published directly and nothing
# sits in front, so the default trusts loopback only (empty means the same — `:-` below treats empty
# as unset). Never `*`: with the port published, anyone could then forge the audit log's client IP.
ENV FORWARDED_ALLOW_IPS=
# Where this module is mounted on the platform. Empty by default, which is the standalone stack:
# Sygna at the root of its own host. Set ROOT_PATH=/sygna and the front door MUST
# strip that prefix (Atrium forwards `/sygna/*` without it) — uvicorn PREPENDS root_path to the request path, so
# a proxy that does not strip gives `/sygna/sygna/...` and 404s everything, and a proxy that
# strips without this flag makes every server-generated URL escape the prefix. One decision, two
# places, one variable. The HEALTHCHECK above needs no change: uvicorn prepends, Starlette strips.
ENV ROOT_PATH=
# `sh -c` so the variables expand at start-up; `exec` so uvicorn keeps PID 1 and gets SIGTERM.
# --no-server-header: nothing in front strips `Server` any more. These flags live ONLY here — no
# compose `command:` override, here or in Atrium's compose.yaml.
CMD ["sh", "-c", "exec uvicorn app.main:app --host 0.0.0.0 --port 8000 --root-path \"$ROOT_PATH\" --proxy-headers --forwarded-allow-ips \"${FORWARDED_ALLOW_IPS:-127.0.0.1}\" --no-server-header"]
