feat: migrate gateway to nestjs

This commit is contained in:
Draculabo committed 2025-12-24 23:26:55 +08:00
1 parent c4a0fa773f
commit e7087bc4b1
18 files changed
+2182 -1161

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { logger } from '../utils/logger';
import { TokenManagerService } from './modules/proxy/token-manager.service';
let app: NestFastifyApplication | null = null;
let currentPort: number = 0;
export async function bootstrapNestServer(port: number = 8045): Promise<boolean> {
if (app) {
logger.info('NestJS server already running.');
return true;
}
try {
app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter(), {
logger: ['error', 'warn', 'log'],
});
// Enable CORS
app.enableCors();
await app.listen(port, '0.0.0.0');
currentPort = port;
logger.info(`NestJS Proxy Server running on http://localhost:${port}`);
return true;
} catch (error) {
logger.error('Failed to start NestJS server', error);
return false;
}
}
export async function stopNestServer(): Promise<boolean> {
if (app) {
try {
await app.close();
app = null;
currentPort = 0;
logger.info('NestJS server stopped.');
return true;
} catch (e) {
logger.error('Failed to stop NestJS server', e);
return false;
}
}
return true;
}
export function isNestServerRunning(): boolean {
return app !== null;
}
export async function getNestServerStatus(): Promise<{
running: boolean;
port: number;
base_url: string;
active_accounts: number;
}> {
const running = isNestServerRunning();
let activeAccounts = 0;
if (app) {
try {
const tokenManager = app.get(TokenManagerService);
activeAccounts = tokenManager.getAccountCount();
} catch (e) {
// TokenManager might not be available
}
}
return {
running,
port: currentPort,
base_url: running ? `http://localhost:${currentPort}` : '',
active_accounts: activeAccounts,
};
}