# Application Engine - Main GoHoarder Server
# This Dockerfile expects a PRE-BUILT binary from GoReleaser (no compilation)
# GoReleaser injects the platform-specific binary automatically

FROM --platform=$TARGETPLATFORM alpine:latest

ARG TARGETARCH

# Install runtime dependencies (CGO/SQLite requires these)
RUN apk add --no-cache \
    ca-certificates \
    tzdata \
    sqlite-libs \
    musl \
    && update-ca-certificates

# Create non-root user
RUN addgroup -g 1000 gohoarder && \
    adduser -D -u 1000 -G gohoarder gohoarder

# Create necessary directories with proper permissions
RUN mkdir -p /var/cache/gohoarder \
             /var/lib/gohoarder/metadata \
             /tmp/gohoarder && \
    chown -R gohoarder:gohoarder /var/cache/gohoarder \
                                  /var/lib/gohoarder \
                                  /tmp/gohoarder && \
    chmod -R 750 /var/cache/gohoarder \
                 /var/lib/gohoarder

# Copy pre-built binary from GoReleaser
# GoReleaser will automatically inject the correct binary for the target platform
# In split/merge mode, binaries are in linux/${TARGETARCH}/ subdirectories
COPY linux/${TARGETARCH}/gohoarder /usr/local/bin/gohoarder
RUN chmod +x /usr/local/bin/gohoarder

# Copy example config
COPY config.yaml.example /etc/gohoarder/config.yaml.example

WORKDIR /var/cache/gohoarder
USER gohoarder

# Expose ports
# 8080: Main proxy port
# 9090: Metrics/health port
EXPOSE 8080 9090

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD ["/usr/local/bin/gohoarder", "version"] || exit 1

# Run the server
ENTRYPOINT ["/usr/local/bin/gohoarder"]
CMD ["serve"]
