# syntax=docker/dockerfile:1
# Build from the website root: docker build -t getforma-site .
FROM node:24.22.0-bookworm-slim AS frontend
WORKDIR /app
COPY package.json package-lock.json .node-version .gitignore .dockerignore Dockerfile railway.json README.md playwright.config.mjs ./
RUN npm ci
COPY scripts ./scripts
COPY src ./src
COPY public ./public
COPY content ./content
COPY docs ./docs
COPY tests ./tests
COPY .github ./.github
COPY examples ./examples
COPY native ./native
RUN npm run build:all

FROM rust:1.93.1-bookworm AS native
ENV CARGO_BUILD_JOBS=3
WORKDIR /app/native
COPY native/Cargo.toml native/Cargo.lock ./
COPY native/src ./src
# Embed exactly the manifest, FMIR and bundles produced together above.
COPY --from=frontend /app/native/dist ./dist
RUN cargo build --release --locked

FROM debian:bookworm-slim AS runtime
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates curl \
    && rm -rf /var/lib/apt/lists/* \
    && useradd --uid 10001 --create-home --shell /usr/sbin/nologin forma
WORKDIR /app
COPY --from=native /app/native/target/release/getforma-native-site /usr/local/bin/getforma-native-site
COPY --from=frontend /app/dist ./dist
# Runtime-only content: never under the public dist or embedded native assets.
COPY --chown=10001:10001 native/private/article.txt /app/private/article.txt
ENV HOST=0.0.0.0 PORT=8080 SITE_DIST=/app/dist PAYWALL_ARTICLE_PATH=/app/private/article.txt
USER 10001:10001
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
    CMD curl --fail --silent "http://127.0.0.1:${PORT}/healthz" > /dev/null || exit 1
CMD ["getforma-native-site"]
