diff --git a/controllers/middlewares/DebugLogging.js b/controllers/middlewares/DebugLogging.js new file mode 100644 index 0000000..ff74c32 --- /dev/null +++ b/controllers/middlewares/DebugLogging.js @@ -0,0 +1,24 @@ +const logger = require('./../../logger') + +class RequestLogging { + constructor () { + logger.log('Initiated RequestLogging middleware.') + } + + #middleware (req, res, next) { + req.locals.debug = { + ip: req.ip, + method: req.method, + path: req.path, + path_parameters: req.path_parameters + } + logger.log(`Incoming from ${req.locals.debug.ip} -> ${req.locals.debug.method} ${req.locals.debug.path}`) + return next() + } + + get middleware () { + return this.#middleware.bind(this) + } +} + +module.exports = RequestLogging diff --git a/lolisafe.js b/lolisafe.js index 32a53e1..75b3d79 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -55,6 +55,7 @@ paths.initSync() const utils = require('./controllers/utilsController') // Middlewares +const DebugLogging = require('./controllers/middlewares/DebugLogging') const ExpressCompat = require('./controllers/middlewares/ExpressCompat') const NunjucksRenderer = require('./controllers/middlewares/NunjucksRenderer') const RateLimiter = require('./controllers/middlewares/RateLimiter') @@ -71,6 +72,12 @@ const file = require('./routes/file') const nojs = require('./routes/nojs') const player = require('./routes/player') +// Incoming requests logging (development mode) +if (utils.devmode) { + const DebugLoggingInstance = new DebugLogging() + safe.use(DebugLoggingInstance.middleware) +} + // Express-compat const expressCompatInstance = new ExpressCompat() safe.use(expressCompatInstance.middleware)