From 3bb32e1352d93f1d847928370729be89471a2c7e Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Mon, 20 Jan 2025 19:18:38 -0700 Subject: [PATCH] Add status api --- index.js | 29 +++++++++++++++++++++++++++++ log.js | 4 ++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index aba9467..5c295f5 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,35 @@ app.use((req, res, next) => { next(); }); +app.get("/health", async (req, res) => { + const status = { + webServer: 'OK', + discordClient: 'OK', + mariaDB: 'OK', + errors: [] + }; + + if (!client.isReady()) { + status.discordClient = 'FAIL'; + status.errors.push('Discord client is not ready.'); + } + + try { + const connection = await pool.getConnection(); + connection.release(); + } catch (error) { + status.mariaDB = 'FAIL'; + status.errors.push('MariaDB connection failed.'); + } + + if (status.errors.length > 0) { + status.status = 'FAIL'; + } else { + status.status = 'OK'; + } + + res.json(status); +}); global.discord_client = client client.on("ready", async () => { diff --git a/log.js b/log.js index c39255d..2784a43 100644 --- a/log.js +++ b/log.js @@ -1,9 +1,9 @@ const colors = require("colors"); module.exports = { info(msg) { - console.log(`${colors.cyan.bold("[INFO]")} ${msg}`); + console.log(`${colors.cyan.bold("[INFO]")}@${new Date().toISOString()} ${msg}`); }, error(msg) { - console.log(`${colors.red.bold("[ERROR]")} ${msg}`); + console.log(`${colors.red.bold("[ERROR]")}@${new Date().toISOString()} ${msg}`); } } \ No newline at end of file