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 {
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) {

View File

@ -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',

View File

@ -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) {

View File

@ -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);
}

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
* [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