Web Handlers are now given the parent Web Server directly

This commit is contained in:
Bryan Ashby 2023-01-07 09:50:16 -07:00
parent a1e54dee6d
commit 23f753e4b3
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
5 changed files with 38 additions and 24 deletions

View File

@ -42,7 +42,7 @@ class Route {
try { try {
this.pathRegExp = new RegExp(this.path); this.pathRegExp = new RegExp(this.path);
} catch (e) { } catch (e) {
Log.debug({ route: route }, 'Invalid regular expression for route path'); this.log.error({ route: route }, 'Invalid regular expression for route path');
} }
} }
@ -77,6 +77,8 @@ exports.getModule = class WebServerModule extends ServerModule {
constructor() { constructor() {
super(); super();
this.log = Log.child({ server: 'Web' });
const config = Config(); const config = Config();
this.enableHttp = config.contentServers.web.http.enabled || false; this.enableHttp = config.contentServers.web.http.enabled || false;
this.enableHttps = config.contentServers.web.https.enabled || false; this.enableHttps = config.contentServers.web.https.enabled || false;
@ -84,6 +86,10 @@ exports.getModule = class WebServerModule extends ServerModule {
this.routes = {}; this.routes = {};
} }
logger() {
return this.log;
}
getDomain() { getDomain() {
const config = Config(); const config = Config();
const overridePrefix = _.get(config.contentServers.web.overrideUrlPrefix); const overridePrefix = _.get(config.contentServers.web.overrideUrlPrefix);
@ -168,23 +174,26 @@ exports.getModule = class WebServerModule extends ServerModule {
try { try {
const normalizedName = _.camelCase(module.moduleInfo.name); const normalizedName = _.camelCase(module.moduleInfo.name);
if (!WebHandlerModule.isEnabled(normalizedName)) { if (!WebHandlerModule.isEnabled(normalizedName)) {
Log.info( this.log.info(
{ moduleName: normalizedName }, { moduleName: normalizedName },
'Skipping web handler module - not enabled.' 'Web handler module not enabled'
); );
return nextModule(null); return nextModule(null);
} }
Log.info( Log.info(
{ moduleName: normalizedName }, { moduleName: normalizedName },
'Initializing web handler module.' 'Initializing web handler module'
); );
moduleInst.init(err => { moduleInst.init(this, err => {
return nextModule(err); return nextModule(err);
}); });
} catch (e) { } catch (e) {
Log.error(e, 'Exception caught web handler!'); this.log.error(
{ error: e.message },
'Exception caught loading web handler'
);
return nextModule(e); return nextModule(e);
} }
}, },
@ -203,7 +212,7 @@ exports.getModule = class WebServerModule extends ServerModule {
if (this[name]) { if (this[name]) {
const port = parseInt(config.contentServers.web[service].port); const port = parseInt(config.contentServers.web[service].port);
if (isNaN(port)) { if (isNaN(port)) {
Log.warn( this.log.error(
{ {
port: config.contentServers.web[service].port, port: config.contentServers.web[service].port,
server: ModuleInfo.name, server: ModuleInfo.name,
@ -238,7 +247,7 @@ exports.getModule = class WebServerModule extends ServerModule {
route = new Route(route); route = new Route(route);
if (!route.isValid()) { if (!route.isValid()) {
Log.warn( this.log.error(
{ route: route }, { route: route },
'Cannot add route: missing or invalid required members' 'Cannot add route: missing or invalid required members'
); );
@ -247,7 +256,7 @@ exports.getModule = class WebServerModule extends ServerModule {
const routeKey = route.getRouteKey(); const routeKey = route.getRouteKey();
if (routeKey in this.routes) { if (routeKey in this.routes) {
Log.warn( this.log.warn(
{ route: route, routeKey: routeKey }, { route: route, routeKey: routeKey },
'Cannot add route: duplicate method/path combination exists' 'Cannot add route: duplicate method/path combination exists'
); );
@ -259,6 +268,8 @@ exports.getModule = class WebServerModule extends ServerModule {
} }
routeRequest(req, resp) { routeRequest(req, resp) {
this.log.trace({ url: req.url, method: req.method }, 'Request');
let route = _.find(this.routes, r => r.matchesRequest(req)); let route = _.find(this.routes, r => r.matchesRequest(req));
if (route) { if (route) {

View File

@ -14,6 +14,7 @@ const Config = require('../../../config').get;
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
const { trim } = require('lodash'); const { trim } = require('lodash');
const enigma_assert = require('../../../enigma_assert');
exports.moduleInfo = { exports.moduleInfo = {
name: 'ActivityPub', name: 'ActivityPub',
@ -25,15 +26,13 @@ exports.moduleInfo = {
exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { exports.getModule = class ActivityPubWebHandler extends WebHandlerModule {
constructor() { constructor() {
super(); super();
this.log = require('../../../logger').log.child({ webHandler: 'ActivityPub' });
} }
init(cb) { init(webServer, cb) {
this.webServer = WebHandlerModule.getWebServer(); this.webServer = webServer;
if (!this.webServer) { enigma_assert(webServer, 'ActivityPub Web Handler init without webServer');
return cb(Errors.UnexpectedState('Cannot access web server!'));
} this.log = webServer.logger().child({ webHandler: 'ActivityPub' });
this.webServer.addRoute({ this.webServer.addRoute({
method: 'GET', method: 'GET',

View File

@ -11,6 +11,7 @@ const {
} = require('../../../activitypub_util'); } = require('../../../activitypub_util');
const _ = require('lodash'); const _ = require('lodash');
const enigma_assert = require('../../../enigma_assert');
exports.moduleInfo = { exports.moduleInfo = {
name: 'WebFinger', name: 'WebFinger',
@ -25,18 +26,16 @@ exports.moduleInfo = {
exports.getModule = class WebFingerWebHandler extends WebHandlerModule { exports.getModule = class WebFingerWebHandler extends WebHandlerModule {
constructor() { constructor() {
super(); super();
this.log = require('../../../logger').log.child({ webHandler: 'WebFinger' });
} }
init(cb) { init(webServer, cb) {
const config = Config(); const config = Config();
// we rely on the web server // we rely on the web server
this.webServer = WebHandlerModule.getWebServer(); this.webServer = webServer;
if (!this.webServer) { enigma_assert(webServer, 'WebFinger Web Handler init without webServer');
return cb(Errors.UnexpectedState('Cannot access web server!'));
} this.log = webServer.logger().child({ webHandler: 'WebFinger' });
const domain = this.webServer.getDomain(); const domain = this.webServer.getDomain();
if (!domain) { if (!domain) {

View File

@ -6,8 +6,9 @@ module.exports = class WebHandlerModule extends PluginModule {
super(options); super(options);
} }
init(cb) { init(webServer, cb) {
// to be implemented! // to be implemented!
this.webServer = webHandler;
return cb(null); return cb(null);
} }

View File

@ -7,3 +7,7 @@ Web handlers provide a way to easily add additional _routes_ to your [Web Server
# Built in Web Handler Modules # Built in Web Handler Modules
* [WebFinger](./webfinger-handler.md): Provides basic [WebFinger](https://webfinger.net/) ([RFC7033](https://www.rfc-editor.org/rfc/rfc7033)) support. * [WebFinger](./webfinger-handler.md): Provides basic [WebFinger](https://webfinger.net/) ([RFC7033](https://www.rfc-editor.org/rfc/rfc7033)) support.
## Building Your Own
Adding a Web Handler
TODO