fedtools-utilities
Version:
Set of utilites for fedtools within nodejs
132 lines (117 loc) • 3.48 kB
JavaScript
/**
* Hostile takeover from http-server.
* Fedtools justification: simple web server.
*/
var
_ = require('lodash/core'),
union = require('union'),
portfinder = require('portfinder'),
ecstatic = require('ecstatic'),
log = require('fedtools-logs'),
config = require('fedtools-config'),
DEFAULT_CACHE = 3600,
DEFAULT_PORT = 8080,
LOCALHOST_ALIAS = config.getKey(config.FEDTOOLSRCKEYS.localhostalias) || 'localhost';
function HttpServer(options) {
var
utc,
self = this,
before = [],
serverOptions;
options = options || {};
self.root = options.root || './';
self.logger = {
info: function () {
if (!options.quiet) {
console.log.apply(this, arguments);
}
},
request: function (req, res, error) {
var date = utc ? new Date().toUTCString() : new Date();
if (error) {
self.logger.info(
'[%s] "%s %s" Error (%s): "%s"',
date, log.strToColor('red', req.method), log.strToColor('red', req.url),
log.strToColor('red', error.status.toString()), log.strToColor('red', error.message)
);
} else {
self.logger.info(
'[%s] "%s %s" "%s"',
date, log.strToColor('cyan', req.method), log.strToColor('cyan', req.url),
req.headers['user-agent']
);
}
}
};
before.push(function (req, res) {
self.logger.request(req, res);
res.emit('next');
});
before.push(ecstatic({
root: self.root,
cors: _.isBoolean(options.cors) ? options.cors : false,
gzip: _.isBoolean(options.gzip) ? options.gzip : true,
cache: _.isNumber(options.cache) ? options.cache : DEFAULT_CACHE // 1 hour
}));
serverOptions = {
before: before,
onError: function (err, req, res) {
self.logger.request(req, res, err);
res.end();
}
};
if (_.isObject(options.headers)) {
serverOptions.headers = options.headers;
}
self.server = union.createServer(serverOptions);
}
function _createServer(options) {
return new HttpServer(options);
}
HttpServer.prototype.listen = function () {
this.server.listen.apply(this.server, arguments);
};
HttpServer.prototype.close = function () {
this.server.close.apply(this.server, arguments);
};
exports.listen = function (options, callback) {
var
server;
// allowing callback to be the first and only arg.
if (options && _.isFunction(options)) {
callback = options;
options = {};
}
options = options || {};
options.port = options.port || DEFAULT_PORT;
portfinder.basePort = options.port;
portfinder.getPort(function (err, port) {
if (err) {
throw (err);
} else if (port && port !== options.port && !options.quiet) {
log.error('Port ' + options.port + ' is not available...');
log.warning('Using next available instead: ' + port);
log.echo();
}
options.port = port;
server = _createServer(options);
server.listen(port, '0.0.0.0', function () {
var
url = 'http://' + LOCALHOST_ALIAS + ':' + port,
msg = [];
msg.push('Simple web server is up and running.\n');
msg.push('URL is now available here:');
msg.push(log.strToColor('cyan', url));
msg.push('\nHit CTRL-C to stop the server.');
if (!options.quiet) {
log.printMessagesInBox(msg);
}
if (_.isFunction(callback)) {
return callback(null, {
url: url,
server: server
});
}
});
});
};