FROM rust:1.94 AS chef

RUN cargo install --locked cargo-chef
WORKDIR /app

COPY Cargo.toml Cargo.lock .
RUN mkdir src && touch src/main.rs

RUN cargo chef prepare --recipe-path recipe.json



FROM chef AS builder

COPY --from=chef /app/recipe.json recipe.json

RUN cargo chef cook --release --recipe-path recipe.json


COPY rust-toolchain.toml rust-toolchain.toml

RUN cargo fetch


COPY . .

RUN cargo build --release



FROM debian:bookworm-slim AS runtime

RUN useradd -m -s /bin/bash appuser

WORKDIR /app

COPY --from=builder /app/target/release/mdpreview /usr/local/bin/mdpreview

USER appuser

EXPOSE 8080

CMD ["mdpreview"]
