# Synor Hosting Gateway Docker Image
# Multi-stage build for optimized final image

FROM rust:1.75-slim-bookworm AS builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
    pkg-config \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /build

# Copy workspace files
COPY Cargo.toml Cargo.lock ./
COPY crates/ ./crates/

# Build with release optimizations
RUN cargo build --release -p synor-hosting --features server --bin hosting-gateway

# Runtime stage
FROM debian:bookworm-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user
RUN useradd -r -u 1000 synor

WORKDIR /app

# Copy binary from builder
COPY --from=builder /build/target/release/hosting-gateway /usr/local/bin/hosting-gateway

# Switch to non-root user
USER synor

# Default configuration
ENV LISTEN_ADDR=0.0.0.0:8080
ENV HOSTING_DOMAIN=synor.cc
ENV STORAGE_GATEWAY_URL=http://storage-gateway:80
ENV RATE_LIMIT=100
ENV RUST_LOG=info

EXPOSE 8080

# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8080/health || exit 1

ENTRYPOINT ["hosting-gateway"]
