2022-02-20 11:51:55 +00:00
|
|
|
import { HttpResponseExceptionFilter } from '@exceptions/http-response.exception';
|
2022-05-22 12:40:55 +00:00
|
|
|
import { IS_PRODUCTION } from '@helpers/env.helper';
|
2022-05-16 09:23:59 +00:00
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
import { NestFactory } from '@nestjs/core';
|
2022-02-20 11:51:55 +00:00
|
|
|
import { ValidationPipe } from '@pipes/validation.pipe';
|
2022-05-16 09:23:59 +00:00
|
|
|
import { HttpResponseTransformInterceptor } from '@transforms/http-response.transform';
|
2022-05-22 02:32:20 +00:00
|
|
|
import * as compression from 'compression';
|
2022-05-22 15:40:22 +00:00
|
|
|
import * as cookieParser from 'cookie-parser';
|
2022-05-16 09:23:59 +00:00
|
|
|
import * as express from 'express';
|
2022-05-26 05:18:36 +00:00
|
|
|
import rateLimit from 'express-rate-limit';
|
2022-05-16 09:23:59 +00:00
|
|
|
import helmet from 'helmet';
|
|
|
|
|
2022-02-20 11:51:55 +00:00
|
|
|
import { AppModule } from './app.module';
|
2022-05-22 12:40:55 +00:00
|
|
|
import { AppClusterService } from './app-cluster.service';
|
2022-02-20 11:51:55 +00:00
|
|
|
|
|
|
|
async function bootstrap() {
|
2022-05-24 09:33:30 +00:00
|
|
|
const app = await NestFactory.create(AppModule);
|
2022-02-20 11:51:55 +00:00
|
|
|
const config = app.get(ConfigService);
|
2022-05-21 06:54:53 +00:00
|
|
|
const port = config.get('server.port') || 5002;
|
2022-02-20 11:51:55 +00:00
|
|
|
|
2022-05-22 15:40:22 +00:00
|
|
|
app.enableCors({
|
2022-05-24 09:33:30 +00:00
|
|
|
origin: config.get('client.siteUrl'),
|
2022-05-22 15:40:22 +00:00
|
|
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
|
|
|
|
credentials: true,
|
|
|
|
});
|
2022-05-26 05:18:36 +00:00
|
|
|
|
|
|
|
config.get('server.enableRateLimit') &&
|
|
|
|
app.use(
|
|
|
|
rateLimit({
|
|
|
|
windowMs: config.get('server.rateLimitWindowMs'),
|
|
|
|
max: config.get('server.rateLimitMax'),
|
|
|
|
})
|
|
|
|
);
|
2022-05-22 15:40:22 +00:00
|
|
|
app.use(cookieParser());
|
2022-05-22 02:32:20 +00:00
|
|
|
app.use(compression());
|
2022-02-20 11:51:55 +00:00
|
|
|
app.use(helmet());
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
app.useGlobalFilters(new HttpResponseExceptionFilter());
|
|
|
|
app.useGlobalInterceptors(new HttpResponseTransformInterceptor());
|
|
|
|
app.useGlobalPipes(new ValidationPipe());
|
|
|
|
app.setGlobalPrefix(config.get('server.prefix') || '/');
|
2022-05-21 06:54:53 +00:00
|
|
|
|
|
|
|
await app.listen(port);
|
|
|
|
console.log(`[think] 主服务启动成功,端口:${port}`);
|
2022-02-20 11:51:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 12:40:55 +00:00
|
|
|
IS_PRODUCTION ? AppClusterService.clusterize(bootstrap) : bootstrap();
|