# Build stage
FROM rust:1-bookworm as builder

WORKDIR /usr/src/rikidown

# Install build dependencies required for git2 and openssl
RUN apt-get update && apt-get install -y \
    cmake \
    pkg-config \
    libssl-dev \
    zlib1g-dev \
    && rm -rf /var/lib/apt/lists/*

# Copy manifests to build dependencies first for caching
COPY Cargo.toml Cargo.lock ./

# Create a dummy main.rs to build dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs
RUN cargo build --release

# Copy the actual source code
COPY src ./src

# Touch the main file to force a rebuild of the application code
RUN touch src/main.rs
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim

WORKDIR /app

# Install runtime dependencies
# ca-certificates is required for git cloning over HTTPS
# libssl is required for the linked openssl dependency
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    && rm -rf /var/lib/apt/lists/*

# Copy the compiled binary from the builder stage
COPY --from=builder /usr/src/rikidown/target/release/rikidown /usr/local/bin/rikidown

# Expose the default listening port
EXPOSE 8080

# Set the entrypoint
ENTRYPOINT ["rikidown"]
