Skip to content

Commit

Permalink
feat: 몽고 db로 메세지 작성 시 ip 로깅
Browse files Browse the repository at this point in the history
  • Loading branch information
yunuo46 committed Dec 13, 2023
1 parent cd735bd commit f05ec62
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 40 deletions.
2 changes: 1 addition & 1 deletion back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0",
"@nestjs/jwt": "^10.2.0",
"@nestjs/mongoose": "^10.0.2",
"@nestjs/mongoose": "^9.2.2",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.16",
Expand Down
2 changes: 2 additions & 0 deletions back/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { APP_INTERCEPTOR } from '@nestjs/core';
import { ControllerLoggerInterceptor } from './common/interceptors/log.interceptor';
import { ThrottlerModule } from '@nestjs/throttler';
import throttlerConfig from './config/throttler-config';
import { MongooseModule } from '@nestjs/mongoose';
//import { ThrottlerBehindProxyGuard } from './common/guards/throttler.guard';

@Module({
imports: [
TypeOrmModule.forRoot(typeOrmConfig),
ThrottlerModule.forRoot(throttlerConfig),
MongooseModule.forRoot(process.env.MONGO_URI),
UserModule,
AuthModule,
JwtModule.register({
Expand Down
28 changes: 4 additions & 24 deletions back/src/common/interceptors/ip-log.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,21 @@ import {
HttpStatus
} from '@nestjs/common';
import { Observable, map } from 'rxjs';
import * as winston from 'winston';
import * as DailyRotateFile from 'winston-daily-rotate-file';
import { IpService } from '../mongo/ip.service';

@Injectable()
export class IPLoggerInterceptor implements NestInterceptor {
private logger: winston.Logger;

constructor() {
this.logger = winston.createLogger({
transports: [
new DailyRotateFile({
filename: '../ip-log/%DATE%.json',
datePattern: 'YYYY-MM-DD',
zippedArchive: false,
maxSize: '20m',
maxFiles: '365d'
})
]
});
}
constructor(private readonly ipService: IpService) {}

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest();
const clientIp = request.headers['x-forwarded-for'] || request.ip;
const logData = {
level: 'info',
clientIp
};

const response = context.switchToHttp().getResponse();
return next.handle().pipe(
map(data => {
map(async data => {
if (response.statusCode === HttpStatus.CREATED) {
const id = data.id;
this.logger.info({ ...logData, id });
await this.ipService.logIp(clientIp, id);
}
return data;
})
Expand Down
17 changes: 17 additions & 0 deletions back/src/common/mongo/ip.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Prop, Schema, SchemaFactory, SchemaOptions } from '@nestjs/mongoose';
import { Document } from 'mongoose';

const options: SchemaOptions = {
timestamps: { updatedAt: false }
};

@Schema(options)
export class Ip extends Document {
@Prop({ required: true })
ip: string;

@Prop({ required: true, unique: true })
message_id: number;
}

export const IPSchema = SchemaFactory.createForClass(Ip);
14 changes: 14 additions & 0 deletions back/src/common/mongo/ip.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Ip } from './ip.schema';

@Injectable()
export class IpService {
constructor(@InjectModel(Ip.name) private readonly ipModel: Model<Ip>) {}

async logIp(ip: string, message_id: number): Promise<Ip> {
const createdIp = new this.ipModel({ ip, message_id });
return createdIp.save();
}
}
13 changes: 0 additions & 13 deletions back/src/common/mongo/models/log.model.ts

This file was deleted.

8 changes: 6 additions & 2 deletions back/src/modules/message/message.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { LetterEntity } from './entity/letter.entity';
import { SnowballEntity } from '../snowball/entity/snowball.entity';
import { DecorationPrefixEntity } from '../snowball/entity/decoration-prefix.entity';
import { UserEntity } from '../user/entity/user.entity';
import { IpService } from 'src/common/mongo/ip.service';
import { MongooseModule } from '@nestjs/mongoose';
import { IPSchema } from 'src/common/mongo/ip.schema';
@Module({
imports: [
TypeOrmModule.forFeature([
Expand All @@ -17,10 +20,11 @@ import { UserEntity } from '../user/entity/user.entity';
UserEntity,
LetterEntity,
DecorationPrefixEntity
])
]),
MongooseModule.forFeature([{ name: 'Ip', schema: IPSchema }])
],
controllers: [MessageController],
providers: [MessageService, ClovaService, JWTGuard],
providers: [MessageService, ClovaService, JWTGuard, IpService],
exports: [MessageService, TypeOrmModule]
})
export class MessageModule {}

0 comments on commit f05ec62

Please sign in to comment.