重构为Monorepo:拆分xhs/xhh应用与core包并完成双服务部署改造

This commit is contained in:
2026-03-03 16:06:16 +08:00
parent ed7fbdd5c2
commit 2cbd6b28b2
84 changed files with 6332 additions and 7678 deletions
+34 -41
View File
@@ -4,57 +4,54 @@
FROM node:22-slim AS builder
# Proxy for downloading dependencies (passed via --build-arg)
ARG APP_NAME=xhs-mcp
ARG HTTP_PROXY
ARG HTTPS_PROXY
ENV HTTP_PROXY=${HTTP_PROXY} \
HTTPS_PROXY=${HTTPS_PROXY}
# China npm mirror
RUN npm config set registry https://registry.npmmirror.com
RUN corepack enable
RUN pnpm config set registry https://registry.npmmirror.com
WORKDIR /app
# Copy package manifests first (layer caching for dependency install)
COPY package.json package-lock.json ./
# Copy manifests first for better caching
COPY pnpm-workspace.yaml package.json pnpm-lock.yaml tsconfig.json tsconfig.base.json ./
COPY packages/core/package.json packages/core/tsconfig.json packages/core/tsup.config.ts ./packages/core/
COPY apps/xhs-mcp/package.json apps/xhs-mcp/tsconfig.json apps/xhs-mcp/tsup.config.ts ./apps/xhs-mcp/
COPY apps/xhh-mcp/package.json apps/xhh-mcp/tsconfig.json apps/xhh-mcp/tsup.config.ts ./apps/xhh-mcp/
# Install all dependencies (including devDependencies for building)
RUN npm ci
RUN pnpm install --frozen-lockfile
# Install Chromium matching rebrowser-playwright version (NOT playwright)
RUN npx rebrowser-playwright install chromium
# Copy source code
COPY tsconfig.json tsup.config.ts ./
COPY src/ src/
COPY packages ./packages
COPY apps ./apps
# Build the backend
RUN npm run build
# Build the web dashboard
COPY web/ web/
RUN cd web && npm ci && npm run build && mkdir -p ../dist/web && cp -r dist/* ../dist/web/
# Remove devDependencies to slim down node_modules for production
RUN npm prune --omit=dev
# Build shared core first, then selected app
RUN pnpm --filter @social/core build
RUN pnpm --filter @social/${APP_NAME} build
# =============================================================================
# Stage 2: Production
# Stage 2: Runtime
# =============================================================================
FROM node:22-slim
# Use China apt mirror
RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null || true
# Proxy for apt-get (passed via --build-arg)
ARG APP_NAME=xhs-mcp
ARG HTTP_PROXY
ARG HTTPS_PROXY
ENV HTTP_PROXY=${HTTP_PROXY} \
HTTPS_PROXY=${HTTPS_PROXY}
# Install Chromium dependencies required by Playwright/rebrowser-playwright
RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources 2>/dev/null || \
sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list 2>/dev/null || true
# Chromium runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
libnss3 \
libnspr4 \
@@ -77,41 +74,37 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
fonts-noto-cjk \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN groupadd --gid 1001 appuser \
&& useradd --uid 1001 --gid appuser --shell /bin/sh --create-home appuser
WORKDIR /app
# Copy built artifacts and production dependencies from builder
COPY --from=builder --chown=appuser:appuser /app/dist ./dist
COPY --from=builder --chown=appuser:appuser /app/node_modules ./node_modules
COPY --from=builder --chown=appuser:appuser /app/package.json ./package.json
COPY --from=builder --chown=appuser:appuser /app/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=builder --chown=appuser:appuser /app/node_modules ./node_modules
COPY --from=builder --chown=appuser:appuser /app/packages ./packages
COPY --from=builder --chown=appuser:appuser /app/apps ./apps
# Copy Playwright browsers from builder
COPY --from=builder --chown=appuser:appuser /root/.cache/ms-playwright /home/appuser/.cache/ms-playwright
# Create data directory for cookies and API token
RUN mkdir -p /home/appuser/.social-mcp \
&& chown -R appuser:appuser /home/appuser/.social-mcp
RUN mkdir -p /home/appuser/.social-mcp-xhs /home/appuser/.social-mcp-xhh \
&& chown -R appuser:appuser /home/appuser/.social-mcp-xhs /home/appuser/.social-mcp-xhh
# Switch to non-root user
USER appuser
# Environment defaults
# Clear proxy env from build stage (must not leak into runtime)
ENV HTTP_PROXY= \
HTTPS_PROXY= \
NODE_ENV=production \
HOST=0.0.0.0 \
PORT=3000 \
PORT=9527 \
HEADLESS=true \
COOKIE_DIR=/home/appuser/.social-mcp \
COOKIE_DIR=/home/appuser/.social-mcp-xhs \
APP_NAME=${APP_NAME} \
ALLOW_REMOTE=yes-i-understand-the-risk
EXPOSE 3000
EXPOSE 9527
HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=10s \
CMD node -e "fetch('http://localhost:3000/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
CMD node -e "fetch('http://localhost:' + (process.env.PORT || '9527') + '/health').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
CMD ["node", "dist/index.js"]
CMD ["sh", "-lc", "node apps/${APP_NAME}/dist/main.js"]