From fa6b610ddfbecccf106eaad168bf0e84016c5548 Mon Sep 17 00:00:00 2001 From: vet Date: Mon, 20 Apr 2026 12:51:03 +0700 Subject: [PATCH] test --- 00-init-tools.sh | 32 +++------------------ 01-init-env.sh | 6 ++-- 07-start-frontend.sh | 7 +++-- 08-build-pc.sh | 58 ++++++++++++++++++++++++++++++++++++++ README.md | 19 +++++++------ dt.sh | 5 ++++ nginx/openim-pc-proxy.conf | 56 ++++-------------------------------- status.sh | 2 +- 8 files changed, 90 insertions(+), 95 deletions(-) create mode 100755 08-build-pc.sh diff --git a/00-init-tools.sh b/00-init-tools.sh index 3a4696a..00585fe 100755 --- a/00-init-tools.sh +++ b/00-init-tools.sh @@ -259,9 +259,6 @@ _install_pc_nginx_proxy() { local conf_src="${script_dir}/nginx/openim-pc-proxy.conf" local conf_name="openim-pc-proxy.conf" local proxy_domain="${PC_PROXY_DOMAIN:-cms-jack.imharry.work}" - local repo_root - repo_root="$(cd "${script_dir}/.." && pwd)" - local sdk_static_dir="/var/www/openim-pc-sdk" if [[ -f "$ENV_FILE" ]]; then # shellcheck source=/dev/null @@ -274,31 +271,9 @@ _install_pc_nginx_proxy() { return 1 fi - info " 准备 OpenIM WASM SDK 静态资源: ${sdk_static_dir}" - mkdir -p "$sdk_static_dir" - local sdk_lib_dir="${repo_root}/pc/node_modules/@openim/wasm-client-sdk/lib" - local sdk_assets_dir="${repo_root}/pc/node_modules/@openim/wasm-client-sdk/assets" - local pc_public_dir="${repo_root}/pc/public" - local asset src - for asset in worker.js worker-legacy.js; do - src="${sdk_lib_dir}/${asset}" - if [[ -s "$src" ]]; then - install -m 0644 "$src" "${sdk_static_dir}/${asset}" - success " 已安装 SDK worker: ${sdk_static_dir}/${asset}" - else - warn " 缺少 SDK worker: $src(请先执行 ./deploy-test/06-install-frontend.sh pc)" - fi - done - for asset in openIM.wasm sql-wasm.wasm wasm_exec.js; do - src="${pc_public_dir}/${asset}" - [[ -s "$src" ]] || src="${sdk_assets_dir}/${asset}" - if [[ -s "$src" ]]; then - install -m 0644 "$src" "${sdk_static_dir}/${asset}" - success " 已安装 SDK 资源: ${sdk_static_dir}/${asset}" - else - warn " 缺少 SDK 资源: ${asset}(请先执行 ./deploy-test/06-install-frontend.sh pc && ./deploy-test/07-start-frontend.sh pc)" - fi - done + if [[ ! -f /app/pc/dist/index.html ]]; then + warn " /app/pc/dist/index.html 不存在;域名首页会不可用。请先执行: ./deploy-test/dt.sh pc-build" + fi if ! _has nginx; then if _has apt-get; then @@ -342,6 +317,7 @@ _install_pc_nginx_proxy() { info " 本机 Nginx 仅监听 TCP 80;curl -sS -H 'Host: ${proxy_domain}' http://127.0.0.1/nginx-health 应返回 ok" info " 外部推荐入口 https://${proxy_domain} 应由 LB/CDN/其它网关终止 HTTPS 后转发到本机 :80" info " 建议 .env.deploy-test 设置 PC_BACKEND_ORIGIN=https://${proxy_domain}" + info " PC 静态根目录: /app/pc/dist(请先执行 ./deploy-test/dt.sh pc-build)" info " 浏览器打开 https://${proxy_domain}/" } diff --git a/01-init-env.sh b/01-init-env.sh index 802c0db..346a896 100755 --- a/01-init-env.sh +++ b/01-init-env.sh @@ -104,10 +104,10 @@ CF_CUSTOMER_CODE=huonxouoa55ent9z TENCENT_SDK_APP_ID=20033091 TENCENT_SDK_SECRET_KEY=cceba44084aaa04f8c48a1858ffd5385875c3a5ec006d34278d9d3714b40e3b0 -# ── PC 客户端(Vite dev)对接的后端公网地址(可选)─────────────────────────── +# ── PC 客户端对接的后端公网地址(可选)─────────────────────────────────────── # 通过域名访问 Nginx 反代(sudo ./deploy-test/00-init-tools.sh nginx)。 -# 填写 HTTPS 根地址,无末尾斜杠;Nginx 会将 / 代理到 pc :5173,并代理 API/WS。 -# ./deploy-test/07-start-frontend.sh 启动 pc 时会 export VITE_*,覆盖 pc/.env,无需改 pc 目录 +# 填写 HTTPS 根地址,无末尾斜杠;Nginx 会将 / 指向 /app/pc/dist,并代理 API/WS。 +# ./deploy-test/08-build-pc.sh 构建 pc 时会 export VITE_*,覆盖 pc/.env,无需改 pc 目录 # 若某路径与网关不一致,可单独覆盖:PC_VITE_API_URL / PC_VITE_WS_URL / PC_VITE_CHAT_URL / PC_VITE_USER_URL PC_PROXY_DOMAIN=cms-jack.imharry.work PC_BACKEND_ORIGIN=https://cms-jack.imharry.work diff --git a/07-start-frontend.sh b/07-start-frontend.sh index b385239..1b28e8e 100755 --- a/07-start-frontend.sh +++ b/07-start-frontend.sh @@ -7,8 +7,9 @@ # ./07-start-frontend.sh # 只启动指定项目 # # 项目与端口: -# pc → 默认 yarn dev:web :5173(无 Electron);本机要 Electron 时设 PC_ELECTRON=1 使用 yarn dev +# pc → 默认 yarn dev:web :5173(无 Electron,仅直连调试);本机要 Electron 时设 PC_ELECTRON=1 使用 yarn dev # 后端地址:在 .env.deploy-test 设 PC_BACKEND_ORIGIN,启动时注入 VITE_*(不改 pc 目录) +# 域名入口由 Nginx 直接访问 /app/pc/dist,需先执行 08-build-pc.sh # meetingh5 → React + Vite :5188 # h5 → Vue + Vite :3003 # cms → UMI Max :8001 @@ -204,8 +205,8 @@ if [[ "$TARGET" == "all" ]]; then echo "" echo -e "${BOLD}访问地址:${NC}" - echo " PC: ${PC_BACKEND_ORIGIN%/}/ (外部 HTTPS → 本机 Nginx :80 → pc :5173;默认 yarn dev:web)" - echo " PC 直连调试: http://${DEPLOY_TEST_IP}:5173 (绕过 Nginx;Electron 用 PC_ELECTRON=1 或 pc 目录内 yarn dev)" + echo " PC 域名入口: ${PC_BACKEND_ORIGIN%/}/ (外部 HTTPS → 本机 Nginx :80 → /app/pc/dist;先执行 ./deploy-test/dt.sh pc-build)" + echo " PC 直连调试: http://${DEPLOY_TEST_IP}:5173 (Vite dev;绕过 Nginx;Electron 用 PC_ELECTRON=1 或 pc 目录内 yarn dev)" echo " H5: http://${DEPLOY_TEST_IP}:3003" echo " CMS: http://${DEPLOY_TEST_IP}:8001" echo " Build CMS: http://${DEPLOY_TEST_IP}:8002" diff --git a/08-build-pc.sh b/08-build-pc.sh new file mode 100755 index 0000000..d80cb43 --- /dev/null +++ b/08-build-pc.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# ============================================================================= +# 08-build-pc.sh — 构建 PC Web 静态产物 +# +# 产物目录: /app/pc/dist +# Nginx 配置 deploy-test/nginx/openim-pc-proxy.conf 会直接访问该目录。 +# ============================================================================= +set -euo pipefail +source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh" +init_dirs + +if [[ -f "$ENV_FILE" ]]; then + set -a + # shellcheck source=/dev/null + source "$ENV_FILE" + set +a +fi + +normalize_pc_proxy_env +init_script_log + +header "构建 PC Web 静态产物" + +PC_DIR="$ROOT_DIR/pc" +if [[ ! -d "$PC_DIR" ]]; then + error "pc 目录不存在: $PC_DIR" + exit 1 +fi + +if ! command -v yarn &>/dev/null; then + error "yarn 未安装,请先执行: ./deploy-test/00-init-tools.sh node" + exit 1 +fi + +if [[ ! -d "$PC_DIR/node_modules" ]]; then + error "pc/node_modules 不存在,请先执行: ./deploy-test/06-install-frontend.sh pc" + exit 1 +fi + +pc_ensure_wasm_assets +pc_export_vite_backend_env +pc_print_vite_backend_env + +step "执行 yarn build" +( + cd "$PC_DIR" + pc_export_vite_backend_env + yarn build +) + +if [[ ! -f "$PC_DIR/dist/index.html" ]]; then + error "构建失败: $PC_DIR/dist/index.html 不存在" + exit 1 +fi + +success "PC Web 构建完成: $PC_DIR/dist" +info "Nginx 静态根目录: /app/pc/dist" +info "重新加载 Nginx 配置: sudo ./deploy-test/00-init-tools.sh nginx" diff --git a/README.md b/README.md index cff92bf..507a13a 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ deploy-test/ ./deploy-test/dt.sh stop # 停止 deploy-test 后端服务 ./deploy-test/dt.sh restart # 重启 deploy-test 后端服务 ./deploy-test/dt.sh status # 查看服务状态 +./deploy-test/dt.sh pc-build # 构建 PC Web 静态产物到 /app/pc/dist ./deploy-test/dt.sh fe-start pc # 启动 pc 前端 ./deploy-test/dt.sh fe-stop pc # 停止 pc 前端 ./deploy-test/dt.sh up # pull + build + restart @@ -307,26 +308,26 @@ TENCENT_SDK_SECRET_KEY=xxx ### Nginx 反代(域名 HTTPS 入口,供 PC / 浏览器访问后端) -在**测试服务器**上部署 Nginx,本机 Nginx 仅监听 HTTP `:80`,统一反代 PC Vite dev server、OpenIM API、chat-api 与 MsgGateway WebSocket。外部访问仍使用 `https://cms-jack.imharry.work`,HTTPS 由外层 LB/CDN/其它网关终止后转发到本机 `:80`。 +在**测试服务器**上部署 Nginx,本机 Nginx 仅监听 HTTP `:80`,直接访问 PC 构建产物 `/app/pc/dist`,并反代 OpenIM API、chat-api 与 MsgGateway WebSocket。外部访问仍使用 `https://cms-jack.imharry.work`,HTTPS 由外层 LB/CDN/其它网关终止后转发到本机 `:80`。 | 路径前缀 | 后端 | |----------|------| | `/api/im/` | `127.0.0.1:10002` | | `/api/user/`、`/api/chat/` | `127.0.0.1:10008` | | `/msg_gateway` | `127.0.0.1:10001`(WebSocket) | -| `/openIM.wasm`、`/sql-wasm.wasm`、`/wasm_exec.js`、`/node_modules/@openim/wasm-client-sdk/lib/worker*.js` | `/var/www/openim-pc-sdk`(OpenIM WASM SDK 静态资源) | -| `/` | `127.0.0.1:5173`(PC Vite dev server) | +| `/` | `/app/pc/dist`(PC Web 静态构建产物) | 1. 服务器上已执行 `05-start.sh` 等,保证 `10001/10002/10008` 在监听。 2. 域名 `cms-jack.imharry.work` 已解析到测试服务器公网 IP。 -3. 仓库根目录执行:`sudo ./deploy-test/00-init-tools.sh nginx`(会安装 `nginx`,并写入配置 `deploy-test/nginx/openim-pc-proxy.conf`;亦已包含在 `00-init-tools.sh` 无参的 **all** 流程末尾,需 root)。 -4. 测试服务器本机/安全组放行 **TCP 80**;外层 LB/CDN/网关对公网提供 HTTPS `443` 并转发到本机 `80`。 -5. `.env.deploy-test` 中设置 **`PC_BACKEND_ORIGIN=https://cms-jack.imharry.work`**(无末尾 `/`);再 `./deploy-test/07-start-frontend.sh pc` 启动 PC 时即注入 `VITE_*`。 -6. 浏览器打开 **`https://cms-jack.imharry.work/`**。 +3. `.env.deploy-test` 中设置 **`PC_BACKEND_ORIGIN=https://cms-jack.imharry.work`**(无末尾 `/`)。 +4. 仓库根目录执行:`./deploy-test/08-build-pc.sh` 或 `./deploy-test/dt.sh pc-build`,构建 `/app/pc/dist`。 +5. 仓库根目录执行:`sudo ./deploy-test/00-init-tools.sh nginx`(会安装 `nginx`,并写入配置 `deploy-test/nginx/openim-pc-proxy.conf`;亦已包含在 `00-init-tools.sh` 无参的 **all** 流程末尾,需 root)。 +6. 测试服务器本机/安全组放行 **TCP 80**;外层 LB/CDN/网关对公网提供 HTTPS `443` 并转发到本机 `80`。 +7. 浏览器打开 **`https://cms-jack.imharry.work/`**。 -**域名访问**:本配置中 `openim-pc-proxy` 使用 `server_name cms-jack.imharry.work`,只监听本机 `80`;推荐外部 `GET /` 走 `https://cms-jack.imharry.work/`,由外层 HTTPS 入口转发到本机 `80` 后代理 PC Vite。`00-init-tools.sh nginx` 会禁用 `sites-enabled/default`,并尝试去掉 `sites-available/default` 里的 `default_server`,避免默认站点抢占 `:80`。若机上还有其它站点也写了 `default_server`,`nginx -t` 会报错,需在该站点配置中删除 `default_server`(保留 `listen 80;` 即可)。 +**域名访问**:本配置中 `openim-pc-proxy` 使用 `server_name cms-jack.imharry.work`,只监听本机 `80`;推荐外部 `GET /` 走 `https://cms-jack.imharry.work/`,由外层 HTTPS 入口转发到本机 `80` 后读取 `/app/pc/dist`。`00-init-tools.sh nginx` 会禁用 `sites-enabled/default`,并尝试去掉 `sites-available/default` 里的 `default_server`,避免默认站点抢占 `:80`。若机上还有其它站点也写了 `default_server`,`nginx -t` 会报错,需在该站点配置中删除 `default_server`(保留 `listen 80;` 即可)。 -**PC WASM SDK 资源**:`00-init-tools.sh nginx` 会把 `pc/node_modules/@openim/wasm-client-sdk/lib/worker.js`、`worker-legacy.js` 以及 `openIM.wasm`、`sql-wasm.wasm`、`wasm_exec.js` 复制到 `/var/www/openim-pc-sdk`,并由 Nginx 直接返回这些文件。这样可以绕开 PC Vite dev server 里用于规避 MIME 报错的 `worker.js` placeholder,避免浏览器端 OpenIM SDK 卡在 worker 初始化阶段,导致不发起 `/api/im` 和 `/msg_gateway` 请求。更新 `pc` 依赖或 SDK 后,请重新执行 `sudo ./deploy-test/00-init-tools.sh nginx` 同步静态资源。 +**PC 构建**:`08-build-pc.sh` 会在构建前注入 `VITE_API_URL`、`VITE_WS_URL`、`VITE_CHAT_URL`、`VITE_USER_URL` 等变量,并确保 `openIM.wasm`、`sql-wasm.wasm`、`wasm_exec.js` 存在于 `pc/public`。更新 `pc` 代码或 `.env.deploy-test` 后,需要重新执行 `./deploy-test/dt.sh pc-build`。 ### Docker 基础设施 diff --git a/dt.sh b/dt.sh index 638b3c0..d0638e4 100755 --- a/dt.sh +++ b/dt.sh @@ -46,6 +46,7 @@ deploy-test 后端/基础服务: status 调用 deploy-test/status.sh deploy-test 前端: + pc-build 调用 deploy-test/08-build-pc.sh,构建 /app/pc/dist fe-start [project] 调用 deploy-test/07-start-frontend.sh [project] fe-stop [project] 调用 deploy-test/stop-frontend.sh [project] @@ -59,6 +60,7 @@ deploy-test 前端: 示例: ./deploy-test/dt.sh pull ./deploy-test/dt.sh build + ./deploy-test/dt.sh pc-build ./deploy-test/dt.sh restart ./deploy-test/dt.sh fe-start pc ./deploy-test/dt.sh deploy @@ -189,6 +191,9 @@ main() { status) run_deploy_test_script "status.sh" "$@" ;; + pc-build) + run_deploy_test_script "08-build-pc.sh" "$@" + ;; fe-start) run_deploy_test_script "07-start-frontend.sh" "$@" ;; diff --git a/nginx/openim-pc-proxy.conf b/nginx/openim-pc-proxy.conf index 29a1f0d..b3e7463 100644 --- a/nginx/openim-pc-proxy.conf +++ b/nginx/openim-pc-proxy.conf @@ -30,6 +30,9 @@ server { listen [::]:80; server_name cms-jack.imharry.work; + root /app/pc/dist; + index index.html; + client_max_body_size 100m; add_header Cross-Origin-Opener-Policy "same-origin" always; add_header Cross-Origin-Embedder-Policy "credentialless" always; @@ -110,57 +113,8 @@ server { return 200 "ok\n"; } - # OpenIM WASM SDK 关键资源。 - # Vite dev server 中有 worker.js placeholder 中间件,会导致 SDK worker 启动失败; - # 这里直接由 Nginx 从稳定目录提供真实文件,绕过 Vite。 - location = /openIM.wasm { - alias /var/www/openim-pc-sdk/openIM.wasm; - default_type application/wasm; - add_header Cross-Origin-Opener-Policy "same-origin" always; - add_header Cross-Origin-Embedder-Policy "credentialless" always; - } - - location = /sql-wasm.wasm { - alias /var/www/openim-pc-sdk/sql-wasm.wasm; - default_type application/wasm; - add_header Cross-Origin-Opener-Policy "same-origin" always; - add_header Cross-Origin-Embedder-Policy "credentialless" always; - } - - location = /wasm_exec.js { - alias /var/www/openim-pc-sdk/wasm_exec.js; - default_type application/javascript; - add_header Cross-Origin-Opener-Policy "same-origin" always; - add_header Cross-Origin-Embedder-Policy "credentialless" always; - } - - location = /node_modules/@openim/wasm-client-sdk/lib/worker.js { - alias /var/www/openim-pc-sdk/worker.js; - default_type application/javascript; - add_header Cross-Origin-Opener-Policy "same-origin" always; - add_header Cross-Origin-Embedder-Policy "credentialless" always; - } - - location = /node_modules/@openim/wasm-client-sdk/lib/worker-legacy.js { - alias /var/www/openim-pc-sdk/worker-legacy.js; - default_type application/javascript; - add_header Cross-Origin-Opener-Policy "same-origin" always; - add_header Cross-Origin-Embedder-Policy "credentialless" always; - } - - # PC Vite dev server. + # PC 静态构建产物。 location / { - proxy_pass http://127.0.0.1:5173; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $connection_upgrade; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_cache_bypass $http_upgrade; - proxy_buffering off; - proxy_read_timeout 86400s; - proxy_send_timeout 86400s; + try_files $uri $uri/ /index.html; } } diff --git a/status.sh b/status.sh index 0968679..f5d90b5 100755 --- a/status.sh +++ b/status.sh @@ -76,7 +76,7 @@ if command -v curl &>/dev/null && [[ -n "${PC_BACKEND_ORIGIN:-}" ]]; then echo " PC Chat API: ${PC_VITE_CHAT_URL:-${PC_BACKEND_ORIGIN}/api/chat}" echo " PC Admin API: ${PC_VITE_ADMIN_URL:-${PC_BACKEND_ORIGIN}/api/admin}" echo " PC WebSocket: ${PC_VITE_WS_URL:-$default_ws_url}" - echo " PC 页面入口: ${PC_BACKEND_ORIGIN}/ (外部 HTTPS → 本机 Nginx :80 → pc :5173)" + echo " PC 页面入口: ${PC_BACKEND_ORIGIN}/ (外部 HTTPS → 本机 Nginx :80 → /app/pc/dist)" echo " Nginx 日志: /var/log/nginx/openim-pc-proxy-access.log" pc_probe_msg_gateway "$PC_BACKEND_ORIGIN" else