# Build stage
FROM golang:1.23-alpine AS builder

RUN apk add --no-cache gcc musl-dev sqlite-dev git

WORKDIR /build

COPY go.mod ./
# Copy everything and generate go.sum inside the container
COPY . .
RUN go mod tidy
RUN go mod download


RUN CGO_ENABLED=1 GOOS=linux go build -tags sqlite_fts5 -ldflags="-s -w" -o /build/drive ./main.go

# Runtime stage
FROM alpine:3.21

RUN apk add --no-cache \
    ffmpeg \
    sqlite \
    ca-certificates \
    tzdata

# Create non-root user
RUN addgroup -S drive && adduser -S drive -G drive

WORKDIR /app

COPY --from=builder /build/drive /app/drive

# Create default directories
RUN mkdir -p /app/data /storage && \
    chown -R drive:drive /app /app/data /storage

USER drive

EXPOSE 5827

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD wget -qO- http://127.0.0.1:5827/health || exit 1

ENTRYPOINT ["/app/drive"]
