This commit is contained in:
Louis Lam 2022-04-30 21:43:39 +08:00
parent dd09351c8e
commit aa872bf782
3 changed files with 52 additions and 0 deletions

38
server/plugins-manager.js Normal file
View File

@ -0,0 +1,38 @@
const fs = require("fs");
const { log } = require("../src/util");
class PluginsManager {
pluginList = [];
/**
* Plugins Dir
*/
pluginsDir;
constructor(dir) {
this.pluginsDir = dir;
if (! fs.existsSync(this.pluginsDir)) {
fs.mkdirSync(this.pluginsDir, { recursive: true });
}
let list = fs.readdirSync(this.pluginsDir);
this.pluginList = [];
for (let item of list) {
let indexFile = this.pluginsDir + item + "/index.js";
if (fs.existsSync(indexFile)) {
this.pluginList.push(require(indexFile));
log.debug("plugin", indexFile);
log.info("plugin", `${item} loaded`);
}
}
}
}
module.exports = {
PluginsManager
};

View File

@ -153,6 +153,8 @@ let needSetup = false;
Database.init(args); Database.init(args);
await initDatabase(testMode); await initDatabase(testMode);
server.loadPlugins(Database.dataDir + "plugins/");
exports.entryPage = await setting("entryPage"); exports.entryPage = await setting("entryPage");
await StatusPage.loadDomainMappingList(); await StatusPage.loadDomainMappingList();

View File

@ -7,6 +7,7 @@ const { R } = require("redbean-node");
const { log } = require("../src/util"); const { log } = require("../src/util");
const Database = require("./database"); const Database = require("./database");
const util = require("util"); const util = require("util");
const { PluginsManager } = require("./plugins-manager");
/** /**
* `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue. * `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue.
@ -35,6 +36,12 @@ class UptimeKumaServer {
*/ */
indexHTML = ""; indexHTML = "";
/**
* Plugins Manager
* @type {PluginsManager}
*/
pluginsManager = null;
static getInstance(args) { static getInstance(args) {
if (UptimeKumaServer.instance == null) { if (UptimeKumaServer.instance == null) {
UptimeKumaServer.instance = new UptimeKumaServer(args); UptimeKumaServer.instance = new UptimeKumaServer(args);
@ -126,6 +133,11 @@ class UptimeKumaServer {
errorLogStream.end(); errorLogStream.end();
} }
loadPlugins(dir) {
this.pluginsManager = new PluginsManager(dir);
}
} }
module.exports = { module.exports = {