FROM node:22 AS builder

WORKDIR /build
COPY . .
# 定义参数
ARG USE_PROXY

# 根据USE_PROXY参数设置环境变量
RUN if [ "$USE_PROXY" = "1" ]; then \
    npm config set registry https://mirrors.huaweicloud.com/repository/npm/ && \
    npm i -g nrm && nrm use taobao; \
    fi \
    && npm install -g npm@latest pnpm && pnpm i && pnpm build

FROM nginx:latest

# 替换nginx中的地址
# ARG NGINX_CONFIG_URL
ENV NGINX_CONFIG_URL=kami_backend

WORKDIR /app
# 替换默认的配置文件
COPY --from=builder /build/deploy/nginx/ /etc/nginx/conf.d/
COPY --from=builder /build/dist/ /app/

# 替换文件里的内容
RUN sed -i "s#NGINX_CONFIG_URL#${NGINX_CONFIG_URL}#g" /etc/nginx/conf.d/default.conf
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# 添加安全检查
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl -f http://127.0.0.1:12400/api/monitor/heathcheck || exit 1
# CMD ["sed", "-i", "'s#NGINX_CONFIG_URL#${NGINX_CONFIG_URL}#g'", "/etc/nginx/conf.d/default.conf && nginx ", "-g", "'daemon off;'"]
EXPOSE 12400