From fb046276622309038be3357bc93ed8f1a8568102 Mon Sep 17 00:00:00 2001
From: gumi <git@gumi.ca>
Date: Sat, 25 May 2019 23:24:51 -0400
Subject: send to webhook on successful account creation

---
 src/api.js                              | 26 +++++++++++++++++++++++++-
 src/routers/tmwa/middlewares/account.js |  4 ++--
 2 files changed, 27 insertions(+), 3 deletions(-)

(limited to 'src')

diff --git a/src/api.js b/src/api.js
index 33cfb42..cbd6c10 100644
--- a/src/api.js
+++ b/src/api.js
@@ -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();
-- 
cgit v1.2.3-70-g09d2