- 服务端新增 /__ws(原生 RFC6455 实现,零第三方依赖,复用同一 HTTP 端口),支持在线人数广播、数据变更广播、心跳保活与断线清理 - 各写接口(config/routes/api-list/mock-files/mock-groups/ip-rules)写入成功后广播 mutation 事件,按 X-Client-Id 请求头跳过发起方,避免自我提示 - 通知中的操作人取该 IP「个性化配置」的备注,显示为 [董] 更新了路由配置,无备注时回退显示 IP - 管理页顶栏新增「在线 N 人」徽标(悬浮显示各客户端备注/IP),断线指数退避重连,重连成功后自动补刷全部数据 - Mock 数据弹窗新增「JSON 格式化」,支持修复单引号、无引号键、尾逗号等松散写法,非法内容给出具体报错位置 - 新增调试接口 GET /__ws-clients;全站文案「Mock 文件」统一改为「Mock 数据」(DB 表名与 /__mock-files 路径保持不变)
74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import * as fs from "fs";
|
|
import * as path from "path";
|
|
import { migrateApiListFromJsonIfNeeded } from "./src/api-list";
|
|
import { loadConfig } from "./src/config";
|
|
import { getActiveRoutes } from "./src/route-matching";
|
|
import { initDatabase } from "./src/db";
|
|
import { initMockFilesFromFsIfNeeded } from "./src/mock-files";
|
|
import {
|
|
createProxyServer,
|
|
getEffectiveTargetHttps,
|
|
getEffectiveTargetPort,
|
|
} from "./src/proxy";
|
|
import { state } from "./src/state";
|
|
import { attachWebSocketServer, WS_PATH } from "./src/ws";
|
|
|
|
function startServer(): void {
|
|
const proxyServer = createProxyServer();
|
|
attachWebSocketServer(proxyServer);
|
|
proxyServer.listen(state.config.proxyPort, "0.0.0.0", () => {
|
|
const targetPort = getEffectiveTargetPort();
|
|
const https = getEffectiveTargetHttps();
|
|
const proto = https ? "https" : "http";
|
|
const defaultPort = https ? 443 : 80;
|
|
console.log(`========================================`);
|
|
console.log(
|
|
`代理服务器运行在: http://localhost:${state.config.proxyPort}`,
|
|
);
|
|
console.log(
|
|
`目标服务器: ${proto}://${state.config.targetHost}${targetPort !== defaultPort ? `:${targetPort}` : ""}`,
|
|
);
|
|
console.log(`数据库: data/mock-mappings.sqlite3`);
|
|
const activeRoutes = getActiveRoutes();
|
|
console.log(
|
|
`已配置Mock路由: ${Object.keys(activeRoutes).length} 个${state.config.mockEnabled !== false ? "" : "(当前 mockEnabled=false,未生效)"}`,
|
|
);
|
|
console.log(`========================================`);
|
|
console.log(`管理接口:`);
|
|
console.log(
|
|
` GET http://localhost:${state.config.proxyPort}/__config 查看当前配置`,
|
|
);
|
|
console.log(
|
|
` POST http://localhost:${state.config.proxyPort}/__reload-config 重新加载配置`,
|
|
);
|
|
console.log(
|
|
` GET http://localhost:${state.config.proxyPort}/__admin 管理面板`,
|
|
);
|
|
console.log(
|
|
` WS ws://localhost:${state.config.proxyPort}${WS_PATH} 实时同步(在线人数/变更通知)`,
|
|
);
|
|
console.log(`========================================`);
|
|
console.log(`Mock路由列表:`);
|
|
|
|
for (const [route, file] of Object.entries(activeRoutes)) {
|
|
const filePath = path.join(__dirname, file);
|
|
const exists = fs.existsSync(filePath) ? "✓" : "✗";
|
|
console.log(` ${exists} ${route} -> ${file}`);
|
|
}
|
|
console.log(`========================================`);
|
|
});
|
|
}
|
|
|
|
async function bootstrap(): Promise<void> {
|
|
await initDatabase();
|
|
await initMockFilesFromFsIfNeeded();
|
|
await migrateApiListFromJsonIfNeeded();
|
|
await loadConfig();
|
|
startServer();
|
|
}
|
|
|
|
bootstrap().catch((error) => {
|
|
console.error("[BOOTSTRAP] 启动失败:", error);
|
|
process.exit(1);
|
|
});
|