From 9048942ce9f15653165819dc80ceddc6e0633c6b Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 4 Oct 2020 02:10:13 +0300 Subject: [PATCH] Expose web server express application to plugins --- docs/plugins.md | 1 + src/main.js | 2 +- src/modules/webserver.js | 21 ++++++++++++--------- src/pluginApi.js | 2 ++ src/plugins.js | 2 ++ 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/docs/plugins.md b/docs/plugins.md index 53929e9..b323f38 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -66,6 +66,7 @@ The first and only argument to the plugin function is an object with the followi | `logs` | An object with functions to get attachment URLs/files and manage log storage types | | `hooks` | An object with functions to add *hooks* that are called at specific times, e.g. before a new thread is created | | `formats` | An object with functions that allow you to replace the default functions used for formatting messages and logs | +| `webserver` | An [Express Application object](https://expressjs.com/en/api.html#app) that functions as the bot's web server | See the auto-generated [Plugin API](plugin-api.md) page for details. diff --git a/src/main.js b/src/main.js index 02e8cd2..e6de612 100644 --- a/src/main.js +++ b/src/main.js @@ -21,7 +21,7 @@ const logs = require("./modules/logs"); const move = require("./modules/move"); const block = require("./modules/block"); const suspend = require("./modules/suspend"); -const webserver = require("./modules/webserver"); +const { plugin: webserver } = require("./modules/webserver"); const greeting = require("./modules/greeting"); const typingProxy = require("./modules/typingProxy"); const version = require("./modules/version"); diff --git a/src/modules/webserver.js b/src/modules/webserver.js index c607573..ae60e9e 100644 --- a/src/modules/webserver.js +++ b/src/modules/webserver.js @@ -54,16 +54,19 @@ function serveAttachments(req, res) { }) } -module.exports = () => { - const server = express(); - server.use(helmet()); +const server = express(); +server.use(helmet()); - server.get("/logs/:threadId", serveLogs); - server.get("/logs/:attachmentId/:filename", serveAttachments); +server.get("/logs/:threadId", serveLogs); +server.get("/logs/:attachmentId/:filename", serveAttachments); - server.on("error", err => { - console.log("[WARN] Web server error:", err.message); - }); +server.on("error", err => { + console.log("[WARN] Web server error:", err.message); +}); - server.listen(config.port); +module.exports = { + server, + plugin() { + server.listen(config.port); + }, }; diff --git a/src/pluginApi.js b/src/pluginApi.js index 662c832..4ae092d 100644 --- a/src/pluginApi.js +++ b/src/pluginApi.js @@ -1,3 +1,4 @@ +const express = require("express"); const { CommandManager } = require("knub-command-manager"); const { Client } = require("eris"); const Knex = require("knex"); @@ -12,6 +13,7 @@ const Knex = require("knex"); * @property {PluginLogsAPI} logs * @property {PluginHooksAPI} hooks * @property {FormattersExport} formats + * @property {express.Application} webserver */ /** diff --git a/src/plugins.js b/src/plugins.js index 4de156d..f50fa64 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -3,6 +3,7 @@ const logs = require("./data/logs"); const { beforeNewThread } = require("./hooks/beforeNewThread"); const { afterThreadClose } = require("./hooks/afterThreadClose"); const formats = require("./formatters"); +const { server: webserver } = require("./modules/webserver"); module.exports = { /** @@ -40,6 +41,7 @@ module.exports = { afterThreadClose, }, formats, + webserver, }; },