FROM lukemathwalker/cargo-chef:latest-rust-1.78 AS chef
WORKDIR /build

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /build/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release --bin server

FROM gcr.io/distroless/cc-debian12 AS runner

COPY --from=builder /build/target/release/server /app/server

USER nonroot:nonroot
WORKDIR /app

EXPOSE 8080
CMD ["/app/server"]
