# ============================================================
# hw-ws-service Dockerfile — 多阶段构建（国内 CI 优化版）
# 优化：go mod vendor 跳过网络下载，Alpine 阿里云源加速
# ============================================================

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

# Alpine 换国内源 + 安装编译依赖
RUN sed -i 's#dl-cdn.alpinelinux.org#mirrors.aliyun.com#g' /etc/apk/repositories && \
    apk add --no-cache gcc musl-dev opus-dev opusfile-dev

WORKDIR /app
COPY . .

# vendor 模式：依赖已随代码提交，无需联网下载
# CGO_ENABLED=1 必须开启（hraban/opus 是 CGO 库）
RUN --mount=type=cache,target=/root/.cache/go-build \
    CGO_ENABLED=1 GOOS=linux \
    go build \
    -mod=vendor \
    -trimpath \
    -ldflags="-s -w" \
    -o hw-ws-service \
    ./cmd/main.go

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

RUN sed -i 's#dl-cdn.alpinelinux.org#mirrors.aliyun.com#g' /etc/apk/repositories && \
    apk add --no-cache opus opusfile ffmpeg ca-certificates && \
    addgroup -S hwws && adduser -S hwws -G hwws

COPY --from=builder /app/hw-ws-service /hw-ws-service

USER hwws
EXPOSE 8888
ENTRYPOINT ["/hw-ws-service"]
