perf: optimize app startup and background lifecycles

This commit is contained in:
inman
2026-08-19 00:56:15 +08:00
parent 31332f6d76
commit 927e13349b
121 changed files with 4359 additions and 1400 deletions

View File

@@ -1,58 +1,11 @@
import { randomBytes } from 'node:crypto';
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
import { createServer, type Server } from 'node:http';
import { getPort } from '../utils/config';
import { logger } from '../utils/logger';
import type { HostApiContext } from './context';
import { handleAiProxyRoutes } from './routes/ai-proxy';
import { handleAiHardwareRoutes } from './routes/ai-hardware';
import { handleAppRoutes } from './routes/app';
import { handleAuthRoutes } from './routes/auth';
import { handleOpencodeRoutes } from './routes/opencode';
import { handleImageWorkspaceRoutes } from './routes/image-workspace';
import { handleImagePromptMuseumRoutes } from './routes/image-prompt-museum';
import { handleLearningRoutes } from './routes/learning';
import { handleWorksRoutes } from './routes/works';
import { handleUserSyncRoutes } from './routes/user-sync';
import { handleSettingsRoutes } from './routes/settings';
import { handleProviderRoutes } from './routes/providers';
import { handleLogRoutes } from './routes/logs';
import { handleUsageRoutes } from './routes/usage';
import { handleFileRoutes } from './routes/files';
import { handleMeowaGameAssetsRoutes } from './routes/meowa-game-assets';
import { handleAgentBrowserRoutes } from './routes/agent-browser';
import { sendJson, setCorsHeaders, requireJsonContentType } from './route-utils';
import { rotateRendererCapability } from './renderer-capability';
type RouteHandler = (
req: IncomingMessage,
res: ServerResponse,
url: URL,
ctx: HostApiContext,
) => Promise<boolean>;
const coreRouteHandlers: RouteHandler[] = [
handleAiProxyRoutes,
handleAiHardwareRoutes,
handleAppRoutes,
handleAuthRoutes,
handleImageWorkspaceRoutes,
handleImagePromptMuseumRoutes,
handleLearningRoutes,
handleWorksRoutes,
handleAgentBrowserRoutes,
handleUserSyncRoutes,
handleOpencodeRoutes,
handleSettingsRoutes,
handleProviderRoutes,
handleFileRoutes,
handleMeowaGameAssetsRoutes,
handleLogRoutes,
handleUsageRoutes,
];
function buildRouteHandlers(): RouteHandler[] {
return coreRouteHandlers;
}
import { hostApiRouteHandlers } from './route-handlers';
/**
* Per-session secret token used to authenticate Host API requests.
@@ -110,8 +63,7 @@ export function startHostApiServer(ctx: HostApiContext, port = getPort('NIANCODE
return;
}
const routeHandlers = buildRouteHandlers();
for (const handler of routeHandlers) {
for (const handler of hostApiRouteHandlers) {
if (await handler(req, res, requestUrl, ctx)) {
return;
}