Add concept of 'handlers' to web server

This commit is contained in:
Bryan Ashby 2022-12-30 22:35:18 -07:00
parent c8dfecbfcc
commit d2d5aad236
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
3 changed files with 63 additions and 12 deletions

View File

@ -160,6 +160,7 @@ module.exports = () => {
mods: paths.join(__dirname, './../mods/'),
loginServers: paths.join(__dirname, './servers/login/'),
contentServers: paths.join(__dirname, './servers/content/'),
webHandlers: paths.join(__dirname, './servers/content/web_handlers'),
chatServers: paths.join(__dirname, './servers/chat/'),
scannerTossers: paths.join(__dirname, './scanner_tossers/'),

View File

@ -6,6 +6,7 @@ const logger = require('./logger.js');
// deps
const async = require('async');
const isFunction = require('lodash/isFunction');
const listeningServers = {}; // packageName -> info
@ -36,24 +37,47 @@ function startListening(cb) {
(module, nextModule) => {
const moduleInst = new module.getModule();
try {
moduleInst.createServer(err => {
if (err) {
return nextModule(err);
}
async.series(
[
callback => {
return moduleInst.createServer(callback);
},
callback => {
listeningServers[module.moduleInfo.packageName] = {
instance: moduleInst,
info: module.moduleInfo,
};
moduleInst.listen(err => {
if (!isFunction(moduleInst.beforeListen)) {
return callback(null);
}
moduleInst.beforeListen(err => {
return callback(err);
});
},
callback => {
return moduleInst.listen(callback);
},
callback => {
if (!isFunction(moduleInst.afterListen)) {
return callback(null);
}
moduleInst.afterListen(err => {
return callback(err);
});
},
],
err => {
if (err) {
delete listeningServers[
module.moduleInfo.packageName
];
return nextModule(err);
}
listeningServers[module.moduleInfo.packageName] = {
instance: moduleInst,
info: module.moduleInfo,
};
return nextModule(null);
});
});
}
);
} catch (e) {
logger.log.error(e, 'Exception caught creating server!');
return nextModule(e);

View File

@ -17,6 +17,8 @@ const mimeTypes = require('mime-types');
const forEachSeries = require('async/forEachSeries');
const findSeries = require('async/findSeries');
const { loadModulesForCategory } = require('../../module_util');
const ModuleInfo = (exports.moduleInfo = {
name: 'Web',
desc: 'Web Server',
@ -138,6 +140,30 @@ exports.getModule = class WebServerModule extends ServerModule {
return cb(null);
}
beforeListen(cb) {
if (!this.isEnabled()) {
return cb(null);
}
loadModulesForCategory(
'webHandlers',
(module, nextModule) => {
const moduleInst = new module.getModule();
try {
moduleInst.init(err => {
return nextModule(err);
});
} catch (e) {
logger.log.error(e, 'Exception caught web handler!');
return nextModule(e);
}
},
err => {
return cb(err);
}
);
}
listen(cb) {
const config = Config();
forEachSeries(