# syntax=docker/dockerfile:1.7
FROM rust:1-bookworm AS build
WORKDIR /app/src/components/backend-core
ARG CARGO_BUILD_JOBS=1
ARG CARGO_HTTP_TIMEOUT=180
ARG CARGO_HTTP_LOW_SPEED_LIMIT=1
ARG CARGO_NET_RETRY=5
ARG CARGO_HTTP_MULTIPLEXING=false
ARG CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
ENV CARGO_BUILD_JOBS=${CARGO_BUILD_JOBS} \
  CARGO_HTTP_TIMEOUT=${CARGO_HTTP_TIMEOUT} \
  CARGO_HTTP_LOW_SPEED_LIMIT=${CARGO_HTTP_LOW_SPEED_LIMIT} \
  CARGO_NET_RETRY=${CARGO_NET_RETRY} \
  CARGO_HTTP_MULTIPLEXING=${CARGO_HTTP_MULTIPLEXING} \
  CARGO_REGISTRIES_CRATES_IO_PROTOCOL=${CARGO_REGISTRIES_CRATES_IO_PROTOCOL}
COPY src/components/backend-core/Cargo.toml ./Cargo.toml
COPY src/components/backend-core/Cargo.lock ./Cargo.lock
RUN mkdir -p src \
  && printf 'fn main() {}\n' > src/main.rs \
  && cargo build --release --locked --jobs "${CARGO_BUILD_JOBS}" \
  && rm -rf src
COPY src/components/backend-core/src ./src
RUN rm -f target/release/backend-core target/release/backend-core.d \
  && find target/release/.fingerprint -maxdepth 1 -type d -name 'unidesk-backend-core-*' -exec rm -rf {} + \
  && cargo build --release --locked --jobs "${CARGO_BUILD_JOBS}"

FROM postgres:16-bookworm
RUN apt-get update \
  && apt-get install -y --no-install-recommends ca-certificates tar gzip \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
COPY --from=build /app/src/components/backend-core/target/release/backend-core /usr/local/bin/backend-core
ENTRYPOINT []
CMD ["backend-core"]
