# syntax=docker/dockerfile:1

# The client is static; Nuxt generate leaves .output/public.
FROM node:22-bookworm-slim AS web
WORKDIR /app/web
RUN npm install --global pnpm@11.22.0
COPY web/package.json web/pnpm-lock.yaml web/pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile
COPY web/ ./
RUN pnpm build

# The server consumes the Rhema crates. The clone is pinned so an image
# does not move when the framework's main branch does.
FROM rust:1.98.1-bookworm AS server
ARG RHEMA_REPOSITORY=https://github.com/vhaudiquet/rhema.git
ARG RHEMA_REF=v0.5.1
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates cmake git pkg-config \
    && rm -rf /var/lib/apt/lists/*
RUN git clone --depth 1 --branch "${RHEMA_REF}" "${RHEMA_REPOSITORY}" /rhema
WORKDIR /app
COPY Cargo.toml Cargo.lock ./
RUN sed -i 's#path = "../rhema/#path = "/rhema/#g' Cargo.toml
COPY src ./src
RUN cargo build --release

FROM debian:bookworm-slim
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd --gid 10001 together \
    && useradd --uid 10001 --gid 10001 --home-dir /data --shell /usr/sbin/nologin --no-create-home together \
    && install -d -o together -g together /data
WORKDIR /app
COPY --from=server /app/target/release/rhema-together /app/rhema-together
COPY --from=web /app/web/.output/public /app/web
ENV TOGETHER_LISTEN=0.0.0.0:7800 \
    TOGETHER_DATABASE=/data/together.db \
    TOGETHER_SECRET_KEY_FILE=/data/secret.key \
    TOGETHER_WEB_DIR=/app/web \
    HOME=/data \
    RUST_LOG=rhema_together=info,tower_http=warn
USER 10001:10001
VOLUME ["/data"]
EXPOSE 7800
ENTRYPOINT ["/app/rhema-together"]
