Przeglądaj źródła

wedo-hub:CORS支持(file://门户跨域轮询,OPTIONS预检鉴权前放行)+线上部署(gogs服务器Docker/nginx /wedo-hub 反代,口令走.env不入库)

lio 2 miesięcy temu
rodzic
commit
80b78393be
4 zmienionych plików z 41 dodań i 37 usunięć
  1. 1 0
      .gitignore
  2. 15 32
      README.md
  3. 17 0
      docker-compose.yml
  4. 8 5
      server.js

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 node_modules/
 events.json
 *.log
+.env

+ 15 - 32
README.md

@@ -19,55 +19,38 @@ wedo 原型协同**中心通知服务**——多人通过 git 共建原型库(
 | GET | `/health` | 健康检查 |
 
 - **鉴权**:所有请求头 `x-wedo-token: <WEDO_TOKEN>`(服务端未设置 WEDO_TOKEN 时不校验);
+- **CORS**:回显请求 Origin 并放行 OPTIONS 预检(预检不带 token,在鉴权前处理),支持 `file://` 门户页面跨域轮询;
 - **项目维度**:`repo` 字段隔离(如 `hanglung`、其他系统原型库各自命名),一份 hub 服务多项目;
 - **持久化**:内存 + `events.json`(最近 200 条,重启不丢)。
 
 ## 部署
 
-### 方式一:node 直启(最快)
+**当前线上部署**:gogs 服务器(CentOS 7)以 Docker 运行,对外经宝塔 nginx 反代复用 gogs.wxapp.info 证书:
 
-```bash
-node server.js                       # 默认 8790 端口,无鉴权(仅内网可信环境)
-WEDO_PORT=8790 WEDO_TOKEN=你的口令 node server.js
-```
+- 对外地址:`https://gogs.wxapp.info/wedo-hub/`(nginx `file-upload.conf` 中 `/wedo-hub/` → `127.0.0.1:8790`,端口仅绑本机)
+- 容器管理:`cd /lio/projects/crrc/wedo-mcp-server && docker compose up -d`(restart=unless-stopped,开机自启)
+- 口令:仓库目录 `.env` 的 `WEDO_TOKEN`(git 忽略,不入库;变更后 `docker compose up -d` 重建生效)
 
-Windows: `set WEDO_TOKEN=xxx && node server.js`
-
-### 方式二:pm2 守护
+### 容器方式(线上采用,宿主机 node 版本过老时)
 
 ```bash
-npm i -g pm2
-WEDO_PORT=8790 WEDO_TOKEN=你的口令 pm2 start server.js --name wedo-hub
-pm2 save && pm2 startup
+echo "WEDO_TOKEN=你的口令" > .env
+docker compose up -d
 ```
 
-### 方式三:systemd(Linux)
-
-```ini
-# /etc/systemd/system/wedo-hub.service
-[Unit]
-Description=wedo prototype hub
-After=network.target
-
-[Service]
-WorkingDirectory=/opt/wedo-hub
-Environment=WEDO_PORT=8790
-Environment=WEDO_TOKEN=你的口令
-ExecStart=/usr/bin/node server.js
-Restart=always
-
-[Install]
-WantedBy=multi-user.target
-```
+### node 直启(内网快速试用)
 
 ```bash
-systemctl enable --now wedo-hub
+node server.js                       # 默认 8790 端口,无鉴权(仅内网可信环境)
+WEDO_PORT=8790 WEDO_TOKEN=你的口令 node server.js
 ```
 
+Windows: `set WEDO_TOKEN=xxx && node server.js`
+
 ### 验证
 
 ```bash
-curl -H "x-wedo-token: 你的口令" http://<host>:8790/health
+curl -H "x-wedo-token: 你的口令" https://gogs.wxapp.info/wedo-hub/health
 ```
 
 ## MCP 侧配套(各协作用户)
@@ -76,7 +59,7 @@ ZCode 的 wedo MCP 配置 env:
 
 ```json
 {
-  "WEDO_HUB_URL": "http://<host>:8790",
+  "WEDO_HUB_URL": "https://gogs.wxapp.info/wedo-hub",
   "WEDO_HUB_TOKEN": "你的口令",
   "WEDO_REPO": "hanglung"
 }

+ 17 - 0
docker-compose.yml

@@ -0,0 +1,17 @@
+# wedo-hub 部署(CentOS 7 宿主机 node 版本过老,统一用容器跑,与 ocr-service 同模式)
+# 对外入口: https://gogs.wxapp.info/wedo-hub/ (宝塔 nginx file-upload.conf 反代 127.0.0.1:8790)
+# 口令在 .env(git 忽略): WEDO_TOKEN=xxx
+services:
+  wedo-hub:
+    image: node:20-alpine
+    container_name: wedo-hub
+    restart: unless-stopped
+    working_dir: /app
+    command: node server.js
+    environment:
+      WEDO_PORT: "8790"
+      WEDO_TOKEN: ${WEDO_TOKEN}
+    ports:
+      - "127.0.0.1:8790:8790" # 仅本机 nginx 可达,不经 token 校验的端口不暴露公网
+    volumes:
+      - ./:/app # events.json 持久化在宿主机仓库目录(git 已忽略)

+ 8 - 5
server.js

@@ -7,6 +7,7 @@
  *   GET  /events?repo=&since=  各端 MCP 定时轮询,拿业务级事件列表("谁改了哪个页面")
  *   GET  /health   健康检查
  * 鉴权: 请求头 x-wedo-token 与启动环境 WEDO_TOKEN 一致(未设置则不校验)
+ * CORS: file:// 门户跨域轮询 -> 回显 Origin + OPTIONS 预检放行(预检不带token,须在鉴权前处理)
  * 存储: 内存 + events.json 追久化(最近200条,重启不丢)
  *
  * 启动: WEDO_PORT=8790 WEDO_TOKEN=xxx node server.js
@@ -25,11 +26,13 @@ try { events = JSON.parse(await readFile(STORE, "utf8")); } catch { }
 async function persist() { try { await writeFile(STORE, JSON.stringify(events.slice(-MAX)), "utf8"); } catch { } }
 
 const server = http.createServer(async (req, res) => {
-  /* CORS:允许门户(file://页面,origin=null)跨域轮询;鉴权仍由token控制 */
-  res.setHeader("Access-Control-Allow-Origin", "*");
-  res.setHeader("Access-Control-Allow-Headers", "content-type, x-wedo-token");
-  res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
-  if (req.method === "OPTIONS") { res.writeHead(204); return res.end(); }
+  /* CORS: 回显请求 Origin(file:// 页面为 null);file:// 门户跨域轮询必需 */
+  res.setHeader("access-control-allow-origin", req.headers.origin || "*");
+  res.setHeader("access-control-allow-methods", "GET, POST, OPTIONS");
+  res.setHeader("access-control-allow-headers", "content-type, x-wedo-token");
+  res.setHeader("vary", "origin");
+  if (req.method === "OPTIONS") { res.writeHead(204, { "access-control-max-age": "86400" }); return res.end(); }
+
   const json = (code, obj) => { res.writeHead(code, { "content-type": "application/json; charset=utf-8" }); res.end(JSON.stringify(obj)); };
   if (TOKEN && req.headers["x-wedo-token"] !== TOKEN) return json(401, { error: "unauthorized" });
   const u = new URL(req.url, "http://x");