# Build Stage - 使用预装 Go 的基础镜像
FROM golang:1.25-alpine AS builder

WORKDIR /app

ENV GOPROXY=https://goproxy.cn,direct
ENV GOSUMDB=sum.golang.google.cn

# 复制依赖文件
COPY go.mod go.sum ./
RUN go mod download

# 复制源代码
COPY . .

# 编译 Go 应用
RUN go build -v -o server ./cmd/server

# Run Stage
FROM alpine:latest

WORKDIR /app

RUN apk add --no-cache wget file

COPY --from=builder /app/server .
RUN chmod +x server

COPY --from=builder /app/web ./web
COPY --from=builder /app/docs ./docs

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:8080/web || exit 1

CMD ["./server"]
