78 lines
3.0 KiB
TypeScript
78 lines
3.0 KiB
TypeScript
import 'reflect-metadata';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { createServer } from 'node:http';
|
|
import type { NestExpressApplication } from '@nestjs/platform-express';
|
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
import { AppModule } from './app.module';
|
|
import { MetricsService } from './metrics/metrics.service';
|
|
import { OpenApiModule } from './open-api/open-api.module';
|
|
import { configureHttpBodyParsers } from './http-body-limits';
|
|
import { configureApiHttpServerTimeouts } from './http-server-timeouts';
|
|
|
|
Object.defineProperty(BigInt.prototype, 'toJSON', {
|
|
configurable: true,
|
|
value(this: bigint) {
|
|
const result = Number(this);
|
|
if (!Number.isSafeInteger(result)) {
|
|
throw new RangeError('金额超过 JavaScript 安全整数范围');
|
|
}
|
|
return result;
|
|
},
|
|
});
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, { rawBody: true, bodyParser: false });
|
|
app.setGlobalPrefix('api');
|
|
configureHttpBodyParsers(app);
|
|
|
|
const swaggerConfig = new DocumentBuilder()
|
|
.setTitle('聆界短信平台 API')
|
|
.setDescription('聆界短信平台 API')
|
|
.setVersion('0.1.0')
|
|
.build();
|
|
const document = SwaggerModule.createDocument(app, swaggerConfig);
|
|
SwaggerModule.setup('api/docs', app, document);
|
|
|
|
const clientDocument = SwaggerModule.createDocument(
|
|
app,
|
|
new DocumentBuilder()
|
|
.setTitle('聆界短信平台 HTTP 客户接口')
|
|
.setDescription('单条短信发送、短信状态查询、上行短信查询及回调验签接口')
|
|
.setVersion('1.0.0')
|
|
.build(),
|
|
{ include: [OpenApiModule] },
|
|
);
|
|
clientDocument.paths = Object.fromEntries(
|
|
Object.entries(clientDocument.paths).filter(([path]) => path.startsWith('/api/openapi/v1/')),
|
|
);
|
|
SwaggerModule.setup('api/client-docs', app, clientDocument, { ui: false });
|
|
|
|
const port = Number(process.env.API_PORT ?? 3000);
|
|
// 生产环境只允许 Nginx 访问管理 API;显式绑定回环,避免默认的全网卡监听绕过入口鉴权与限流。
|
|
const host = process.env.API_HOST?.trim() || '127.0.0.1';
|
|
const apiServer = await app.listen(port, host);
|
|
configureApiHttpServerTimeouts(apiServer, process.env);
|
|
|
|
const metrics = app.get(MetricsService);
|
|
const metricsHost = process.env.API_METRICS_HOST?.trim() || '127.0.0.1';
|
|
const metricsPort = Number(process.env.API_METRICS_PORT ?? 9464);
|
|
const metricsServer = createServer((request, response) => {
|
|
if (request.method !== 'GET' || request.url !== '/metrics') {
|
|
response.writeHead(404).end();
|
|
return;
|
|
}
|
|
response.writeHead(200, {
|
|
'Content-Type': 'text/plain; version=0.0.4; charset=utf-8',
|
|
'Cache-Control': 'no-store',
|
|
});
|
|
response.end(metrics.render());
|
|
});
|
|
// Metrics use a dedicated loopback listener so Nginx cannot accidentally expose them through /api/.
|
|
await new Promise<void>((resolve, reject) => {
|
|
metricsServer.once('error', reject);
|
|
metricsServer.listen(metricsPort, metricsHost, resolve);
|
|
});
|
|
}
|
|
|
|
void bootstrap();
|