# 1. Build Stage
FROM rust:1.90 as builder

WORKDIR /usr/src/app

# Pre-build dependencies to leverage Docker layer caching
COPY ./services/config-service-rs/Cargo.toml ./services/config-service-rs/Cargo.lock* ./services/config-service-rs/
WORKDIR /usr/src/app/services/config-service-rs
RUN mkdir -p src && \
    echo "fn main() {}" > src/main.rs && \
    cargo build --release --bin config-service-rs

# Copy the full source code
COPY ./services/config-service-rs /usr/src/app/services/config-service-rs
COPY ./config /usr/src/app/config

# Build the application
WORKDIR /usr/src/app/services/config-service-rs
RUN cargo build --release --bin config-service-rs

# 2. Runtime Stage
FROM debian:bookworm-slim

WORKDIR /app

# Set timezone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Copy the built binary and the config directory from the builder stage
COPY --from=builder /usr/src/app/services/config-service-rs/target/release/config-service-rs /usr/local/bin/
COPY --from=builder /usr/src/app/config ./config

# Set the binary as the entrypoint
ENTRYPOINT ["/usr/local/bin/config-service-rs"]
