diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/api.js | 26 | ||||
-rw-r--r-- | src/routers/tmwa/middlewares/account.js | 4 |
2 files changed, 27 insertions, 3 deletions
@@ -7,14 +7,38 @@ if (process.env.npm_package_config_port === undefined) { process.exit(1); } +const send_hook = (msg) => { + const req = new Request(process.env.npm_package_config_logger_webhook, { + method: "POST", + cache: "no-cache", + redirect: "follow", + headers: { + "Accept": "application/json", + "Content-Type": "application/json", + }, + body: JSON.stringify({ + content: msg, + }), + }); + fetch(req); + console.log(msg); +}; + +const logger = { + log: msg => send_hook(`${msg}`), + info: msg => send_hook(`ℹ ${msg}`), + warn: msg => send_hook(`⚠ ${msg}`), + error: msg => send_hook(`❌ ${msg}`), +}; // config common to all routers: api.locals = Object.assign({ rate_limiting: new Set(), // XXX: or do we want routers to each have their own rate limiter? mailer: { from: process.env.npm_package_config_mailer_from, - } + }, + logger: logger, }, api.locals); diff --git a/src/routers/tmwa/middlewares/account.js b/src/routers/tmwa/middlewares/account.js index 3c82dbc..bc96b6c 100644 --- a/src/routers/tmwa/middlewares/account.js +++ b/src/routers/tmwa/middlewares/account.js @@ -142,7 +142,7 @@ const create_account = (req, res, next) => { res.status(201).json({ status: "success" }); - console.info("TMWA.account: an account was created: %s [%s]", req.body.username, req.ip); + req.app.locals.logger.info("TMWA.account: an account was created: %s [%s]", req.body.username, req.ip); req.app.locals.rate_limiting.add(req.ip); setTimeout(() => req.app.locals.rate_limiting.delete(req.ip), 300000); @@ -155,7 +155,7 @@ const create_account = (req, res, next) => { subject: "The Mana World account registration", text: `Your account (\"${req.body.username}\") was created successfully.\nHave fun playing The Mana World!` }, (err, info) => { - console.info("TMWA.account: sent account creation email: %s %s", req.body.username, info.messageId); + req.app.locals.logger.info("TMWA.account: sent account creation email: %s %s", req.body.username, info.messageId); }); }); child.stdin.end(); |