# Build the Linux binary in a separate stage.
FROM golang:1.22-alpine AS builder

RUN addgroup -S appgroup && adduser -S appuser -G appgroup

WORKDIR /app

COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags="-s -w" -o /usr/local/bin/data-platform ./cmd/data-platform

# Create the runtime image.
FROM alpine:3.18

RUN apk add --no-cache ca-certificates tzdata && \
    addgroup -S appgroup && adduser -S appuser -G appgroup

WORKDIR /app
RUN mkdir -p /app/config /app/data

COPY --from=builder --chown=appuser:appgroup /usr/local/bin/data-platform /usr/local/bin/data-platform

USER appuser

ENV PORT=8868
EXPOSE 8868

CMD ["/usr/local/bin/data-platform", "-c", "/app/config/config.yaml"]
