69 lines
2.5 KiB
TypeScript
69 lines
2.5 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";
|
||
|
||
function startServer(): void {
|
||
const proxyServer = createProxyServer();
|
||
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(`========================================`);
|
||
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);
|
||
});
|