-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathapp.module.ts
More file actions
87 lines (83 loc) · 3.59 KB
/
Copy pathapp.module.ts
File metadata and controls
87 lines (83 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { APP_GUARD } from '@nestjs/core';
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
import { ThrottlerModule } from '@nestjs/throttler';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth-module/auth.module';
import { OpenAPIController } from './common/openapi-publish.controller';
import { ThrottlerGuard } from './auth/throttler.guard';
import { JwtAuthGuard } from './auth-module/guards/jwt-auth.guard';
import { RolesGuard } from './auth-module/guards/roles.guard';
import { PublicGuard } from './auth-module/guards/public.guard';
import { LoggingModule } from './common/logging.module';
import { CorrelationIdMiddleware } from './common/middleware/correlation-id.middleware';
import { LoggingMiddleware } from './common/middleware/logging.middleware';
import { MetricsMiddleware } from './common/middleware/metrics.middleware';
import { CreatorsModule } from './creators/creators.module';
import { HealthModule } from './health/health.module';
import { MetricsModule } from './metrics/metrics.module';
import { NotificationsModule } from './notifications/notifications.module';
import { SubscriptionsModule } from './subscriptions/subscriptions.module';
import { ModerationModule } from './moderation/moderation.module';
import { IdempotencyModule } from './idempotency/idempotency.module';
import { IdempotencyMiddleware } from './idempotency/idempotency.middleware';
import { ReferralModule } from './referral/referral.module';
import { CsrfModule } from './csrf/csrf.module';
import { CsrfMiddleware } from './common/middleware/csrf.middleware';
/** Routes where idempotency protection is enforced. */
const IDEMPOTENCY_ROUTES = [
{ path: 'v1/creators/plans', method: RequestMethod.POST },
{ path: 'v1/subscriptions/checkout', method: RequestMethod.POST },
{ path: 'v1/posts', method: RequestMethod.POST },
{ path: 'v1/posts/:id', method: RequestMethod.PUT },
{ path: 'v1/comments', method: RequestMethod.POST },
{ path: 'v1/comments/:id', method: RequestMethod.PUT },
{ path: 'v1/conversations', method: RequestMethod.POST },
{ path: 'v1/conversations/:id/messages', method: RequestMethod.POST },
];
@Module({
imports: [
ThrottlerModule.forRoot([
{ name: 'auth', ttl: 60000, limit: 5 },
{ name: 'short', ttl: 60000, limit: 10 },
{ name: 'medium', ttl: 60000, limit: 50 },
{ name: 'long', ttl: 60000, limit: 100 },
]),
LoggingModule,
MetricsModule,
AuthModule,
CreatorsModule,
SubscriptionsModule,
NotificationsModule,
HealthModule,
ModerationModule,
IdempotencyModule,
ReferralModule,
CsrfModule,
],
controllers: [AppController, OpenAPIController],
providers: [
AppService,
{ provide: APP_GUARD, useClass: ThrottlerGuard },
{ provide: APP_GUARD, useClass: JwtAuthGuard },
{ provide: APP_GUARD, useClass: RolesGuard },
{ provide: APP_GUARD, useClass: PublicGuard },
],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(CorrelationIdMiddleware, LoggingMiddleware, MetricsMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
consumer.apply(IdempotencyMiddleware).forRoutes(...IDEMPOTENCY_ROUTES);
// CSRF double-submit cookie protection on all state-mutating routes
consumer
.apply(CsrfMiddleware)
.forRoutes(
{ path: '*', method: RequestMethod.POST },
{ path: '*', method: RequestMethod.PUT },
{ path: '*', method: RequestMethod.PATCH },
{ path: '*', method: RequestMethod.DELETE },
);
}
}