# YouTube Scraper Dockerfile
FROM golang:1.21-alpine AS builder

# Install git and other build dependencies
RUN apk add --no-cache git

# Set working directory
WORKDIR /app

# Copy go mod files
COPY youtube-scraper/go.mod youtube-scraper/go.sum ./

# Download dependencies
RUN go mod download

# Copy source code
COPY youtube-scraper/main.go ./

# Build the YouTube scraper
RUN CGO_ENABLED=0 GOOS=linux go build -o youtube-scraper main.go

# Final stage
FROM alpine:latest

# Install ca-certificates for HTTPS requests
RUN apk --no-cache add ca-certificates

# Create non-root user
RUN addgroup -g 1001 -S scraper && \
    adduser -u 1001 -S scraper -G scraper

WORKDIR /app

# Copy the binary from builder stage
COPY --from=builder /app/youtube-scraper .

# Change ownership to non-root user
RUN chown scraper:scraper /app/youtube-scraper

# Switch to non-root user
USER scraper

# Expose port
EXPOSE 7857

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=20s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:7857/ || exit 1

# Run the YouTube scraper
CMD ["./youtube-scraper"]
