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

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

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

# 先拷依赖清单利用缓存
COPY go.mod go.sum ./
RUN go mod download

# 拷源码并构建
COPY . .
# 静态编译，CGO_DISABLED 便于 alpine 运行
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/bin/api-gateway ./main.go

# ============ 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/api-gateway /app/api-gateway

USER app
EXPOSE 8080

# 健康检查
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD wget --quiet --spider http://localhost:8080/healthz || exit 1

ENTRYPOINT ["/app/api-gateway"]
