Skip to content

Commit

Permalink
FEATURE - Add middleware to log all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Juansecu committed Feb 25, 2024
1 parent 0cebc30 commit ed25533
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import router from './router';

import { environmentVariablesChecker } from './environment-variables.checker';

import { logger } from './middlewares/logger.middleware';

import { getPort } from './utils/get-port.util';
import { shouldUseHttps } from './utils/get-protocol.util';

Expand All @@ -19,6 +21,8 @@ const swaggerSpecification: object = swaggerJsDoc(swaggerConfig);

environmentVariablesChecker();

app.use(logger);

app.use(router);

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecification));
Expand Down
2 changes: 2 additions & 0 deletions src/middlewares/check-whether-client-is-github.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export function checkIfClientIsGitHub(
consoleLogger.verbose('Checking if client is GitHub...');

if (process.env.NODE_ENV === 'production') {
consoleLogger.info(`Client host: ${request.get('host')}`);

if (request.get('host') !== 'api.github.com') {
consoleLogger.error('Client is not GitHub');
response.status(403).send('Forbidden');
Expand Down
17 changes: 17 additions & 0 deletions src/middlewares/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NextFunction, Request, Response } from 'express';
import { Logger } from 'winston';
import { ConsoleLogger } from '../loggers/console.logger';

export function logger(
request: Request,
response: Response,
next: NextFunction
): void {
const consoleLogger: Logger = ConsoleLogger.getLogger(logger.name);

consoleLogger.verbose(
`Request received: ${request.method} ${request.originalUrl} from ${request.ip}`
);

next();
}

0 comments on commit ed25533

Please sign in to comment.