server: add rateLimit

pull/60/head
fantasticit 2022-05-26 13:18:36 +08:00
parent 04ae4949e9
commit cd19310974
2 changed files with 12 additions and 0 deletions

View File

@ -20,6 +20,9 @@ server:
collaborationPort: 5003
maxDocumentVersion: 20 # 最大版本记录数
logRetainDays: 3 # 日志保留天数,比如只保留近三天日志
enableRateLimit: true # 是否限流
rateLimitWindowMs: 60000 # 限流时间
rateLimitMax: 1000 # 单位限流时间内单个 up 最大访问数量
# 数据库配置
db:

View File

@ -7,6 +7,7 @@ import { HttpResponseTransformInterceptor } from '@transforms/http-response.tran
import * as compression from 'compression';
import * as cookieParser from 'cookie-parser';
import * as express from 'express';
import rateLimit from 'express-rate-limit';
import helmet from 'helmet';
import { AppModule } from './app.module';
@ -22,6 +23,14 @@ async function bootstrap() {
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true,
});
config.get('server.enableRateLimit') &&
app.use(
rateLimit({
windowMs: config.get('server.rateLimitWindowMs'),
max: config.get('server.rateLimitMax'),
})
);
app.use(cookieParser());
app.use(compression());
app.use(helmet());