# ========== 构建阶段 ==========
FROM golang:1.26-alpine AS builder

RUN apk add --no-cache git ca-certificates

WORKDIR /app

# 复制服务代码 + 共享 pkg（保持目录结构以匹配 go.mod replace 路径）
COPY backend/iot-debug-service/ ./backend/iot-debug-service/
COPY backend/pkg/ ./backend/pkg/

WORKDIR /app/backend/iot-debug-service
ENV GOPROXY=https://goproxy.cn,direct
RUN go mod download

# 编译 (静态链接)
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /iot-debug-service ./cmd/main.go

# ========== 运行阶段 ==========
FROM alpine:3.20

RUN apk add --no-cache ca-certificates tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

WORKDIR /app

COPY --from=builder /iot-debug-service .

RUN adduser -D -H cyrene
USER cyrene

EXPOSE 8083

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:8083/api/v1/health || exit 1

ENTRYPOINT ["./iot-debug-service"]
