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/'), mods: paths.join(__dirname, './../mods/'),
loginServers: paths.join(__dirname, './servers/login/'), loginServers: paths.join(__dirname, './servers/login/'),
contentServers: paths.join(__dirname, './servers/content/'), contentServers: paths.join(__dirname, './servers/content/'),
webHandlers: paths.join(__dirname, './servers/content/web_handlers'),
chatServers: paths.join(__dirname, './servers/chat/'), chatServers: paths.join(__dirname, './servers/chat/'),
scannerTossers: paths.join(__dirname, './scanner_tossers/'), scannerTossers: paths.join(__dirname, './scanner_tossers/'),

View File

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

View File

@ -17,6 +17,8 @@ const mimeTypes = require('mime-types');
const forEachSeries = require('async/forEachSeries'); const forEachSeries = require('async/forEachSeries');
const findSeries = require('async/findSeries'); const findSeries = require('async/findSeries');
const { loadModulesForCategory } = require('../../module_util');
const ModuleInfo = (exports.moduleInfo = { const ModuleInfo = (exports.moduleInfo = {
name: 'Web', name: 'Web',
desc: 'Web Server', desc: 'Web Server',
@ -138,6 +140,30 @@ exports.getModule = class WebServerModule extends ServerModule {
return cb(null); 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) { listen(cb) {
const config = Config(); const config = Config();
forEachSeries( forEachSeries(