Skip to content

Commit

Permalink
log requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nimarion committed Jun 18, 2024
1 parent d1d2727 commit f744cea
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module } from '@nestjs/common';
import { AthletesModule } from './athletes/athletes.module';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { SentryInterceptor } from './sentry-interceptor/sentry-interceptor.interceptor';
Expand All @@ -8,6 +8,8 @@ import { HealthModule } from './health/health.module';
import { DisciplinesModule } from './disciplines/disciplines.module';
import { AthleteRepresentativesModule } from './athlete_representatives/athlete_representatives.module';
import { GraphqlModule } from './graphql/graphql.module';
import { CompetitionsModule } from './competitions/competitions.module';
import { LoggerMiddleware } from './logger.middleware';

@Module({
imports: [
Expand All @@ -20,6 +22,7 @@ import { GraphqlModule } from './graphql/graphql.module';
DisciplinesModule,
AthleteRepresentativesModule,
GraphqlModule,
CompetitionsModule,
],
providers: [
{
Expand All @@ -28,4 +31,8 @@ import { GraphqlModule } from './graphql/graphql.module';
},
],
})
export class AppModule {}
export class AppModule {
configure(consumer: MiddlewareConsumer): void {
consumer.apply(LoggerMiddleware).forRoutes('*');
}
}
25 changes: 25 additions & 0 deletions src/competitions/competition.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ApiProperty } from '@nestjs/swagger';

export class CompetitionOrganiserInfo {
@ApiProperty()
websiteUrl: string;
@ApiProperty()
liveStreamUrl: string;
@ApiProperty()
resultsUrl: string;
@ApiProperty()
events: any[];
@ApiProperty()
contactPersons: ContactPerson[];
}

export class ContactPerson {
@ApiProperty()
email: string;
@ApiProperty()
name: string;
@ApiProperty()
phone: string;
@ApiProperty()
role: string;
}
24 changes: 24 additions & 0 deletions src/competitions/competitions.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller, Get, Param } from '@nestjs/common';
import { CompetitionsService } from './competitions.service';

@Controller('competitions')
export class CompetitionsController {
constructor(private readonly competitionsService: CompetitionsService) {}

@Get()
findAll() {
return null;
}

@Get(':id')
findOne(@Param('id') id: string) {
console.log(id);
return null;
}

@Get(':id/organiser')
findOrganiser(@Param('id') id: string) {
console.log(id);
return null;
}
}
10 changes: 10 additions & 0 deletions src/competitions/competitions.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { CompetitionsService } from './competitions.service';
import { CompetitionsController } from './competitions.controller';
import { GraphqlService } from 'src/graphql/graphql.service';

@Module({
controllers: [CompetitionsController],
providers: [CompetitionsService, GraphqlService],
})
export class CompetitionsModule {}
16 changes: 16 additions & 0 deletions src/competitions/competitions.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class CompetitionsService {
findAll() {
return `This action returns all competitions`;
}

findOne(id: number) {
return `This action returns a #${id} competition`;
}

remove(id: number) {
return `This action removes a #${id} competition`;
}
}
23 changes: 23 additions & 0 deletions src/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
private logger = new Logger('HTTP');

use(request: Request, response: Response, next: NextFunction): void {
const { method, originalUrl } = request;
const userAgent = request.get('user-agent') || '';

response.on('finish', () => {
const { statusCode } = response;
const contentLength = response.get('content-length');

this.logger.log(
`${method} ${originalUrl} ${statusCode} ${contentLength} - ${userAgent}`,
);
});

next();
}
}

0 comments on commit f744cea

Please sign in to comment.