# ---- Stage 1: build the React/Vite SPA ----
FROM docker.m.daocloud.io/node:20-alpine AS build

WORKDIR /app

# npm registry mirror for CN runners
RUN npm config set registry https://registry.npmmirror.com

COPY package.json package-lock.json ./
RUN npm ci

COPY . .
# VITE_API_BASE_URL is empty => the SPA calls /api on the same origin,
# which nginx (below) proxies to the backend service. Zero CORS.
ENV VITE_API_BASE_URL=""
RUN npm run build

# ---- Stage 2: serve static + reverse-proxy /api to backend ----
FROM docker.m.daocloud.io/nginx:alpine

RUN sed -i 's#dl-cdn.alpinelinux.org#mirrors.aliyun.com#g' /etc/apk/repositories

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist /usr/share/nginx/html

EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
