FROM docker.m.daocloud.io/library/node:22-bullseye AS build

# 环境变量与系统依赖
ENV npm_config_canvas_binary_host_mirror="https://npmmirror.com/mirrors/canvas" \
    npm_config_sharp_binary_host="https://npmmirror.com/mirrors/sharp" \
    npm_config_sharp_libvips_binary_host="https://npmmirror.com/mirrors/sharp-libvips" \
    ESBUILD_BINARY_HOST="https://npmmirror.com/mirrors/esbuild"

RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    apt-get update && apt-get install -y --no-install-recommends \
    build-essential python3 pkg-config \
    libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev libvips-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY package.json package-lock.json ./
RUN npm cache clean --force && \
    npm install sharp@0.34.3 --verbose --registry=https://registry.npmmirror.com && \
    npm install --legacy-peer-deps --verbose --registry=https://registry.npmmirror.com

COPY . .
RUN npm run build:test

FROM docker.m.daocloud.io/library/nginx:alpine
RUN echo "https://mirrors.aliyun.com/alpine/v3.22/main/" > /etc/apk/repositories && \
    echo "https://mirrors.aliyun.com/alpine/v3.22/community/" >> /etc/apk/repositories && \
    apk update && apk add --no-cache tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 8333
CMD ["nginx", "-g", "daemon off;"]
