diff --git a/core/servers/content/web.js b/core/servers/content/web.js index 463c0767..3fb3e370 100644 --- a/core/servers/content/web.js +++ b/core/servers/content/web.js @@ -42,7 +42,7 @@ class Route { try { this.pathRegExp = new RegExp(this.path); } 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() { super(); + this.log = Log.child({ server: 'Web' }); + const config = Config(); this.enableHttp = config.contentServers.web.http.enabled || false; this.enableHttps = config.contentServers.web.https.enabled || false; @@ -84,6 +86,10 @@ exports.getModule = class WebServerModule extends ServerModule { this.routes = {}; } + logger() { + return this.log; + } + getDomain() { const config = Config(); const overridePrefix = _.get(config.contentServers.web.overrideUrlPrefix); @@ -168,23 +174,26 @@ exports.getModule = class WebServerModule extends ServerModule { try { const normalizedName = _.camelCase(module.moduleInfo.name); if (!WebHandlerModule.isEnabled(normalizedName)) { - Log.info( + this.log.info( { moduleName: normalizedName }, - 'Skipping web handler module - not enabled.' + 'Web handler module not enabled' ); return nextModule(null); } Log.info( { moduleName: normalizedName }, - 'Initializing web handler module.' + 'Initializing web handler module' ); - moduleInst.init(err => { + moduleInst.init(this, err => { return nextModule(err); }); } catch (e) { - Log.error(e, 'Exception caught web handler!'); + this.log.error( + { error: e.message }, + 'Exception caught loading web handler' + ); return nextModule(e); } }, @@ -203,7 +212,7 @@ exports.getModule = class WebServerModule extends ServerModule { if (this[name]) { const port = parseInt(config.contentServers.web[service].port); if (isNaN(port)) { - Log.warn( + this.log.error( { port: config.contentServers.web[service].port, server: ModuleInfo.name, @@ -238,7 +247,7 @@ exports.getModule = class WebServerModule extends ServerModule { route = new Route(route); if (!route.isValid()) { - Log.warn( + this.log.error( { route: route }, 'Cannot add route: missing or invalid required members' ); @@ -247,7 +256,7 @@ exports.getModule = class WebServerModule extends ServerModule { const routeKey = route.getRouteKey(); if (routeKey in this.routes) { - Log.warn( + this.log.warn( { route: route, routeKey: routeKey }, 'Cannot add route: duplicate method/path combination exists' ); @@ -259,6 +268,8 @@ exports.getModule = class WebServerModule extends ServerModule { } routeRequest(req, resp) { + this.log.trace({ url: req.url, method: req.method }, 'Request'); + let route = _.find(this.routes, r => r.matchesRequest(req)); if (route) { diff --git a/core/servers/content/web_handlers/activitypub.js b/core/servers/content/web_handlers/activitypub.js index 0125cc4e..57a16b14 100644 --- a/core/servers/content/web_handlers/activitypub.js +++ b/core/servers/content/web_handlers/activitypub.js @@ -14,6 +14,7 @@ const Config = require('../../../config').get; // deps const _ = require('lodash'); const { trim } = require('lodash'); +const enigma_assert = require('../../../enigma_assert'); exports.moduleInfo = { name: 'ActivityPub', @@ -25,15 +26,13 @@ exports.moduleInfo = { exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { constructor() { super(); - - this.log = require('../../../logger').log.child({ webHandler: 'ActivityPub' }); } - init(cb) { - this.webServer = WebHandlerModule.getWebServer(); - if (!this.webServer) { - return cb(Errors.UnexpectedState('Cannot access web server!')); - } + init(webServer, cb) { + this.webServer = webServer; + enigma_assert(webServer, 'ActivityPub Web Handler init without webServer'); + + this.log = webServer.logger().child({ webHandler: 'ActivityPub' }); this.webServer.addRoute({ method: 'GET', diff --git a/core/servers/content/web_handlers/webfinger.js b/core/servers/content/web_handlers/webfinger.js index 27f280c5..a7cf6e82 100644 --- a/core/servers/content/web_handlers/webfinger.js +++ b/core/servers/content/web_handlers/webfinger.js @@ -11,6 +11,7 @@ const { } = require('../../../activitypub_util'); const _ = require('lodash'); +const enigma_assert = require('../../../enigma_assert'); exports.moduleInfo = { name: 'WebFinger', @@ -25,18 +26,16 @@ exports.moduleInfo = { exports.getModule = class WebFingerWebHandler extends WebHandlerModule { constructor() { super(); - - this.log = require('../../../logger').log.child({ webHandler: 'WebFinger' }); } - init(cb) { + init(webServer, cb) { const config = Config(); // we rely on the web server - this.webServer = WebHandlerModule.getWebServer(); - if (!this.webServer) { - return cb(Errors.UnexpectedState('Cannot access web server!')); - } + this.webServer = webServer; + enigma_assert(webServer, 'WebFinger Web Handler init without webServer'); + + this.log = webServer.logger().child({ webHandler: 'WebFinger' }); const domain = this.webServer.getDomain(); if (!domain) { diff --git a/core/web_handler_module.js b/core/web_handler_module.js index b666c933..bd9a8644 100644 --- a/core/web_handler_module.js +++ b/core/web_handler_module.js @@ -6,8 +6,9 @@ module.exports = class WebHandlerModule extends PluginModule { super(options); } - init(cb) { + init(webServer, cb) { // to be implemented! + this.webServer = webHandler; return cb(null); } diff --git a/docs/_docs/servers/contentservers/web-handlers.md b/docs/_docs/servers/contentservers/web-handlers.md index 8064c5ae..ba11a065 100644 --- a/docs/_docs/servers/contentservers/web-handlers.md +++ b/docs/_docs/servers/contentservers/web-handlers.md @@ -7,3 +7,7 @@ Web handlers provide a way to easily add additional _routes_ to your [Web Server # Built in Web Handler Modules * [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 \ No newline at end of file