Skip to content

Commit b230b19

Browse files
committed
update code
1 parent 9e30b0d commit b230b19

File tree

8 files changed

+85
-71
lines changed

8 files changed

+85
-71
lines changed

.env.example

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ DB_USER=namdd72
88
DB_PASS=123456789
99
DB_NAME=nestjs_base
1010

11-
#Redis
11+
# Redis
1212
REDIS_HOST=localhost
1313
REDIS_PORT=6379
14-
REDIS_PASS=
14+
REDIS_PASS=xxxxx
1515

1616
# Token
1717
#ACCESS_TOKEN_SECRET=

src/app.module.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import { ConnectionsModule } from '$connections/connections.module';
99
import { WorkersModule } from '$workers/workers.module';
1010
import { EventEmitterModule } from '@nestjs/event-emitter';
1111
import { QueuesModule } from '$queues/queues.module';
12+
import { LoggerMiddleware } from '$middlewares/logger.middleware';
1213

1314
@Module({
1415
imports: [
15-
TypeOrmModule.forRoot(),
16-
EventEmitterModule.forRoot(),
1716
ConnectionsModule,
1817
ServicesModule,
1918
ControllersModule,
@@ -22,11 +21,11 @@ import { QueuesModule } from '$queues/queues.module';
2221
WorkersModule,
2322
QueuesModule,
2423
],
25-
controllers: [],
2624
providers: [ConnectionsModule, ServicesModule],
2725
})
2826
export class AppModule implements NestModule {
2927
configure(consumer: MiddlewareConsumer) {
28+
consumer.apply(LoggerMiddleware).forRoutes({ path: '*', method: RequestMethod.ALL });
3029
consumer.apply(AuthMiddleware).forRoutes({ path: '*', method: RequestMethod.ALL });
3130
}
3231
}

src/connections/connections.module.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import config from '$config';
22
import { Module } from '@nestjs/common';
33
import { RedisService } from './redis.provider';
44
import { BullModule } from '@nestjs/bull';
5+
import { TypeOrmModule } from '@nestjs/typeorm';
6+
import { EventEmitterModule } from '@nestjs/event-emitter';
57

68
@Module({
79
imports: [
10+
TypeOrmModule.forRoot(),
11+
EventEmitterModule.forRoot(),
812
BullModule.forRoot({
913
redis: {
1014
host: config.ENV.REDIS_HOST,
@@ -14,6 +18,6 @@ import { BullModule } from '@nestjs/bull';
1418
}),
1519
],
1620
providers: [RedisService],
17-
exports: [RedisService, BullModule],
21+
exports: [TypeOrmModule, EventEmitterModule, BullModule, RedisService],
1822
})
1923
export class ConnectionsModule {}

src/controllers/upload.controller.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiFile, ApiFiles, MulterConfig } from '$helpers/file.helper';
1+
import { ApiFile, ApiFiles, MulterConfig } from '$helpers/swagger.helper';
22
import {
33
Controller,
44
Get,
@@ -19,8 +19,6 @@ import {
1919
ApiParam,
2020
ApiTags,
2121
} from '@nestjs/swagger';
22-
import { diskStorage } from 'multer';
23-
import { extname } from 'path';
2422

2523
@ApiBearerAuth()
2624
@ApiTags('upload')

src/gateways/test.gateway.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { UserService } from '$services/user.service';
1313
import { RedisService } from '$connections/redis.provider';
1414
import { EventAction } from '$events/EventAction';
1515

16-
@WebSocketGateway()
16+
@WebSocketGateway({ path: '/test-gateway' })
1717
@Injectable()
1818
export class TestGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
1919
@WebSocketServer() server: Server;

src/helpers/file.helper.ts

-60
This file was deleted.

src/helpers/swagger.helper.ts

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { INestApplication } from '@nestjs/common';
2-
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
2+
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
3+
import { ApiBody, DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
4+
import { diskStorage } from 'multer';
5+
import { extname } from 'path';
36

47
export const swaggerSetup = (app: INestApplication) => {
58
const documentBuilder = new DocumentBuilder()
@@ -12,3 +15,59 @@ export const swaggerSetup = (app: INestApplication) => {
1215
const document = SwaggerModule.createDocument(app, documentBuilder);
1316
SwaggerModule.setup('api/docs', app, document);
1417
};
18+
19+
export const ApiFile = (fileName: string = 'file'): MethodDecorator => (
20+
target: any,
21+
propertyKey: string,
22+
descriptor: PropertyDescriptor,
23+
) => {
24+
ApiBody({
25+
schema: {
26+
type: 'object',
27+
properties: {
28+
[fileName]: {
29+
type: 'string',
30+
format: 'binary',
31+
},
32+
},
33+
},
34+
})(target, propertyKey, descriptor);
35+
};
36+
37+
export const ApiFiles = (fileName: string = 'files'): MethodDecorator => (
38+
target: any,
39+
propertyKey: string,
40+
descriptor: PropertyDescriptor,
41+
) => {
42+
ApiBody({
43+
type: 'multipart/form-data',
44+
required: true,
45+
schema: {
46+
type: 'object',
47+
properties: {
48+
[fileName]: {
49+
type: 'array',
50+
items: {
51+
type: 'string',
52+
format: 'binary',
53+
},
54+
},
55+
},
56+
},
57+
})(target, propertyKey, descriptor);
58+
};
59+
60+
export const MulterConfig: MulterOptions = {
61+
storage: diskStorage({
62+
destination: './uploads',
63+
filename: (req, file, cb) => {
64+
// Generating a 32 random chars long string
65+
const randomName = Array(32)
66+
.fill(null)
67+
.map(() => Math.round(Math.random() * 16).toString(16))
68+
.join('');
69+
//Calling the callback passing the random name generated with the original extension name
70+
cb(null, `${randomName}${extname(file.originalname)}`);
71+
},
72+
}),
73+
};

src/middlewares/logger.middleware.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
2+
import { Request, Response, NextFunction } from 'express';
3+
4+
@Injectable()
5+
export class LoggerMiddleware implements NestMiddleware {
6+
private logger: Logger = new Logger(LoggerMiddleware.name);
7+
8+
use(req: Request, res: Response, next: NextFunction) {
9+
this.logger.log(
10+
`Client ${req.method} ${req.originalUrl} with body ${JSON.stringify(req.body)}`,
11+
);
12+
return next();
13+
}
14+
}

0 commit comments

Comments
 (0)