Skip to content

Commit

Permalink
Extensive refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Rakowski committed Jun 12, 2019
1 parent a2ff6a9 commit 72a8c63
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 354 deletions.
40 changes: 27 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,44 @@
//const config = Object.freeze(require('plain-config')());

module.exports = (config) => {

// Holds vhost instances
const oStack = {
list: []
};

// Holds all servers associed with their ports
const serverList = {};

const handler = require('./lib/router.js')(oStack);

const load = require('./lib/load');
load(config, oStack, serverList, handler);
const loader = require('./lib/loader')(config, oStack, serverList, handler);
loader.load();

//load(oStack, serverList, handler);

process.on('message', function (msg) {
switch (msg.action) {
case 'reload':
load(config, oStack, serverList, handler);
break;
default:
console.warn('Unknown inter-process message received: ' + msg.action);
break;
case 'reload':
loader.load();
break;
case 'stop':
loader.close();
break;
default:
console.warn('Unknown inter-process message received: ' + msg.action);
break;
}
});

return loader;

// return {
// reload: () => {
// load(config, oStack, serverList, handler);
// },
// stop: () => {
// require('./lib/close')(serverList, {});
// }
// }
};
11 changes: 11 additions & 0 deletions lib/close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = (serverList, portMap = {}) => {
for (const port in serverList) {
if (serverList[port] && !portMap[port]) {
console.info('Shutting down port:' + port);
serverList[port].close(() => {
console.debug('Instance shut down');
});
delete serverList[port];
}
}
};
56 changes: 0 additions & 56 deletions lib/load.js

This file was deleted.

64 changes: 64 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const fs = require('fs');
const path = require('path');
const vhost = require('./vhost');

module.exports = (config, oStack, serverList, handler) => {

const port_map = {}; // Holds a list of all ports and protocols;

const output = {};

output.load = () => {
const arr_vhost_objects = []; // Holds a list of vhost Objects

let arr_vhost_config = []; // Holds a list of vhost configurations

if(config.controller.vhost) {

if(config.controller.vhost.list) {
arr_vhost_config = arr_vhost_config.concat(config.controller.vhost.list);
}

if(config.controller.vhost.path) {
arr_vhost_config = arr_vhost_config.concat(require('./scan-vhost-dir')(config.controller.vhost.path));
}

if(arr_vhost_config) {
arr_vhost_config.forEach(hostConf => {
const ovh = new vhost(hostConf, config.node.global_path);
if (port_map[ovh.config.vhost.port] && port_map[ovh.config.vhost.port] !== ovh.config.vhost.protocol) {
throw new Error('Invalid configuration. Requested port already used by a different protocol: ' + ovh.config.vhost.port + ' mapped to ' + port_map[ovh.config.vhost.port]);
}
port_map[ovh.config.vhost.port] = ovh.config.vhost.protocol;
arr_vhost_objects.push(ovh);
});
}
}

oStack.list = arr_vhost_objects;
console.info('All vhosts reloaded');

// Shutting down server ports that are not needed anymore;
console.info('Shutting down any unneeded ports');
output.close();
// Adding any missing ports
console.info('Starting any additional ports');
for (const port in port_map) {
if (!serverList[port]) {
const protocol = port_map[port];
const s = require(protocol).createServer(handler).listen(port, (err) => {
console.info('Listening on port %d', s.address().port);
});
serverList[port] = s;
console.info('Started on port:' + port);
}
}
};

output.close = () => {
require('./close')(serverList, {});
};

return output;

};
3 changes: 2 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ module.exports = function(oStack) {
}
if (matched) {
//res.proxyHeaders = [];
matched.executeModules(req, res, function(err, req, res) {
matched.executeModules(req, res, function(err, preq, pres) {
if (err) {
if (err.getStatus) {
res.statusCode = err.getStatus();
} else {
res.statusCode = 500;
}
console.error(err);
res.end();
} else {
res.statusCode = 503;
Expand Down
20 changes: 20 additions & 0 deletions lib/scan-vhost-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const path = require('path');


module.exports = (pathname) => {
let output = [];
fs.readdirSync(path.join(pathname)).forEach(file => {
if (file.match(/\.vhost\.js$/)) {
try {
console.debug('Loading vhost from file: ' + path.join(pathname, file));
const m = path.join(pathname, file);
delete require.cache[m];
output.push(require(m));
} catch(e) {
console.error(e);
}
}
});
return output;
};
21 changes: 15 additions & 6 deletions lib/vhost.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@ module.exports = class vhost {
for (let x = 0; x < this.config.modules.length; x++) {
let module;
try {
console.debug('Loading module: ' + this.config.modules[x].name);
module = require(this.config.modules[x].name)(
//require('../../../hardbox-session-passport')(
this.config.modules[x].config
);
console.debug('Loaded module: ' + this.config.modules[x].name);
} catch(e) {
const gpath = path.resolve(this.globalPath, this.config.modules[x].name);
module = require(gpath)(
//require('../../../hardbox-session-passport')(
this.config.modules[x].config
);
try {
console.debug('Loading module via global: ' + this.config.modules[x].name);
const gpath = path.resolve(this.globalPath, this.config.modules[x].name);
module = require(gpath)(
//require('../../../hardbox-session-passport')(
this.config.modules[x].config
);
console.debug('Loaded module via global: ' + this.config.modules[x].name);
} catch(e) {
console.error(e);
console.error('Failed to load module' + this.config.modules[x].name);
}
}
this.modules.push(module);
if (module) this.modules.push(module);
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions misc/hello.js

This file was deleted.

Loading

0 comments on commit 72a8c63

Please sign in to comment.