# 多阶段构建：push-gateway 生产镜像
# 用法：docker build -t edu/push-gateway:latest -f services/push-gateway/Dockerfile .

# ============ Builder ============
FROM golang:1.25-alpine AS builder
WORKDIR /app

# git 与 ca-certificates 为 go mod 下载所需
RUN apk add --no-cache git ca-certificates

# 利用 go.work 时需拷贝根 go.work 与 shared-go 包
COPY go.mod go.sum ./
COPY packages/shared-go ./packages/shared-go
COPY services/push-gateway/go.mod services/push-gateway/go.sum ./services/push-gateway/
RUN cd services/push-gateway && go mod download

# 拷源码并构建（静态编译便于 alpine 运行）
COPY services/push-gateway ./services/push-gateway
COPY packages/shared-go ./packages/shared-go
RUN CGO_ENABLED=0 GOOS=linux go build \
    -ldflags="-s -w -X main.serviceName=push-gateway" \
    -o /app/bin/push-gateway \
    ./services/push-gateway

# ============ Runtime ============
FROM alpine:3.20 AS runner
WORKDIR /app

RUN apk add --no-cache ca-certificates tzdata wget

# 非 root 用户
RUN addgroup -g 1001 -S app && adduser -S app -u 1001 -G app

COPY --from=builder /app/bin/push-gateway /app/push-gateway

USER app
EXPOSE 8081

# 健康检查（liveness probe；readiness 由 /readyz 单独探活）
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD wget --quiet --spider http://localhost:8081/healthz || exit 1

ENTRYPOINT ["/app/push-gateway"]
