Spaces:
Sleeping
Sleeping
Trần Viết Sơn
commited on
Commit
·
15f4599
1
Parent(s):
3cdf53d
feat: initial commit
Browse files
backend/README.md
CHANGED
@@ -33,4 +33,3 @@ $ npm run start:dev
|
|
33 |
|
34 |
We currently don't care about test
|
35 |
|
36 |
-
Notice: We use `synchronize: true (src/config/database)`, so that no need to use migration
|
|
|
33 |
|
34 |
We currently don't care about test
|
35 |
|
|
backend/src/app.module.ts
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
-
import { Module } from '@nestjs/common';
|
2 |
import { AppController } from './app.controller';
|
3 |
import { AppService } from './app.service';
|
4 |
import { ConfigModule } from '@nestjs/config';
|
5 |
import { TypeOrmModule } from '@nestjs/typeorm';
|
6 |
import { configuration } from './config/config.js';
|
7 |
import { DatabaseConfigService } from './config/database.js';
|
|
|
8 |
|
9 |
@Module({
|
10 |
imports: [
|
@@ -20,4 +21,8 @@ import { DatabaseConfigService } from './config/database.js';
|
|
20 |
controllers: [AppController],
|
21 |
providers: [AppService],
|
22 |
})
|
23 |
-
export class AppModule {
|
|
|
|
|
|
|
|
|
|
1 |
+
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
|
2 |
import { AppController } from './app.controller';
|
3 |
import { AppService } from './app.service';
|
4 |
import { ConfigModule } from '@nestjs/config';
|
5 |
import { TypeOrmModule } from '@nestjs/typeorm';
|
6 |
import { configuration } from './config/config.js';
|
7 |
import { DatabaseConfigService } from './config/database.js';
|
8 |
+
import { AppLoggerMiddleware } from './common/middlewares/app-logger.middleware.js';
|
9 |
|
10 |
@Module({
|
11 |
imports: [
|
|
|
21 |
controllers: [AppController],
|
22 |
providers: [AppService],
|
23 |
})
|
24 |
+
export class AppModule implements NestModule {
|
25 |
+
configure(consumer: MiddlewareConsumer): void {
|
26 |
+
consumer.apply(AppLoggerMiddleware).forRoutes('*');
|
27 |
+
}
|
28 |
+
}
|
backend/src/common/middlewares/app-logger.middleware.ts
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Injectable, NestMiddleware, Logger } from '@nestjs/common';
|
2 |
+
|
3 |
+
import { Request, Response, NextFunction } from 'express';
|
4 |
+
|
5 |
+
@Injectable()
|
6 |
+
export class AppLoggerMiddleware implements NestMiddleware {
|
7 |
+
private logger = new Logger();
|
8 |
+
|
9 |
+
use(request: Request, response: Response, next: NextFunction): void {
|
10 |
+
const { ip, method, baseUrl: url } = request;
|
11 |
+
const userAgent = request.get('user-agent') || '';
|
12 |
+
|
13 |
+
response.on('close', () => {
|
14 |
+
const { statusCode } = response;
|
15 |
+
const contentLength = response.get('content-length');
|
16 |
+
|
17 |
+
const queries = Object.entries(request.query);
|
18 |
+
const appendUrl = queries.map(query => query.join("=")).join("&");
|
19 |
+
|
20 |
+
this.logger.log(
|
21 |
+
`${method} ${url + (queries.length ? "?" + appendUrl : "")} ${statusCode} ${contentLength} - ${userAgent} ${ip}`,
|
22 |
+
);
|
23 |
+
});
|
24 |
+
|
25 |
+
next();
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|