diff --git a/src/main.ts b/src/main.ts index 62b079b..0638737 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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'; @@ -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)); diff --git a/src/middlewares/check-whether-client-is-github.middleware.ts b/src/middlewares/check-whether-client-is-github.middleware.ts index 1fa6f93..354e7f2 100644 --- a/src/middlewares/check-whether-client-is-github.middleware.ts +++ b/src/middlewares/check-whether-client-is-github.middleware.ts @@ -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'); diff --git a/src/middlewares/logger.middleware.ts b/src/middlewares/logger.middleware.ts new file mode 100644 index 0000000..c3de467 --- /dev/null +++ b/src/middlewares/logger.middleware.ts @@ -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(); +}