# The built dictionary is gitignored. Produce it here so a clean checkout can build the image.
FROM node:24-bookworm-slim AS dictionary
WORKDIR /src
ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
RUN corepack enable && corepack prepare pnpm@10.13.1 --activate
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY packages/bengali-spell-checker ./packages/bengali-spell-checker
COPY apps/web/package.json ./apps/web/package.json
COPY apps/vscode/package.json ./apps/vscode/package.json
COPY dictionaries ./dictionaries
RUN pnpm install --frozen-lockfile --ignore-scripts --filter root
RUN pnpm --filter bengali-spell-checker build:dictionary
FROM rust:1.100-slim-bookworm AS builder
WORKDIR /src
COPY Cargo.toml Cargo.lock ./
COPY crates ./crates
RUN cargo build --release --locked -p bnspell-api
FROM debian:bookworm-slim AS runtime
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/* \
&& useradd --system --uid 65532 --user-group --create-home --home-dir /opt/bnspell --shell /usr/sbin/nologin bnspell
COPY --from=builder /src/target/release/bnspell-api /usr/local/bin/bnspell-api
COPY --from=dictionary --chown=bnspell:bnspell /src/packages/bengali-spell-checker/dictionary /opt/bnspell/dictionary
ENV HOST=0.0.0.0
ENV PORT=8787
ENV BNSPELL_DICT=/opt/bnspell/dictionary
EXPOSE 8787
USER bnspell
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD curl -fsS "http://127.0.0.1:${PORT}/health" || exit 1
ENTRYPOINT ["/usr/local/bin/bnspell-api"]