84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
import { APP_FILTER, APP_GUARD } from '@nestjs/core';
|
|
|
|
import { validate } from './config/env.validation';
|
|
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
|
|
import { JwtAuthGuard } from './common/guards/jwt-auth.guard';
|
|
import { RolesGuard } from './common/guards/roles.guard';
|
|
import { CsrfMiddleware } from './common/middleware/csrf.middleware';
|
|
|
|
import { AuthModule } from './auth/auth.module';
|
|
import { UsersModule } from './users/users.module';
|
|
import { BlogPostsModule } from './blog-posts/blog-posts.module';
|
|
import { TokensModule } from './tokens/tokens.module';
|
|
|
|
import { User } from './users/entities/user.entity';
|
|
import { BlogPost } from './blog-posts/entities/blog-post.entity';
|
|
import { RefreshToken } from './tokens/entities/refresh-token.entity';
|
|
import { MagicLinkToken } from './tokens/entities/magic-link-token.entity';
|
|
import { PasswordResetToken } from './tokens/entities/password-reset-token.entity';
|
|
import { OAuthAccount } from './tokens/entities/oauth-account.entity';
|
|
|
|
@Module({
|
|
imports: [
|
|
ConfigModule.forRoot({
|
|
isGlobal: true,
|
|
envFilePath: '.env',
|
|
validate,
|
|
}),
|
|
TypeOrmModule.forRootAsync({
|
|
imports: [ConfigModule],
|
|
useFactory: (configService: ConfigService) => ({
|
|
type: 'postgres',
|
|
host: configService.get<string>('DB_HOST'),
|
|
port: configService.get<number>('DB_PORT'),
|
|
username: configService.get<string>('DB_USER'),
|
|
password: configService.get<string>('DB_PASSWORD'),
|
|
database: configService.get<string>('DB_NAME'),
|
|
ssl:
|
|
configService.get<string>('DB_SSL') === 'true'
|
|
? { rejectUnauthorized: false }
|
|
: false,
|
|
entities: [
|
|
User,
|
|
BlogPost,
|
|
RefreshToken,
|
|
MagicLinkToken,
|
|
PasswordResetToken,
|
|
OAuthAccount,
|
|
],
|
|
synchronize: configService.get<string>('NODE_ENV') !== 'production',
|
|
logging: configService.get<string>('NODE_ENV') === 'development',
|
|
}),
|
|
inject: [ConfigService],
|
|
}),
|
|
AuthModule,
|
|
UsersModule,
|
|
BlogPostsModule,
|
|
TokensModule,
|
|
],
|
|
providers: [
|
|
{
|
|
provide: APP_FILTER,
|
|
useClass: AllExceptionsFilter,
|
|
},
|
|
{
|
|
provide: APP_GUARD,
|
|
useClass: JwtAuthGuard,
|
|
},
|
|
{
|
|
provide: APP_GUARD,
|
|
useClass: RolesGuard,
|
|
},
|
|
],
|
|
})
|
|
export class AppModule implements NestModule {
|
|
configure(consumer: MiddlewareConsumer) {
|
|
consumer
|
|
.apply(CsrfMiddleware)
|
|
.forRoutes({ path: '*', method: RequestMethod.ALL });
|
|
}
|
|
}
|