think/packages/server/src/main.ts

65 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-05-16 09:23:59 +00:00
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
2023-04-09 05:24:34 +00:00
import { HttpResponseExceptionFilter } from '@exceptions/http-response.exception';
import { FILE_DEST, FILE_ROOT_PATH } from '@helpers/file.helper/local.client';
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';
import * as bodyParser from 'body-parser';
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';
async function bootstrap() {
2022-07-30 06:08:49 +00:00
const app = await NestFactory.create(AppModule, {
logger: ['warn', 'error'],
});
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-06-04 09:01:33 +00:00
app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({ limit: '100mb', extended: true }));
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
2022-06-04 09:01:33 +00:00
if (config.get('oss.local.enable')) {
const serverStatic = express.static(FILE_ROOT_PATH);
app.use(FILE_DEST, (req, res, next) => {
res.header('Cross-Origin-Resource-Policy', 'cross-origin');
return serverStatic(req, res, next);
});
}
2022-05-21 06:54:53 +00:00
await app.listen(port);
2022-06-04 09:01:33 +00:00
2022-05-21 06:54:53 +00:00
console.log(`[think] 主服务启动成功,端口:${port}`);
2022-02-20 11:51:55 +00:00
}
2022-06-22 11:03:11 +00:00
bootstrap();