# ---- 前端构建 ----
FROM node:20-alpine AS web-builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN npm config set registry https://registry.npmmirror.com
WORKDIR /app/web
COPY web/package.json web/package-lock.json ./
RUN npm ci
COPY web/ ./
RUN npm run build

# ---- 后端构建 ----
FROM golang:1.24-alpine AS go-builder
ENV GOPROXY=https://goproxy.cn,direct
ENV GOTOOLCHAIN=auto
WORKDIR /app/server
COPY server/go.mod server/go.sum ./
RUN go mod download
COPY server/ ./
COPY --from=web-builder /app/web/dist ./static/
RUN CGO_ENABLED=0 GOOS=linux go build -o ashareview .

# ---- 运行镜像 ----
FROM alpine:3.20
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
WORKDIR /app
COPY --from=go-builder /app/server/ashareview .
COPY --from=go-builder /app/server/static ./static/
COPY server/config.yaml ./config.yaml
EXPOSE 8080
CMD ["./ashareview"]
