FROM golang:1.23-alpine AS builder

WORKDIR /src
COPY go.mod go.sum* ./
RUN go mod download || true
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /out/app ./cmd/app \
 && CGO_ENABLED=0 GOOS=linux go build -o /out/backfill ./cmd/backfill

FROM alpine:3.20
# wget 用于容器 healthcheck；ca-certificates 用于 TLS；tzdata 用于本地时区
RUN apk add --no-cache ca-certificates tzdata wget
WORKDIR /app
COPY --from=builder /out/app /app/app
COPY --from=builder /out/backfill /app/backfill
EXPOSE 8080

# 容器级 healthcheck：拒绝把 unhealthy 容器纳入负载均衡
HEALTHCHECK --interval=15s --timeout=3s --start-period=20s --retries=3 \
  CMD wget --spider -q http://127.0.0.1:8080/v1/health || exit 1

ENTRYPOINT ["/app/app"]
