Переглянути джерело

wedo-hub:原型协同中心通知服务(多项目事件广播/鉴权/持久化/部署文档)

lio 2 місяців тому
коміт
499ed181a1
4 змінених файлів з 176 додано та 0 видалено
  1. 3 0
      .gitignore
  2. 92 0
      README.md
  3. 9 0
      package.json
  4. 72 0
      server.js

+ 3 - 0
.gitignore

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

+ 92 - 0
README.md

@@ -0,0 +1,92 @@
+# wedo-hub
+
+wedo 原型协同**中心通知服务**——多人通过 git 共建原型库(各自 MCP `wedo` 推送/拉取),hub 只负责一件事:**广播"谁改了什么"**。不存原型内容(内容归 git),零依赖(node ≥ 18 单文件)。
+
+```
+用户A改完原型 → MCP publish(push到git仓库) → POST /events 上报hub
+                                                    │
+用户B的MCP定时轮询 GET /events?repo=&since= ────────┘ → 🔔"A更新了 财务管理/包额"
+(hub不可达时MCP自动降级为裸 git fetch 比对提交,功能不缺失)
+```
+
+## 接口
+
+| 方法 | 路径 | 说明 |
+|---|---|---|
+| POST | `/events` | 上报改动事件 `{repo, user, message, commit, files[]}`(repo/message 必填) |
+| GET | `/events?repo=<项目>&since=<时间戳>` | 增量轮询某项目事件(since=上次游标,首次传 0) |
+| GET | `/repos` | 项目维度视图:每项目的活跃用户/事件数/最近改动 |
+| GET | `/health` | 健康检查 |
+
+- **鉴权**:所有请求头 `x-wedo-token: <WEDO_TOKEN>`(服务端未设置 WEDO_TOKEN 时不校验);
+- **项目维度**:`repo` 字段隔离(如 `hanglung`、其他系统原型库各自命名),一份 hub 服务多项目;
+- **持久化**:内存 + `events.json`(最近 200 条,重启不丢)。
+
+## 部署
+
+### 方式一:node 直启(最快)
+
+```bash
+node server.js                       # 默认 8790 端口,无鉴权(仅内网可信环境)
+WEDO_PORT=8790 WEDO_TOKEN=你的口令 node server.js
+```
+
+Windows: `set WEDO_TOKEN=xxx && node server.js`
+
+### 方式二:pm2 守护
+
+```bash
+npm i -g pm2
+WEDO_PORT=8790 WEDO_TOKEN=你的口令 pm2 start server.js --name wedo-hub
+pm2 save && pm2 startup
+```
+
+### 方式三: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
+```
+
+```bash
+systemctl enable --now wedo-hub
+```
+
+### 验证
+
+```bash
+curl -H "x-wedo-token: 你的口令" http://<host>:8790/health
+```
+
+## MCP 侧配套(各协作用户)
+
+ZCode 的 wedo MCP 配置 env:
+
+```json
+{
+  "WEDO_HUB_URL": "http://<host>:8790",
+  "WEDO_HUB_TOKEN": "你的口令",
+  "WEDO_REPO": "hanglung"
+}
+```
+
+不配置 hub 时 MCP 自动使用纯 git 模式(publish 正常,更新感知降级为提交级比对)。
+
+## 目录
+
+```
+server.js      服务本体(零依赖单文件)
+events.json    运行时事件持久化(git忽略)
+```

+ 9 - 0
package.json

@@ -0,0 +1,9 @@
+{
+  "name": "wedo-hub",
+  "version": "1.0.0",
+  "description": "wedo原型协同中心通知服务:多项目事件广播(publish/轮询),零依赖node",
+  "type": "module",
+  "main": "server.js",
+  "scripts": { "start": "node server.js" },
+  "engines": { "node": ">=18" }
+}

+ 72 - 0
server.js

@@ -0,0 +1,72 @@
+#!/usr/bin/env node
+/**
+ * wedo-hub —— 原型库多人协同中心通知服务(零依赖,node>=18)
+ *
+ * 职责(刻意保持极简,只做"谁改了什么"的事件广播,不碰原型内容):
+ *   POST /events   MCP publish 成功后上报改动事件 {repo,user,message,commit,files}
+ *   GET  /events?repo=&since=  各端 MCP 定时轮询,拿业务级事件列表("谁改了哪个页面")
+ *   GET  /health   健康检查
+ * 鉴权: 请求头 x-wedo-token 与启动环境 WEDO_TOKEN 一致(未设置则不校验)
+ * 存储: 内存 + events.json 追久化(最近200条,重启不丢)
+ *
+ * 启动: WEDO_PORT=8790 WEDO_TOKEN=xxx node server.js
+ * MCP 侧配套: WEDO_HUB_URL=http://<host>:8790 WEDO_HUB_TOKEN=xxx
+ */
+import http from "node:http";
+import { readFile, writeFile } from "node:fs/promises";
+
+const PORT = Number(process.env.WEDO_PORT || 8790);
+const TOKEN = process.env.WEDO_TOKEN || "";
+const STORE = new URL("./events.json", import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1");
+const MAX = 200;
+let events = [];
+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) => {
+  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");
+
+  if (req.method === "GET" && u.pathname === "/health") return json(200, { ok: true, events: events.length, now: Date.now() });
+
+  /* 项目维度视图:每个项目的活跃用户/事件数/最近事件时间(运维与接入确认用) */
+  if (req.method === "GET" && u.pathname === "/repos") {
+    const by = {};
+    for (const e of events) {
+      by[e.repo] = by[e.repo] || { repo: e.repo, events: 0, users: new Set(), latest: 0, latestMessage: "" };
+      by[e.repo].events++;
+      by[e.repo].users.add(e.user);
+      if (e.ts > by[e.repo].latest) { by[e.repo].latest = e.ts; by[e.repo].latestMessage = e.message; }
+    }
+    return json(200, { repos: Object.values(by).map(r => ({ repo: r.repo, events: r.events, users: [...r.users], latest: r.latest, latestMessage: r.latestMessage })) });
+  }
+
+  if (req.method === "POST" && u.pathname === "/events") {
+    let body = "";
+    for await (const c of req) body += c;
+    let d; try { d = JSON.parse(body); } catch { return json(400, { error: "bad json" }); }
+    if (!d || !d.repo || !d.message) return json(400, { error: "repo/message required" });
+    const ev = {
+      ts: Date.now(), repo: String(d.repo).slice(0, 64), user: String(d.user || "unknown").slice(0, 32),
+      message: String(d.message).slice(0, 200), commit: String(d.commit || "").slice(0, 40),
+      files: Array.isArray(d.files) ? d.files.slice(0, 10).map(f => String(f).slice(0, 120)) : [],
+    };
+    events.push(ev);
+    if (events.length > MAX) events = events.slice(-MAX);
+    await persist();
+    console.log(`[wedo-hub] ${ev.user}: ${ev.message}`);
+    return json(200, { ok: true, ts: ev.ts });
+  }
+
+  if (req.method === "GET" && u.pathname === "/events") {
+    const repo = u.searchParams.get("repo");
+    const since = Number(u.searchParams.get("since") || 0);
+    const list = events.filter(e => (!repo || e.repo === repo) && e.ts > since);
+    return json(200, { events: list, latest: events.length ? events[events.length - 1].ts : 0 });
+  }
+
+  json(404, { error: "not found" });
+});
+server.listen(PORT, () => console.log(`[wedo-hub] listening :${PORT} (token ${TOKEN ? "on" : "off"}, ${events.length} events restored)`));