Extract memind_adm admin server, add local dev tooling, and remove image-generation.
Split platform admin and ops APIs into standalone admin-server.mjs with network guards; simplify billing to RMB token pricing, refactor user auth, and add rsync deploy plus local-test scripts and docs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+29
-120
@@ -1,8 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
import { spawn, execSync } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
@@ -13,20 +11,9 @@ const portalPort = Number(process.env.H5_PORT ?? 8081);
|
||||
const vitePort = Number(process.env.VITE_PORT ?? 5173);
|
||||
const plazaPort = Number(process.env.PLAZA_PORT ?? 3001);
|
||||
const opsPort = Number(process.env.OPS_PORT ?? 3002);
|
||||
const adminPort = Number(process.env.ADMIN_PORT ?? 8082);
|
||||
const portalUrl = `http://127.0.0.1:${portalPort}`;
|
||||
const plazaHost = process.env.PLAZA_LOCAL_HOST ?? 'plaza.tkmind.cn';
|
||||
const plazaPublicPort = Number(process.env.PLAZA_PUBLIC_PORT ?? 443);
|
||||
const plazaPublicScheme = String(process.env.PLAZA_PUBLIC_BASE ?? 'https://plaza.tkmind.cn').startsWith('http://')
|
||||
? 'http'
|
||||
: 'https';
|
||||
const plazaPublicBase = (
|
||||
process.env.PLAZA_PUBLIC_BASE ??
|
||||
(plazaPublicPort === 80 || plazaPublicPort === 443
|
||||
? `${plazaPublicScheme}://${plazaHost}`
|
||||
: `${plazaPublicScheme}://${plazaHost}:${plazaPublicPort}`)
|
||||
).replace(/\/$/, '');
|
||||
const plazaUrl = plazaPublicBase;
|
||||
const opsUrl = `http://127.0.0.1:${opsPort}`;
|
||||
const adminUrl = `http://127.0.0.1:${adminPort}`;
|
||||
|
||||
function loadEnvFile(filePath) {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
@@ -69,8 +56,8 @@ function spawnChild(command, args, label, cwd = root, extraEnv = {}) {
|
||||
}
|
||||
|
||||
let server;
|
||||
let admin;
|
||||
let plaza;
|
||||
let plazaProxy;
|
||||
let ops;
|
||||
let vite;
|
||||
let stopping = false;
|
||||
@@ -79,8 +66,8 @@ function shutdown(code = 0) {
|
||||
if (stopping) return;
|
||||
stopping = true;
|
||||
server?.kill('SIGTERM');
|
||||
admin?.kill('SIGTERM');
|
||||
plaza?.kill('SIGTERM');
|
||||
plazaProxy?.kill('SIGTERM');
|
||||
ops?.kill('SIGTERM');
|
||||
vite?.kill('SIGTERM');
|
||||
setTimeout(() => process.exit(code), 300);
|
||||
@@ -102,7 +89,11 @@ async function waitFor(url, check, label, retries = 60) {
|
||||
}
|
||||
|
||||
const mindSpacePublicBase = (
|
||||
process.env.H5_PUBLIC_BASE_URL ?? `http://localhost:${vitePort}`
|
||||
process.env.H5_PUBLIC_BASE_URL ?? `http://127.0.0.1:${vitePort}`
|
||||
).replace(/\/$/, '');
|
||||
|
||||
const plazaPublicBase = (
|
||||
process.env.PLAZA_PUBLIC_BASE ?? `http://127.0.0.1:${plazaPort}`
|
||||
).replace(/\/$/, '');
|
||||
|
||||
const plazaEnv = {
|
||||
@@ -122,6 +113,7 @@ const viteEnv = {
|
||||
|
||||
const opsEnv = {
|
||||
OPS_API_PROXY: portalUrl,
|
||||
OPS_ADMIN_PROXY: adminUrl,
|
||||
VITE_PLAZA_BASE: plazaPublicBase,
|
||||
VITE_MINDSPACE_BASE: mindSpacePublicBase,
|
||||
};
|
||||
@@ -132,86 +124,9 @@ function ensurePlazaApp() {
|
||||
}
|
||||
}
|
||||
|
||||
function plazaPublicReachable() {
|
||||
const url = new URL(`${plazaPublicBase}/plaza`);
|
||||
const isHttps = url.protocol === 'https:';
|
||||
const port = url.port || (isHttps ? 443 : 80);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const transport = isHttps ? https : http;
|
||||
const req = transport.request(
|
||||
{
|
||||
hostname: url.hostname,
|
||||
port,
|
||||
path: `${url.pathname}${url.search}`,
|
||||
method: 'GET',
|
||||
rejectUnauthorized: false,
|
||||
},
|
||||
(res) => {
|
||||
resolve(res.statusCode === 200 || res.statusCode === 307 || res.statusCode === 308);
|
||||
},
|
||||
);
|
||||
req.on('error', () => resolve(false));
|
||||
req.setTimeout(3000, () => {
|
||||
req.destroy();
|
||||
resolve(false);
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
async function ensurePlazaPublicProxy() {
|
||||
if (await plazaPublicReachable()) {
|
||||
console.log(`==> Plaza 公开入口已可用: ${plazaPublicBase}/plaza`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`==> 尝试启动 Plaza 80 端口代理 (${plazaPublicBase})...`);
|
||||
const proxyScript = path.join(root, 'scripts/plaza-local-proxy.mjs');
|
||||
plazaProxy = spawn('node', [proxyScript], {
|
||||
cwd: root,
|
||||
env: process.env,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
|
||||
let proxyFailed = false;
|
||||
plazaProxy.stdout?.on('data', (chunk) => process.stdout.write(`[plaza-proxy] ${chunk}`));
|
||||
plazaProxy.stderr?.on('data', (chunk) => {
|
||||
const text = String(chunk);
|
||||
process.stderr.write(`[plaza-proxy] ${text}`);
|
||||
if (/EACCES|EADDRINUSE|需要 root/.test(text)) proxyFailed = true;
|
||||
});
|
||||
plazaProxy.on('exit', (code) => {
|
||||
if (code && code !== 0) proxyFailed = true;
|
||||
});
|
||||
|
||||
for (let i = 0; i < 20; i += 1) {
|
||||
if (proxyFailed) break;
|
||||
if (await plazaPublicReachable()) {
|
||||
console.log(`==> Plaza 公开入口已可用: ${plazaPublicBase}/plaza`);
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 250));
|
||||
}
|
||||
|
||||
console.warn('');
|
||||
console.warn(`⚠ 无法在本进程绑定 ${plazaPublicPort} 端口,${plazaPublicBase} 暂不可用`);
|
||||
console.warn(' 请先:sudo pnpm setup:plaza-local');
|
||||
console.warn(' 再另开终端:sudo pnpm dev:plaza-proxy');
|
||||
console.warn('');
|
||||
}
|
||||
|
||||
function plazaHostConfigured(host) {
|
||||
try {
|
||||
const hosts = fs.readFileSync('/etc/hosts', 'utf8');
|
||||
return new RegExp(`^\\s*127\\.0\\.0\\.1\\s+${host.replace('.', '\\.')}(\\s|$)`, 'm').test(hosts);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`==> 清理端口 ${portalPort} / ${vitePort} / ${plazaPort} / ${opsPort}...`);
|
||||
console.log(`==> 清理端口 ${portalPort} / ${adminPort} / ${vitePort} / ${plazaPort} / ${opsPort}...`);
|
||||
freePort(portalPort);
|
||||
freePort(adminPort);
|
||||
freePort(vitePort);
|
||||
freePort(plazaPort);
|
||||
freePort(opsPort);
|
||||
@@ -219,13 +134,6 @@ await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
ensurePlazaApp();
|
||||
|
||||
if (!plazaHostConfigured(plazaHost)) {
|
||||
console.warn('');
|
||||
console.warn(`⚠ ${plazaHost} 尚未写入 /etc/hosts,浏览器无法打开 ${plazaPublicBase}`);
|
||||
console.warn(' 请执行:sudo pnpm setup:plaza-dns');
|
||||
console.warn('');
|
||||
}
|
||||
|
||||
console.log('==> 启动 portal (server.mjs)...');
|
||||
server = spawnChild('node', ['server.mjs'], 'portal');
|
||||
|
||||
@@ -233,7 +141,12 @@ try {
|
||||
await waitFor(portalUrl, async (url) => (await fetch(`${url}/auth/status`)).ok, 'Portal');
|
||||
console.log(`==> Portal 就绪: ${portalUrl}`);
|
||||
|
||||
console.log(`==> 启动 Plaza Next.js @ ${plazaPublicBase} (bind ${plazaHost})`);
|
||||
console.log('==> 启动 memind_adm (admin-server.mjs)...');
|
||||
admin = spawnChild('node', ['admin-server.mjs'], 'admin');
|
||||
await waitFor(adminUrl, async (url) => (await fetch(`${url}/healthz`)).ok, 'Admin');
|
||||
console.log(`==> memind_adm 就绪: ${adminUrl}`);
|
||||
|
||||
console.log(`==> 启动 Plaza Next.js @ ${plazaPublicBase}/plaza`);
|
||||
plaza = spawnChild(
|
||||
'npm',
|
||||
['run', 'dev', '--', '-H', '0.0.0.0', '-p', String(plazaPort)],
|
||||
@@ -244,28 +157,25 @@ try {
|
||||
await waitFor(
|
||||
`http://127.0.0.1:${plazaPort}`,
|
||||
async (url) => {
|
||||
const res = await fetch(`${url}/plaza`, {
|
||||
headers: { Host: `${plazaHost}:${plazaPort}` },
|
||||
});
|
||||
const res = await fetch(`${url}/plaza`);
|
||||
return res.ok || res.status === 307 || res.status === 308;
|
||||
},
|
||||
'Plaza',
|
||||
80,
|
||||
);
|
||||
console.log(`==> Plaza 内部就绪: http://127.0.0.1:${plazaPort}/plaza`);
|
||||
await ensurePlazaPublicProxy();
|
||||
console.log(`==> Plaza 就绪: http://127.0.0.1:${plazaPort}/plaza`);
|
||||
|
||||
console.log(`==> 启动 Ops 后台 @ ${opsUrl}/ops/`);
|
||||
console.log(`==> 启动 Ops 后台 @ http://127.0.0.1:${opsPort}/ops/`);
|
||||
ops = spawnChild('npm', ['run', 'dev'], 'ops', opsDir, opsEnv);
|
||||
await waitFor(
|
||||
`http://localhost:${opsPort}`,
|
||||
`http://127.0.0.1:${opsPort}`,
|
||||
async (url) => (await fetch(`${url}/ops/`)).ok,
|
||||
'Ops',
|
||||
40,
|
||||
);
|
||||
console.log(`==> Ops 就绪: http://localhost:${opsPort}/ops/`);
|
||||
console.log(`==> Ops 就绪: http://127.0.0.1:${opsPort}/ops/`);
|
||||
|
||||
console.log(`==> 启动 Vite @ http://localhost:${vitePort}`);
|
||||
console.log(`==> 启动 Vite @ http://127.0.0.1:${vitePort}`);
|
||||
vite = spawnChild('npx', ['vite'], 'vite', root, viteEnv);
|
||||
await waitFor(
|
||||
`http://127.0.0.1:${vitePort}`,
|
||||
@@ -278,16 +188,15 @@ try {
|
||||
},
|
||||
'Vite',
|
||||
);
|
||||
console.log(`==> Vite 就绪: http://localhost:${vitePort}`);
|
||||
console.log(`==> Vite 就绪: http://127.0.0.1:${vitePort}`);
|
||||
|
||||
console.log('');
|
||||
console.log('本地服务:');
|
||||
console.log(` MindSpace UI http://localhost:${vitePort}/?preview=mindspace`);
|
||||
console.log(` Plaza 广场 ${plazaPublicBase}/plaza`);
|
||||
console.log(` Plaza 内部 http://127.0.0.1:${plazaPort}/plaza`);
|
||||
console.log(` (setup) sudo pnpm setup:plaza-local && sudo pnpm dev:plaza-proxy`);
|
||||
console.log(` Ops 审核后台 http://localhost:${opsPort}/ops/`);
|
||||
console.log(` MindSpace UI http://127.0.0.1:${vitePort}/?preview=mindspace`);
|
||||
console.log(` Plaza 广场 http://127.0.0.1:${plazaPort}/plaza`);
|
||||
console.log(` Ops 审核后台 http://127.0.0.1:${opsPort}/ops/`);
|
||||
console.log(` API / Portal ${portalUrl}`);
|
||||
console.log(` memind_adm ${adminUrl}`);
|
||||
console.log('');
|
||||
console.log('首次使用 Ops:先登录 MindSpace,再执行 node scripts/grant-ops-role.mjs admin ops_admin');
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user